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
use crate::prelude::*;
pub trait Configure: Default + de::DeserializeOwned + Serialize {
#[deprecated(note = "plan to be removed")]
fn print_toml(&self) {
println!("{:}", self.to_toml().unwrap());
}
fn from_json(s: &str) -> Result<Self> {
let x = serde_json::from_str(s)?;
Ok(x)
}
fn from_toml(s: &str) -> Result<Self> {
let x = toml::from_str(s)?;
Ok(x)
}
fn to_json(&self) -> Result<String> {
let s = serde_json::to_string_pretty(self)?;
Ok(s)
}
fn to_toml(&self) -> Result<String> {
let s = toml::to_string(self)?;
Ok(s)
}
}
impl<T> Configure for T where T: Default + de::DeserializeOwned + Serialize {}
pub use lazy_static::*;
#[cfg(test)]
mod test {
use super::*;
#[derive(Debug, Serialize, Deserialize, Clone)]
pub struct Atom {
symbol: String,
mass: f64,
}
#[derive(Deserialize, Serialize, Debug)]
pub(crate) struct Settings {
pub atoms: Vec<Atom>,
}
impl Default for Settings {
fn default() -> Self {
let atoms = vec![
Atom {
symbol: "H".into(),
mass: 1.008,
},
Atom {
symbol: "C".into(),
mass: 12.011,
},
];
Settings { atoms }
}
}
#[test]
fn test_settings() {
let s = Settings::default().to_json().unwrap();
println!("{}", s);
}
}