-
Notifications
You must be signed in to change notification settings - Fork 0
/
presentation.html
468 lines (328 loc) · 10.3 KB
/
presentation.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
<!DOCTYPE html>
<html>
<head>
<title>Effective serde By Writing Less Rust Code</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
<style type="text/css">
body { font-family: san-serif; }
h1, h2, h3 {
font-weight: normal;
}
.remark-code, .remark-inline-code { font-family: 'Ubuntu Mono'; }
</style>
</head>
<body>
<textarea id="source">
class: center, middle
Effective `serde`°
=========================
By Writing Less Rust Code
=========================
### Topics on the Rust Programming Language
### Vancouver Rust meetup, 17 April 2019
[Daniel J. Pezely](https://pezely.com/daniel)
`dpezely` on
[GitHub](https://github.com/dpezely/),
[GitLab](https://gitlab.com/dpezely/),
[Linkedin](https://ca.linkedin.com/in/dpezely)
> ° “serde” means <em><b>ser</b></em>ialize / <em><b>de</b></em>serialize
> and is the name of a Rust crate
---
class: center, middle
## "My deep hierarchy of data structures is too complicated for auto-conversion."
### --someone _not_ using serde
---
class: middle
## Contents:
1. **The Way Of Serde**
2. **A Realistic Example** -- minimalist data files
3. **Simple Hierarchy of Enums** -- simple tricks
4. **Untagged Enums** -- "... indistinguishable from magic"
5. **Renaming Variants** -- Pretty JSON and prettier Rust
6. **Error Handling** -- using `?` early and often
7. **Flattening** -- but still writing less code
8. **Asymmetric JSON** -- populate Rust fields only when JSON is non-null
Of course, all this applies to far more than just JSON
But JSON is easier for presentation purposes here
---
class: center, middle
# Take time to read https://serde.rs/ *entirely*
## before jumping into API docs at https://crates.io/crates/serde
## You'll find it time well-invested!
Spoilers: it's resolved entirely at compile-time, and
**without** run-time “reflection” mechanisms
---
class: middle
# The Way Of Serde
### Let serde give you superpowers by relying upon:
### I. Decorate structs & enums with attributes
### II. Write methods of auto-convert traits
### III. Coalesce errors via `?` operator
Bonus: Deep or mixed structures? Easy!
---
### The Way Of Serde
## I. Decorate structs with attributes
- *Container* attributes for `struct` or `enum` declaration
- *Variant* attributes for each variant of an `enum`
- *Field* attributes for individual `struct` field or within `enum` variant
> See [serde.rs/attributes.html](https://serde.rs/attributes.html)
---
### The Way Of Serde
## II. Write methods of auto-convert traits
- If writing code handling common patterns:
+ That's probably the wrong approach!
- If writing code to handle name or value conversions:
+ That's probably the wrong approach!
- If checking for existence of nulls or special values:
+ That's probably the wrong approach!
---
### The Way Of Serde
## III. Coalesce errors via `?` operator
- Make aggressive use of `?` operator
+ e.g., use `Result` and `ErrorKind` together
- Implement various methods of `From` and `Into` traits
+ compiler reveals exactly what you need
+ so this becomes fairly straight-forward plug-and-chug
- A common Rust idiom-- not just a `serde` thing
---
### The Way Of Serde
## IV. Deep or mixed structures? Easy!
- Populate a nested `enum` and their variants from a flattened set
+ i.e., each variant must map to exactly one Enum
+ then, nested Enums may be resolved when decorating with a single attribute
- Ingest minimal data file structures to well-defined structures in Rust
+ e.g., JSON without naming each structural component
+ where keys contain data (_NOT_ name of struct)
- Thus, have your idiomatic Rust cake and eat minimalist data files too!
???
For those that are non-native to English, "Wanting to have your cake and eat
it too" simply indicates the impression of a paradox. For those using
serde, however, there is no paradox at all.
---
class: middle
# 2. A Realistic Example:
### a) Each entry may have multiple categories
### b) Given as a flattened set in JSON
### c) Expand to well-defined structs in Rust
---
## Unpacking Minimalist JSON
```JSON
{
"energy-preferences": {
"2000s": ["solar", "wind"],
"1900s": ["kerosene", "soy", "peanut", "petroleum"],
"1800s": ["wind", "whale", "seal", "kerosene"]
}
}
```
Notable:
- Outer structure is an object (_NOT_ an array)
- Top-level keys contain information (_NOT_ name of structure)
- Inner values within array indicate mixed categories
---
## Starting From The Top
### Serde can handle various naming conventions
### e.g., snake_case, camelCase, PascalCase, kebab-case, etc.
```rust
#[derive(Serialize, Deserialize, Debug)]
#[serde(rename_all = "kebab-case")]
struct EnergyPreferenceHistory {
energy_preferences: EnergyPreferences
}
#[derive(Serialize, Deserialize, Debug)]
struct EnergyPreferences (HashMap<Century, Vec<EnergySources>>);
```
### See [serde.rs/attributes.html](https://serde.rs/attributes.html)
### Particularly, [serde.rs/container-attrs.html](https://serde.rs/container-attrs.html#rename_all)
---
## Avoid Merging Concepts In An `enum`
```rust
enum EnergySources { // Don't mix categories like this!
Solar,
Wind,
// ...
Kerosene,
Petroleum,
// ...
PeanutOil,
SoyOil,
// ...
SealBlubber,
WhaleBlubber,
// ...
}
```
### It would be more idiomatic Rust
### grouping them by category, instead
---
# 3. Simple Hierarchy Of Enums
Continuing from previous example...
```rust
enum EnergySources {
Sustainable(Inexhaustible),
Animal(Blubber),
Vegetable(Crop),
Mineral(Fossil),
}
enum Inexhaustible { Solar, Wind, /* ... */ }
enum Blubber { Seal, Whale, /* ... */ }
enum Crop { Peanut, Soy, /* ... */ }
enum Fossil { Kerosene, Petroleum, /* ... */ }
```
### This is more idomatic Rust
### But our data file doesn't look anything like this... Fear not!
???
Focus on the Rust code, not precision of these categories.
For instance, pulp or pellets made from trees or other vegetable matter are
all ignored here yet were in common usage during the late Nineteenth and
early Twentieth Century within North America.
Other divisions or categories might be better, such as petrochemical,
oleochemical, etc. Or rendered, cultivated, extracted, etc.
---
# 4. Untagged Enums
## Decorate With *Attributes*:
Continuing from previous example...
```rust
#[derive(Serialize, Deserialize, Debug)]
#[serde(untagged)] // <-- Unflatten from compact JSON
enum EnergySources {
Sustainable(Inexhaustible),
Animal(Blubber),
Vegetable(Crop),
Mineral(Fossil),
}
```
See "Untagged" section in
[serde.rs/enum-representations.html](https://serde.rs/enum-representations.html#untagged)
---
# 5. Renaming Variants
### Pretty JSON and prettier Rust
```rust
#[derive(Serialize, Deserialize, Debug, PartialEq, Eq, Hash)]
enum Century {
#[serde(rename = "1800s")]
NinteenthCentury,
#[serde(rename = "1900s")]
TwentiethCentury,
#[serde(rename = "2000s")]
TwentyfirstCentury
}
```
### Each has its preferred naming convention
### Note additional attributes: `PartialEq`, `Eq`, `Hash`
---
# 6. Error Handling
### Use `?` early and often:
```rust
fn main() -> Result<(), ErrorKind> {
let json_string = fs::read_to_string("energy.json")?;
let sources: EnergyPreferenceHistory =
serde_json::de::from_str(&json_string)?;
println!("{:#?}", sources);
Ok(())
}
```
Note uses of question mark `?` operator above
### Implementing just the above, the compiler helpfully tells you exactly which `impl From` methods to add
---
## Example `ErrorKind`
### For Use With `Result` Type
Continuing from previous example...
```rust
#[derive(Debug)]
enum ErrorKind {
BadJson,
NoJson,
NoFilePath,
Unknown,
}
```
---
## Implementing `From` methods
### For Use With `?` Operator
```rust
impl From<serde_json::Error> for ErrorKind {
fn from(err: serde_json::Error) -> ErrorKind {
use serde_json::error::Category;
match err.classify() {
Category::Io => {
println!("Serde JSON IO-error: {:?}", &err);
ErrorKind::NoJson
}
Category::Syntax | Category::Data | Category::Eof => {
println!("Serde JSON error: {:?} {:?}",
err.classify(), &err);
ErrorKind::BadJson
}
}
}
}
```
---
class: center, middle
# Other Powerful Features Of
# `serde`
## By Writing Less Code
---
# 7. Flattening
```rust
#[derive(Serialize, Deserialize)]
struct CatalogueEntry {
id: u64,
#[serde(flatten)] // <-- Field Attribute
description: HashMap<String, String>,
}
```
Would ultimately produce the following JSON representation:
```JSON
{
"id": 1234,
"size": "bigger than a car",
"weight": "less than an airplane"
}
```
All fields rendered to same level within JSON
See [serde.rs/field-attrs.html](https://serde.rs/field-attrs.html#flatten)
---
### Write the preceding item to JSON file
```rust
fn populate_catalogue() -> Result<(), ErrorKind> {
let id = 1234;
let mut description = HashMap::new();
description.insert("size".to_string(),
"bigger than a house".to_string());
description.insert("weight".to_string(),
"less than an airplane".to_string());
let catalogue = vec![CatalogueEntry{id, description}];
fs::write("foo.json", serde_json::to_string(&catalogue)?)?;
Ok(())
}
```
### Nothing special here
### Serde handles iterables-- just implement the trait
---
# 8. Asymmetric JSON
## Populate fields only when non-null
```rust
struct Thing {
pub keyword: String,
#[serde(default="Vec::new")] // <-- constructor
pub attributes: Vec<String>,
}
```
### This yields an empty `Vec`
### instead of Vec with empty string
### without wrapping value with `Option`
---
## Incentive To Read [serde.rs](https://serde.rs/):
### [Borrowing data in a derived impl](https://serde.rs/lifetimes.html#borrowing-data-in-a-derived-impl)
### When data has already been loaded and memory allocated:
### Let your deserialized structs track only references
</textarea>
<script src="remark-latest.min.js" type="text/javascript">
</script>
<script type="text/javascript">
var slideshow = remark.create();
</script>
</body>
</html>