1 unstable release

0.1.1 Jan 30, 2023

#133 in #derive-debug

MIT/Apache

5KB

RSCONFIG-macros

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(())
    }
}

Dependencies

~1.6–2.2MB
~53K SLoC