-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathlib.rs
More file actions
78 lines (68 loc) · 2.01 KB
/
lib.rs
File metadata and controls
78 lines (68 loc) · 2.01 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
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>,
}
fn default_str() -> String {
"default".to_string()
}
#[derive(Catalyst)]
#[catalyst(bind = Base)]
#[complex(name = "SmallCpx")]
#[allow(dead_code)]
#[complex(attribute(derive(Default)))]
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);
let toml_str = toml::to_string_pretty(&complex).unwrap();
assert_eq!(
toml_str,
r#"field_bool = true
field_string = ""
extra_bool = false
extra_string = ""
"#
);
let toml_str = r#" field_bool = true
field_string = ""
extra_bool = true
"#;
let complex: AmyloidComplex = toml::from_str(toml_str).unwrap();
assert_eq!(complex.extra_string, "default");
}
}