-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathusage.rs
More file actions
94 lines (87 loc) · 2.21 KB
/
usage.rs
File metadata and controls
94 lines (87 loc) · 2.21 KB
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
/* Feature gates */
// ensure the new derive macro doesn't cause trouble with no_std support
#![no_std]
/* Clippy config */
#![allow(dead_code, clippy::unwrap_used)]
/* Built-in imports */
extern crate alloc;
use alloc::string::String;
/* Dependencies */
use serde_versioning::Deserialize;
const V0: &str = r#"{ "name": "vic1707" }"#;
const V1: &str = r#"{ "name": "vic1707", "age": 0 }"#;
const V2: &str = r#"{ "name": "vic1707", "age": 0, "placeholder": "hi" }"#;
const VF: &str = r#"{ "name": "vic1707", "age": 0, "place_holder": "hi" }"#;
#[derive(Deserialize)]
struct FooV0 {
name: String,
}
#[derive(Deserialize)]
#[versioning(previous_version = "FooV0")]
struct FooV1 {
name: String,
age: u8,
}
#[derive(Deserialize)]
#[versioning(pessimistic, previous_versions = [FooV0, "FooV1"])]
struct FooV2 {
name: String,
age: u8,
placeholder: String,
}
#[derive(Deserialize, Default)]
#[versioning(previous_version = FooV2, optimistic)]
struct Foo {
name: String,
age: u8,
#[serde(default)]
place_holder: String,
}
fn main() {
// FooV0
serde_json::from_str::<FooV0>(V0).unwrap();
// FooV1
serde_json::from_str::<FooV0>(V1).unwrap();
serde_json::from_str::<FooV1>(V1).unwrap();
// FooV2
serde_json::from_str::<FooV2>(V0).unwrap();
serde_json::from_str::<FooV2>(V1).unwrap();
serde_json::from_str::<FooV2>(V2).unwrap();
// Foo
serde_json::from_str::<Foo>(V0).unwrap();
serde_json::from_str::<Foo>(V1).unwrap();
serde_json::from_str::<Foo>(V2).unwrap();
serde_json::from_str::<Foo>(VF).unwrap();
}
// all type conversions needed, will also work with TryFrom
impl From<FooV0> for FooV1 {
fn from(v0: FooV0) -> Self {
Self {
name: v0.name,
age: 0,
}
}
}
impl From<FooV1> for FooV2 {
fn from(v1: FooV1) -> Self {
Self {
name: v1.name,
age: v1.age,
placeholder: String::new(),
}
}
}
impl From<FooV0> for FooV2 {
fn from(v0: FooV0) -> Self {
Self {
name: v0.name,
age: 0,
placeholder: String::new(),
}
}
}
impl From<FooV2> for Foo {
fn from(_: FooV2) -> Self {
Self::default()
}
}