rsconfig-macros

Crates.iorsconfig-macros
lib.rsrsconfig-macros
version0.1.1
sourcesrc
created_at2023-01-30 21:05:46.389888
updated_at2023-01-30 21:05:46.389888
descriptionMacros for RSCONFIG
homepage
repositoryhttps://github.com/HyperCodec/rsconfig-macros
max_upload_size
id772094
size4,726
Tristan Murphy (HyperCodec)

documentation

README

RSCONFIG-macros

github crates.io docs.rs

Macro implementation for RSCONFIG

Examples

FileConfig

#[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(())
    }
}
Commit count: 6

cargo fmt