| Crates.io | config-file2 |
| lib.rs | config-file2 |
| version | 0.4.1 |
| created_at | 2024-07-19 17:02:02.913554+00 |
| updated_at | 2025-04-16 12:59:47.100769+00 |
| description | Extremely easy to read and write configuration file |
| homepage | |
| repository | https://github.com/lxl66566/config-file |
| max_upload_size | |
| id | 1308883 |
| size | 37,576 |
Extremely easy to load and store your configuration file!
cargo add config-file2
alltoml (enabled by default)jsonxmlyamlronHere's an example of how to use it with json and yaml format:
[dependencies]
config-file2 = { version = "0.4", features = ["json", "yaml"] }
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) -> impl AsRef<Path> {
&self.path
}
}
TestStorable { path: PathBuf::from("/tmp/myconfig.toml") }.store().unwrap();
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<()>;