4 releases

new 0.4.0 Dec 1, 2024
0.3.2 Jul 28, 2024
0.3.1 Jul 20, 2024
0.3.0 Jul 19, 2024

#597 in Configuration

Download history 3/week @ 2024-08-24 4/week @ 2024-08-31 8/week @ 2024-09-14 23/week @ 2024-09-21 4/week @ 2024-09-28 4/week @ 2024-10-05 7/week @ 2024-10-12 1/week @ 2024-10-19 9/week @ 2024-11-02 4/week @ 2024-11-16 1/week @ 2024-11-23 153/week @ 2024-11-30

163 downloads per month
Used in git-simple-encrypt

BSD-2-Clause

24KB
406 lines

config-file2

Extremely easy to load and store your configuration file!

Usage

  1. Add dependency:
    cargo add config-file2
    
  2. Enable which format you want to use in features.
    • all
    • toml (enabled by default)
    • json
    • xml
    • yaml
    • ron

Here's an example of how to use it with json and yaml format:

[dependencies]
config-file2 = { version = "0.4", features = ["json", "yaml"] }

Examples

use config_file2::{LoadConfigFile, StoreConfigFile};
use serde::{Serialize, Deserialize};

#[derive(Serialize, Deserialize)]
struct Config {
    host: String,
}

// store
Config { host: "example.com".into() }.store("/tmp/myconfig.toml").unwrap();

// load
let config = Config::load("/tmp/myconfig.toml").unwrap().unwrap();
assert_eq!(config.host.as_str(), "example.com");

Another way to store a struct into a configuration file:

use config_file2::Storable;
use serde::{Serialize, Deserialize};
use std::path::{Path, PathBuf};

#[derive(Serialize)]
struct TestStorable {
    path: PathBuf,
}

impl Storable for TestStorable {
    fn path(&self) -> &Path {
        &self.path
    }
}

TestStorable { path: PathBuf::from("/tmp/myconfig.toml") }.store().unwrap();

more

fn load_with_specific_format(path: impl AsRef<Path>, config_type: ConfigFormat) -> Result<Self>;
fn load_or_default(path: impl AsRef<Path>) -> Result<Self>;
fn store_with_specific_format(self, path: impl AsRef<Path>, config_type: ConfigFormat) -> Result<()>;
fn store_without_overwrite(self, path: impl AsRef<Path>) -> Result<()>;

Dependencies

~0.4–1.9MB
~39K SLoC