#serialize-deserialize #json-toml #toml #serialization #hcl #json #serde

config_file_macros

Add file configuration support for the struct to enable seamless serialization and deserialization of its fields, improving data management and persistence

7 stable releases

new 2024.12.11 Dec 11, 2024
2024.11.22 Nov 25, 2024

#567 in Encoding

Download history 294/week @ 2024-11-11 280/week @ 2024-11-18 217/week @ 2024-11-25 8/week @ 2024-12-02 195/week @ 2024-12-09

716 downloads per month
Used in 2 crates (via config_file_types)

LGPL-3.0+

11KB
95 lines

Wrap struct to file configuration

Docs Crates.io Crates.io

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

No runtime deps