| Crates.io | oxidite-config |
| lib.rs | oxidite-config |
| version | 2.0.1 |
| created_at | 2025-12-07 12:35:06.516998+00 |
| updated_at | 2026-01-24 18:46:47.298222+00 |
| description | Configuration management for the Oxidite v2 web framework |
| homepage | https://github.com/meshackbahati/rust-oxidite |
| repository | https://github.com/meshackbahati/rust-oxidite |
| max_upload_size | |
| id | 1971512 |
| size | 27,033 |
Configuration management for the Oxidite web framework.
oxidite-config provides flexible configuration management for Oxidite applications. It supports multiple configuration sources including environment variables, configuration files, and command-line arguments. The crate offers type-safe configuration loading with automatic deserialization from various formats.
Add this to your Cargo.toml:
[dependencies]
oxidite-config = "2.0"
use oxidite_config::{Config, ConfigSource};
#[derive(serde::Deserialize, Clone)]
struct AppConfig {
server_port: u16,
database_url: String,
debug_mode: bool,
}
#[tokio::main]
async fn main() -> Result<(), Box<dyn std::error::Error>> {
let mut config = Config::new();
// Load from environment variables
config.load_source(ConfigSource::Environment);
// Load from config file
config.load_source(ConfigSource::File("config/app.json".to_string()));
// Get typed configuration
let app_config: AppConfig = config.get("app")?;
println!("Server will run on port: {}", app_config.server_port);
Ok(())
}
use oxidite_config::{Config, ConfigBuilder};
#[derive(serde::Deserialize, Clone)]
struct ServerConfig {
host: String,
port: u16,
ssl_enabled: bool,
}
impl Default for ServerConfig {
fn default() -> Self {
Self {
host: "127.0.0.1".to_string(),
port: 3000,
ssl_enabled: false,
}
}
}
async fn load_server_config() -> Result<ServerConfig, Box<dyn std::error::Error>> {
let config = ConfigBuilder::new()
.add_default_source() // Built-in defaults
.add_env_source() // Environment variables
.add_file_source("config/server.toml") // Config file
.build()
.await?;
Ok(config.get_with_default("server", ServerConfig::default())?)
}
use oxidite_config::Config;
// Define environment variable mappings
std::env::set_var("DATABASE_URL", "postgres://localhost/myapp");
std::env::set_var("PORT", "8080");
std::env::set_var("DEBUG", "true");
let config = Config::from_env()
.prefix("MYAPP_") // Use MYAPP_DATABASE_URL, MYAPP_PORT, etc.
.separator("_")
.build()?;
let database_url: String = config.get("database_url")?;
let port: u16 = config.get("port")?;
let debug: bool = config.get("debug")?;
The crate supports multiple configuration formats:
use oxidite_config::{Config, ValidationError};
let config = Config::builder()
.add_source(ConfigSource::File("config/app.json".to_string()))
.validator(|config| {
let port: u16 = config.get("server.port")?;
if port == 0 || port > 65535 {
return Err(ValidationError::new("server.port must be between 1 and 65535"));
}
Ok(())
})
.build()?;
use oxidite_config::Config;
// Enable hot reloading
let config = Config::builder()
.enable_hot_reload(true)
.add_source(ConfigSource::File("config/app.toml".to_string()))
.build()?;
// Configuration will automatically reload when the file changes
let updated_value = config.get::<String>("some_setting")?;
MIT