config-file2

Crates.ioconfig-file2
lib.rsconfig-file2
version
sourcesrc
created_at2024-07-19 17:02:02.913554
updated_at2024-12-01 01:22:24.211005
descriptionExtremely easy to read and write configuration file
homepage
repositoryhttps://github.com/lxl66566/config-file
max_upload_size
id1308883
Cargo.toml error:TOML parse error at line 21, column 1 | 21 | autolib = false | ^^^^^^^ unknown field `autolib`, expected one of `name`, `version`, `edition`, `authors`, `description`, `readme`, `license`, `repository`, `homepage`, `documentation`, `build`, `resolver`, `links`, `default-run`, `default_dash_run`, `rust-version`, `rust_dash_version`, `rust_version`, `license-file`, `license_dash_file`, `license_file`, `licenseFile`, `license_capital_file`, `forced-target`, `forced_dash_target`, `autobins`, `autotests`, `autoexamples`, `autobenches`, `publish`, `metadata`, `keywords`, `categories`, `exclude`, `include`
size0
Absolutex (lxl66566)

documentation

https://docs.rs/config-file2

README

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<()>;
Commit count: 39

cargo fmt