config_file_derives

Crates.ioconfig_file_derives
lib.rsconfig_file_derives
version2024.11.25
sourcesrc
created_at2024-11-14 02:43:23.416682
updated_at2024-11-25 01:56:52.067352
descriptionAutomatically generate file configuration functions for the specified struct
homepage
repositoryhttps://github.com/ascpkg/asc/tree/main/config_file_derives
max_upload_size
id1447359
size9,159
Capric (capric8416)

documentation

README

Wrap struct to file configuration

  • hcl

  • json

  • toml

  • xml

  • yml

use std::collections::{BTreeMap, BTreeSet};

use serde::{Deserialize, Serialize};

use config_file_derives::ConfigFile;
use config_file_types;

#[derive(Debug, Default, Deserialize, Serialize, ConfigFile)]
#[config_file_ext("toml")]
pub struct MyTomlConfig {
    pub dependencies: BTreeMap<String, BTreeSet<String>>,

    #[serde(skip)]
    pub path: String,
}

#[derive(Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq, Deserialize, Serialize, ConfigFile)]
#[config_file_ext("json")]
pub struct MyJsonConfig {
    pub dependencies: BTreeMap<String, BTreeSet<String>>,

    #[serde(skip)]
    pub path: String,
}

#[derive(Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq, Deserialize, Serialize, ConfigFile)]
#[config_file_ext("xml")]
pub struct MyXmlConfig {
    pub dependencies: BTreeMap<String, BTreeSet<String>>,

    #[serde(skip)]
    pub path: String,
}

#[derive(Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq, Deserialize, Serialize, ConfigFile)]
#[config_file_ext("yml")]
pub struct MyYmlConfig {
    pub dependencies: BTreeMap<String, BTreeSet<String>>,

    #[serde(skip)]
    pub path: String,
}

#[derive(Debug, Default, Clone, Ord, PartialOrd, Eq, PartialEq, Deserialize, Serialize, ConfigFile)]
#[config_file_ext("hcl")]
pub struct MyHclConfig {
    pub dependencies: BTreeMap<String, BTreeSet<String>>,

    #[serde(skip)]
    pub path: String,
}

fn main() {
    // load from file or default
    let mut my_toml = MyTomlConfig::load("target/debug/test.toml", true).unwrap();
    // update
    my_toml.dependencies.insert(String::from("a"), BTreeSet::from([String::from("b"), String::from("c")]));
    // serialize to string (pretty & output error)
    println!("{}\n", my_toml.dumps(true, false));
    // serialize to file (pretty & output error)
    my_toml.dump(true, false);
    // load from file or panic
    println!("{:#?}\n\n", MyTomlConfig::load("target/debug/test.toml", false).unwrap());

    // load from file or default
    let mut my_json = MyJsonConfig::load("target/debug/test.json", true).unwrap();
    // update
    my_json.dependencies.insert(String::from("a"), BTreeSet::from([String::from("b"), String::from("c")]));
    // serialize to string (pretty & output error)
    println!("{}\n", my_toml.dumps(true, false));
    // serialize to file (pretty & output error)
    my_json.dump(true, false);
    // load from file or panic
    println!("{:#?}\n\n", MyJsonConfig::load("target/debug/test.json", false).unwrap());

    // load from file or default
    let mut my_xml = MyXmlConfig::load("target/debug/test.xml", true).unwrap();
    // update
    my_xml.dependencies.insert(String::from("a"), BTreeSet::from([String::from("b"), String::from("c")]));
    // serialize to string (no indent & output error)
    println!("{}\n", my_xml.dumps(false, false));
    // serialize to file (no indent & output error)
    my_xml.dump(false, false);
    // load from file or panic
    println!("{:#?}\n\n", MyXmlConfig::load("target/debug/test.xml", false).unwrap());

    // load from file or default
    let mut my_yml = MyYmlConfig::load("target/debug/test.yml", true).unwrap();
    // update
    my_yml.dependencies.insert(String::from("a"), BTreeSet::from([String::from("b"), String::from("c")]));
    // serialize to string (no indent & output error)
    println!("{}\n", my_yml.dumps(false, false));
    // serialize to file (no indent & output error)
    my_yml.dump(false, false);
    // load from file or panic
    println!("{:#?}\n\n", MyYmlConfig::load("target/debug/test.yml", false).unwrap());

    // load from file or default
    let mut my_hcl = MyHclConfig::load("target/debug/test.hcl", true).unwrap();
    // update
    my_hcl.dependencies.insert(String::from("a"), BTreeSet::from([String::from("b"), String::from("c")]));
    // serialize to string (no indent & output error)
    println!("{}\n", my_hcl.dumps(false, false));
    // serialize to file (no indent & output error)
    my_hcl.dump(false, false);
    // load from file or panic
    println!("{:#?}\n\n", MyHclConfig::load("target/debug/test.hcl", false).unwrap());
}
Commit count: 0

cargo fmt