-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlib.rs
More file actions
154 lines (137 loc) · 4.61 KB
/
Copy pathlib.rs
File metadata and controls
154 lines (137 loc) · 4.61 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
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
use serde::{Deserialize, Serialize};
use struct_patch::Catalyst;
use substrate::Base;
#[derive(Default, Catalyst)]
#[catalyst(bind = Base)]
// The Substrate has `#[serde(...)]` on fields , and catalyst keep_field_attribute
// so the complex should have the corresponding Serialize derive
#[catalyst(keep_field_attribute)]
#[complex(attribute(derive(Debug, Deserialize, Serialize)))]
#[allow(dead_code)]
struct Amyloid {
pub extra_bool: bool,
#[complex(attribute(serde(default = "default_str")))]
pub extra_string: String,
pub extra_option: Option<usize>,
#[complex(attribute(serde(default = "default_extra_private_number")))]
extra_private_number: u8,
}
fn default_str() -> String {
"default".to_string()
}
fn default_extra_private_number() -> u8 {
7
}
#[allow(dead_code)]
impl AmyloidComplex {
/// Sum up the private_number(from Substrate) and the extra_private_number(from Catalyst)
fn private_number_sum(&self) -> u8 {
self.private_number + self.extra_private_number
}
}
#[derive(Default, Catalyst)]
#[catalyst(bind = Base)]
#[catalyst(keep_field_attribute)]
#[catalyst(exclude_field_attributes = ["serde"])]
#[complex(attribute(derive(Debug, Deserialize)))]
#[allow(dead_code)]
struct ExcludedAmyloid {
pub extra_bool: bool,
}
#[derive(Catalyst)]
#[catalyst(bind = Base)]
#[complex(name = "SmallCpx")]
#[allow(dead_code)]
#[complex(attribute(derive(Default, Deserialize)))]
#[complex(override_field_attribute("field_string", serde(default = "default_str")))]
#[complex(override_field_attribute("field_string", serde(rename = "renamed_field")))]
struct SmallAmyloid {
pub extra_bool: bool,
}
#[allow(dead_code)]
impl SmallCpx {
/// A reaction to change the substrate
pub fn reaction(&mut self) {
self.field_bool = true;
}
}
#[cfg(test)]
mod tests {
use super::*;
use struct_patch::Complex;
#[test]
fn complex_works() {
let mut small_complex = SmallCpx::default();
assert_eq!(small_complex.field_bool, false);
assert_eq!(small_complex.field_string, String::new());
assert_eq!(small_complex.field_option, None);
assert_eq!(small_complex.extra_bool, false);
small_complex.reaction();
let (_cat, substrate) = small_complex.decouple();
assert!(substrate.has_bool());
let amyloid = Amyloid::default();
let complex = amyloid.bind(substrate);
assert_eq!(complex.field_bool, true);
assert_eq!(complex.private_number_sum(), 0);
let toml_str = toml::to_string_pretty(&complex).unwrap();
assert_eq!(
toml_str,
r#"field_bool = true
field_string = ""
private_number = 0
extra_bool = false
extra_string = ""
extra_private_number = 0
"#
);
let toml_str = r#" field_bool = true
field_string = ""
private_number = 1
extra_bool = true
"#;
let complex: AmyloidComplex = toml::from_str(toml_str).unwrap();
assert_eq!(complex.extra_string, "default");
// the `extra_private_number` is 7 generated by `default_extra_private_number`, when serialize with a missing field
// such that the `private_number` + `extra_private_number` = 1 + 7 = 8
assert_eq!(complex.private_number_sum(), 8);
}
#[test]
fn exclude_field_attributes_works() {
// Base has `#[serde(default)]` on `field_bool`. With exclude_field_attributes = ["serde"],
// that attribute is stripped from the complex, making `field_bool` a required field.
let toml_missing_field_bool = r#"field_string = "test"
private_number = 0
extra_bool = false
"#;
assert!(
toml::from_str::<ExcludedAmyloidComplex>(toml_missing_field_bool).is_err(),
"field_bool should be required when serde attributes are excluded"
);
let toml_all_fields = r#"field_bool = true
field_string = "test"
private_number = 0
extra_bool = false
"#;
let complex: ExcludedAmyloidComplex =
toml::from_str(toml_all_fields).expect("all required fields provided");
assert_eq!(complex.field_bool, true);
assert_eq!(complex.field_string, "test");
assert_eq!(complex.extra_bool, false);
}
#[test]
fn override_works() {
let toml_str = r#"field_bool = false
private_number = 0
extra_bool = false
"#;
let complex: SmallCpx = toml::from_str(toml_str).unwrap();
assert_eq!(complex.field_string, "default");
let toml_str = r#"field_bool = false
private_number = 0
renamed_field = "Renamed"
extra_bool = false
"#;
let complex: SmallCpx = toml::from_str(toml_str).unwrap();
assert_eq!(complex.field_string, "Renamed");
}
}