Crates.io | config_helper |
lib.rs | config_helper |
version | |
source | src |
created_at | 2024-11-24 07:20:28.089723 |
updated_at | 2024-11-29 07:40:03.716966 |
description | A simple file upload and download server in Rust using Actix |
homepage | |
repository | https://github.com/LunaStev/config-helper |
max_upload_size | |
id | 1459001 |
Cargo.toml error: | TOML parse error at line 18, column 1 | 18 | 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` |
size | 0 |
Config Helper
is a simple and flexible library for handling configuration files in various formats, such as TOML, JSON, and YAML. It allows you to easily load and save configuration data to and from these formats and automatically deserializes them into Rust structs.
To use Config Helper
in your project, add it as a dependency in your Cargo.toml
:
[dependencies]
config_helper = "0.1.1"
First, define a struct that matches the configuration format you expect to load.
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug)]
pub struct Config {
pub database_url: String,
pub port: u16,
pub debug: bool,
}
Use the load_config
function to load a configuration file. It automatically detects the file format (TOML, JSON, or YAML) based on the file extension.
use config_helper::{load_config, Config};
fn main() {
let config: Config = load_config("config.toml").expect("Failed to load config");
println!("{:?}", config);
}
Use the save_config
function to save a Rust struct as a configuration file. The file format is determined by the file extension.
use config_helper::{save_config, Config};
fn main() {
let new_config = Config {
database_url: "http://localhost".to_string(),
port: 8080,
debug: true,
};
save_config("new_config.toml", &new_config).expect("Failed to save config");
}
The library returns a Result
type for both loading and saving configurations. Errors may occur due to invalid file format, deserialization issues, or I/O errors. Make sure to handle these errors appropriately in your application.
Feel free to open issues or submit pull requests for any improvements or bug fixes. Contributions are always welcome!
This project is licensed under the MPL-2.0 License - see the LICENSE file for details.