serde_typename 0.1.0

Conviently serialize and deserialize rust types into / from their serde name.
Documentation
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
#![warn(missing_docs)]
#![warn(rustdoc::missing_doc_code_examples)]
//!Conveniently serialize and deserialize rust types into / from their serde name.
//!
//!## Usage
//!
//!```rust
//!use serde::{Serialize, Deserialize};
//!use serde_typename::{to_str, from_str};
//!
//!/// Enums
//!
//!#[derive(Debug, PartialEq, Serialize, Deserialize)]
//!enum Enum {
//!    UnitVariant,
//!    #[serde(rename = "RENAMED")]
//!    Renamed,
//!    HoldsData(u8),
//!    HoldsDataAsTuple(u8, u8),
//!    HoldsDataAsStruct { field: u8 }
//!}
//!
//!// Safe to serialize, held data gets discarded
//!assert_eq!(to_str(&Enum::UnitVariant).unwrap(), "UnitVariant");
//!assert_eq!(to_str(&Enum::Renamed).unwrap(), "RENAMED");
//!assert_eq!(to_str(&Enum::HoldsData(0)).unwrap(), "HoldsData");
//!assert_eq!(to_str(&Enum::HoldsDataAsTuple(0, 0)).unwrap(), "HoldsDataAsTuple");
//!assert_eq!(to_str(&Enum::HoldsDataAsStruct { field: 0 }).unwrap(), "HoldsDataAsStruct");
//!
//!// Safe to deserialize since no data is held
//!assert_eq!(from_str::<Enum>("UnitVariant").unwrap(), Enum::UnitVariant);
//!assert_eq!(from_str::<Enum>("RENAMED").unwrap(), Enum::Renamed);
//!// Cant be deserialized since their data was lost during serialization
//!assert!(from_str::<Enum>("HoldsData").is_err());
//!assert!(from_str::<Enum>("HoldsDataAsTuple").is_err());
//!assert!(from_str::<Enum>("HoldsDataAsStruct").is_err());
//!
//!/// Structs
//!
//!#[derive(Debug, PartialEq, Serialize, Deserialize)]
//!struct UnitStruct;
//!assert_eq!(to_str(&UnitStruct).unwrap(), "UnitStruct");
//!assert_eq!(from_str::<UnitStruct>("UnitStruct").unwrap(), UnitStruct);
//!
//!#[derive(Debug, PartialEq, Serialize, Deserialize)]
//!struct TupleStruct(u64, u64);
//!assert_eq!(to_str(&TupleStruct(0, 0)).unwrap(), "TupleStruct");
//!assert!(from_str::<TupleStruct>("TupleStruct").is_err());
//!
//!#[derive(Debug, PartialEq, Serialize, Deserialize)]
//!struct Struct {
//!    field: u8
//!}
//!assert_eq!(to_str(&Struct { field: 123 }).unwrap(), "Struct");
//!assert!(from_str::<Struct>("Struct").is_err());
//!```

mod de;
mod error;
mod ser;

use serde::Deserialize;
use serde::Serialize;

pub use de::Deserializer;
pub(crate) use error::ErrorCode;
pub use error::{Error, Result};
pub use ser::Serializer;

/// Convert enums and structs into its variant name
///
/// Keep in mind that all data held by the value is
/// discarded and only the type name is serialized
///
/// ```rust
///use serde::{Serialize, Deserialize};
///use serde_typename::{to_str, from_str};
///
///#[derive(Debug, PartialEq, Serialize, Deserialize)]
///enum Enum {
///    NoData,
///    WithData(u8),
///}
///assert_eq!(to_str(&Enum::NoData).unwrap(), "NoData");
///assert_eq!(to_str(&Enum::WithData(1)).unwrap(), "WithData");
///
///#[derive(Debug, PartialEq, Serialize, Deserialize)]
///struct Unit;
///assert_eq!(to_str(&Unit).unwrap(), "Unit");
///
///#[derive(Debug, PartialEq, Serialize, Deserialize)]
///struct Tuple(u64, u64);
///assert_eq!(to_str(&Tuple(1, 2)).unwrap(), "Tuple");
///
///#[derive(Debug, PartialEq, Serialize, Deserialize)]
///struct Struct {
///    field: u8
///}
///assert_eq!(to_str(&Struct { field: 1 }).unwrap(), "Struct");
/// ```
pub fn to_str<T>(value: &T) -> Result<&'static str>
where
    T: Serialize,
{
    let mut serializer = ser::Serializer {};
    value.serialize(&mut serializer)
}

/// Convert a variant name back into an enum or struct if possible
///
/// Keep in mind that all target variants or structs which
/// hold data (newtype, tuple, field variants / structs)
/// can't be deserialized since data was discarded during
/// serialization
///
/// ```rust
///use serde::{Serialize, Deserialize};
///use serde_typename::{to_str, from_str};
///
///#[derive(Debug, PartialEq, Serialize, Deserialize)]
///enum Enum {
///    NoData,
///    WithData(u8),
///}
///assert_eq!(from_str::<Enum>("NoData").unwrap(), Enum::NoData);
///assert!(from_str::<Enum>("WithData").is_err());
///
///#[derive(Debug, PartialEq, Serialize, Deserialize)]
///struct Unit;
///assert_eq!(from_str::<Unit>("Unit").unwrap(), Unit);
///
///#[derive(Debug, PartialEq, Serialize, Deserialize)]
///struct Tuple(u64, u64);
///assert!(from_str::<Tuple>("Tuple").is_err());
///
///#[derive(Debug, PartialEq, Serialize, Deserialize)]
///struct Struct {
///    field: u8
///}
///assert!(from_str::<Struct>("Struct").is_err());
/// ```
pub fn from_str<'a, D>(value: &'a str) -> Result<D>
where
    D: Deserialize<'a>,
{
    let mut deserializer = de::Deserializer::new(value);
    let variant = D::deserialize(&mut deserializer)?;
    if deserializer.input.is_empty() {
        Ok(variant)
    } else {
        Err(Error::deserialization(error::ErrorCode::TrailingCharacters))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    mod ser {
        use super::*;

        mod enums {
            use super::*;

            #[test]
            fn unit_variants() {
                #[derive(Serialize)]
                enum Foo {
                    Var1,
                    #[serde(rename = "VAR2")]
                    Var2,
                }

                assert_eq!(to_str(&Foo::Var1).unwrap(), "Var1");
                assert_eq!(to_str(&Foo::Var2).unwrap(), "VAR2");
            }

            #[test]
            fn newtype_variants() {
                #[derive(Serialize)]
                enum Foo {
                    Var1(()),
                    #[serde(rename = "VAR2")]
                    Var2(u32),
                }

                assert_eq!(to_str(&Foo::Var1(())).unwrap(), "Var1");
                assert_eq!(to_str(&Foo::Var2(42)).unwrap(), "VAR2");
            }

            #[test]
            fn tuple_variants() {
                #[derive(Serialize)]
                enum Foo {
                    BAz((), u64),
                    #[serde(rename = "VAR")]
                    Var((), (), ()),
                }

                assert_eq!(to_str(&Foo::BAz((), 1337)).unwrap(), "BAz");
                assert_eq!(to_str(&Foo::Var((), (), ())).unwrap(), "VAR");
            }

            #[test]
            fn struct_variants() {
                #[derive(Serialize)]
                enum Foo {
                    Var1 {
                        field: u8,
                    },
                    #[serde(rename = "Renamed")]
                    Var2 {
                        foo: &'static str,
                    },
                }

                assert_eq!(to_str(&Foo::Var1 { field: 0 }).unwrap(), "Var1");
                assert_eq!(to_str(&Foo::Var2 { foo: "BAR" }).unwrap(), "Renamed");
            }
        }

        mod structs {
            use super::*;

            #[test]
            fn unit_structs() {
                #[derive(Serialize)]
                struct Bar;

                assert_eq!(to_str(&Bar).unwrap(), "Bar");
            }

            #[test]
            fn newtype_structs() {
                #[derive(Serialize)]
                struct Bar(u64);

                assert_eq!(to_str(&Bar(42)).unwrap(), "Bar");
            }

            #[test]
            fn tuple_structs() {
                #[derive(Serialize)]
                struct Bar(u64, u64);

                assert_eq!(to_str(&Bar(1, 42)).unwrap(), "Bar");
            }

            #[test]
            fn field_structs() {
                #[derive(Serialize)]
                struct Bar {
                    field: u8,
                }

                assert_eq!(to_str(&Bar { field: 0 }).unwrap(), "Bar");
            }
        }
    }

    mod de {
        use super::*;

        mod enums {
            use super::*;

            #[test]
            fn case_sensisitive() {
                #[derive(Debug, PartialEq, Deserialize)]
                #[allow(non_camel_case_types)]
                enum Foo {
                    foo,
                    FoO,
                    FOO,
                    fOO,
                }

                assert_eq!(from_str::<Foo>("foo").unwrap(), Foo::foo);
                assert_eq!(from_str::<Foo>("FoO").unwrap(), Foo::FoO);
                assert_eq!(from_str::<Foo>("FOO").unwrap(), Foo::FOO);
                assert_eq!(from_str::<Foo>("fOO").unwrap(), Foo::fOO);
                assert!(from_str::<Foo>("Foo").is_err());
                assert!(from_str::<Foo>("fOo").is_err());
                assert!(from_str::<Foo>("foO").is_err());
                assert!(from_str::<Foo>("FOo").is_err());
            }

            #[test]
            fn space_sensisitive() {
                #[derive(Debug, PartialEq, Deserialize)]
                #[allow(non_camel_case_types)]
                enum Foo {
                    Foo,
                }

                assert_eq!(from_str::<Foo>("Foo").unwrap(), Foo::Foo);
                assert!(from_str::<Foo>("Foo ").is_err());
                assert!(from_str::<Foo>(" Foo").is_err());
                assert!(from_str::<Foo>("F oo").is_err());
                assert!(from_str::<Foo>("F o o").is_err());
            }

            #[test]
            fn unit_variants() {
                #[derive(Debug, PartialEq, Deserialize)]
                enum Foo {
                    Var1,
                    #[serde(rename = "VAR2")]
                    Var2,
                }

                assert_eq!(from_str::<Foo>("Var1").unwrap(), Foo::Var1);
                assert_eq!(from_str::<Foo>("VAR2").unwrap(), Foo::Var2);
            }

            mod impossible {
                use super::*;

                #[test]
                fn newtype_variants() {
                    #[derive(Debug, PartialEq, Deserialize)]
                    enum Foo {
                        Foo(u8),
                    }

                    assert!(from_str::<Foo>("Foo").is_err());
                }

                #[test]
                fn tuple_variants() {
                    #[derive(Debug, PartialEq, Deserialize)]
                    enum Foo {
                        BAz(u8),
                        #[serde(rename = "VAR")]
                        Var((), (), u8),
                    }

                    assert!(from_str::<Foo>("BAz").is_err());
                    assert!(from_str::<Foo>("VAR").is_err());
                }

                #[test]
                fn struct_variants() {
                    #[derive(Debug, PartialEq, Deserialize)]
                    enum Foo {
                        BAz(u8),
                        #[serde(rename = "VAR")]
                        Var((), (), u8),
                    }

                    assert!(from_str::<Foo>("BAz").is_err());
                    assert!(from_str::<Foo>("VAR").is_err());
                }
            }
        }

        mod structs {
            use super::*;

            #[test]
            #[allow(non_camel_case_types)]
            fn case_sensisitive() {
                #[derive(Debug, Deserialize, PartialEq)]
                struct foo;
                #[derive(Debug, Deserialize, PartialEq)]
                struct FoO;
                #[derive(Debug, Deserialize, PartialEq)]
                struct FOO;
                #[derive(Debug, Deserialize, PartialEq)]
                struct fOO;

                assert_eq!(from_str::<foo>("foo").unwrap(), foo);
                assert_eq!(from_str::<FoO>("FoO").unwrap(), FoO);
                assert_eq!(from_str::<FOO>("FOO").unwrap(), FOO);
                assert_eq!(from_str::<fOO>("fOO").unwrap(), fOO);

                assert!(from_str::<foo>("Foo").is_err());
                assert!(from_str::<foo>("FoO").is_err());
                assert!(from_str::<foo>("FoO").is_err());
                assert!(from_str::<foo>("FOo").is_err());

                assert!(from_str::<FoO>("Foo").is_err());
                assert!(from_str::<FoO>("foO").is_err());
                assert!(from_str::<FoO>("foo").is_err());
                assert!(from_str::<FoO>("FOo").is_err());

                assert!(from_str::<FOO>("Foo").is_err());
                assert!(from_str::<FOO>("foO").is_err());
                assert!(from_str::<FOO>("FoO").is_err());
                assert!(from_str::<FOO>("FOo").is_err());

                assert!(from_str::<fOO>("Foo").is_err());
                assert!(from_str::<fOO>("foO").is_err());
                assert!(from_str::<fOO>("foO").is_err());
                assert!(from_str::<fOO>("FOo").is_err());
            }

            #[test]
            fn space_sensisitive() {
                #[derive(Debug, Deserialize, PartialEq)]
                struct Foo;

                assert_eq!(from_str::<Foo>("Foo").unwrap(), Foo);
                assert!(from_str::<Foo>("Foo ").is_err());
                assert!(from_str::<Foo>(" Foo").is_err());
                assert!(from_str::<Foo>("F oo").is_err());
                assert!(from_str::<Foo>("F o o").is_err());
            }

            #[test]
            fn unit_struct() {
                #[derive(Debug, Deserialize, PartialEq)]
                struct Foo;

                #[derive(Debug, Deserialize, PartialEq)]
                #[serde(rename = "BAR")]
                struct Bar;

                assert_eq!(from_str::<Foo>("Foo").unwrap(), Foo);
                assert_eq!(from_str::<Bar>("BAR").unwrap(), Bar);

                assert!(from_str::<Bar>("bAR").is_err());
                assert!(from_str::<Bar>("bar").is_err());
            }

            mod impossible {
                use super::*;

                #[test]
                fn newtype_struct() {
                    #[derive(Debug, Deserialize, PartialEq)]
                    struct Foo(u8);

                    assert!(from_str::<Foo>("Foo").is_err());
                }

                #[test]
                fn field_struct() {
                    #[derive(Debug, Deserialize, PartialEq)]
                    struct Foo {
                        field: u8,
                    }

                    assert!(from_str::<Foo>("Foo").is_err());
                }
            }
        }
    }
}