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

macro config_file_derives

Automatically generate file configuration functions for the specified struct

6 stable releases

2024.11.25 Nov 25, 2024
2024.11.21 Nov 21, 2024
2024.11.19 Nov 19, 2024
2024.11.16 Nov 15, 2024
2024.11.14 Nov 14, 2024

#17 in #hcl

Download history 283/week @ 2024-11-11 272/week @ 2024-11-18 210/week @ 2024-11-25 6/week @ 2024-12-02 25/week @ 2024-12-09

528 downloads per month
Used in asc_bin

LGPL-3.0+

10KB
71 lines

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

Dependencies

~220–660KB
~16K SLoC