Crates.io | rsconfig-macros |
lib.rs | rsconfig-macros |
version | 0.1.1 |
source | src |
created_at | 2023-01-30 21:05:46.389888 |
updated_at | 2023-01-30 21:05:46.389888 |
description | Macros for RSCONFIG |
homepage | |
repository | https://github.com/HyperCodec/rsconfig-macros |
max_upload_size | |
id | 772094 |
size | 4,726 |
Macro implementation for RSCONFIG
#[derive(Debug, FileConfig)]
struct TestConfig {
test: bool
}
impl YamlConfig for TestConfig {
fn from_yaml(yaml: Vec<yaml_rust::Yaml>) -> Self {
Self { test: *&yaml[0]["test"].as_bool().unwrap() }
}
fn save_yaml(&self, path: &str) -> Result<()> {
let mut data = "test: ".to_string();
data.push_str(self.test.to_string().as_str());
fs::write(path, data).unwrap();
Ok(())
}
}
impl JsonConfig for TestConfig {
fn from_json(val: Value) -> Self {
Self { test: val["test"].as_bool().unwrap() }
}
fn save_json(&self, path: &str) -> io::Result<()> {
// convert to json pretty format and save
let mut m: HashMap<&str, Value> = HashMap::new();
m.insert("test", Value::from(self.test));
let data = serde_json::to_string_pretty(&m).unwrap();
fs::write(path, data).unwrap();
Ok(())
}
}