| Crates.io | toolcraft-config |
| lib.rs | toolcraft-config |
| version | 0.2.0 |
| created_at | 2025-08-06 05:58:12.905747+00 |
| updated_at | 2025-08-06 05:58:12.905747+00 |
| description | Toolcraft config module |
| homepage | |
| repository | https://github.com/code-serenade/toolcraft.git |
| max_upload_size | |
| id | 1783450 |
| size | 22,941 |
A simple and flexible configuration management library for Rust applications.
config crateAdd this to your Cargo.toml:
[dependencies]
toolcraft-config = "*"
Check the crates.io page for the latest version.
use toolcraft_config::load_settings;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct AppConfig {
server: ServerConfig,
database: DatabaseConfig,
}
#[derive(Debug, Deserialize)]
struct ServerConfig {
host: String,
port: u16,
}
#[derive(Debug, Deserialize)]
struct DatabaseConfig {
url: String,
max_connections: u32,
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Load configuration from a file
let config: AppConfig = load_settings("config.toml")?;
println!("Server: {}:{}", config.server.host, config.server.port);
println!("Database: {}", config.database.url);
Ok(())
}
Create a config.toml file:
[server]
host = "localhost"
port = 8080
[database]
url = "postgresql://localhost/myapp"
max_connections = 10
The library supports all formats provided by the config crate:
The format is automatically detected based on the file extension.
use toolcraft_config::load_settings;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct MyConfig {
#[serde(default = "default_timeout")]
timeout: u64,
#[serde(rename = "max-retries")]
max_retries: u32,
features: Features,
}
#[derive(Debug, Deserialize)]
struct Features {
#[serde(default)]
enable_cache: bool,
#[serde(default)]
enable_logging: bool,
}
fn default_timeout() -> u64 {
30
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config: MyConfig = load_settings("my_app.toml")?;
println!("Timeout: {} seconds", config.timeout);
Ok(())
}
use toolcraft_config::{load_settings, Result};
fn load_config() -> Result<AppConfig> {
load_settings("config.toml")
}
fn main() {
match load_config() {
Ok(config) => {
println!("Configuration loaded successfully");
}
Err(e) => {
eprintln!("Failed to load configuration: {}", e);
std::process::exit(1);
}
}
}
While the current API supports single file loading, you can easily extend it:
use toolcraft_config::load_settings;
fn load_with_defaults<T>() -> Result<T, Box<dyn std::error::Error>>
where
T: serde::de::DeserializeOwned + Default,
{
match load_settings("config.toml") {
Ok(config) => Ok(config),
Err(_) => {
// Fall back to defaults if config file doesn't exist
Ok(T::default())
}
}
}
load_settings<T>(config_path: &str) -> Result<T> - Load configuration from a file
T: The type to deserialize into (must implement DeserializeOwned)config_path: Path to the configuration fileResult<T, Error> with the parsed configuration or an errorResult<T> - Type alias for std::result::Result<T, Error>Error - Custom error type for configuration loading failures#[serde(default)] for optional configuration valuesuse toolcraft_config::load_settings;
use serde::Deserialize;
#[derive(Debug, Deserialize)]
struct WebAppConfig {
server: ServerConfig,
database: DatabaseConfig,
redis: RedisConfig,
jwt: JwtConfig,
}
#[derive(Debug, Deserialize)]
struct ServerConfig {
host: String,
port: u16,
workers: Option<usize>,
}
#[derive(Debug, Deserialize)]
struct DatabaseConfig {
url: String,
pool_size: u32,
timeout: u64,
}
#[derive(Debug, Deserialize)]
struct RedisConfig {
url: String,
#[serde(default = "default_redis_pool")]
pool_size: u32,
}
#[derive(Debug, Deserialize)]
struct JwtConfig {
secret: String,
expiration: u64,
}
fn default_redis_pool() -> u32 {
10
}
fn main() -> Result<(), Box<dyn std::error::Error>> {
let config: WebAppConfig = load_settings("webapp.toml")?;
// Use the configuration to initialize your application
println!("Starting server on {}:{}", config.server.host, config.server.port);
Ok(())
}
This project is licensed under the MIT License - see the LICENSE file for details.
Contributions are welcome! Please feel free to submit a Pull Request.