| Crates.io | filecaster |
| lib.rs | filecaster |
| version | 0.2.3 |
| created_at | 2025-07-14 19:06:18.608659+00 |
| updated_at | 2025-07-15 16:52:52.426471+00 |
| description | Procedural macro to derive configuration from files, with optional merging capabilities. |
| homepage | https://github.com/kristoferssolo/filecaster |
| repository | https://github.com/kristoferssolo/filecaster |
| max_upload_size | |
| id | 1752192 |
| size | 29,638 |
Procedural macro to derive configuration from files, with optional merging capabilities.
#[from_file(default = "...")] attribute.merge feature is enabled, allows merging multiple configuration sources.[dependencies]
filecaster = "0.2"
use filecaster::FromFile;
#[derive(Debug, Clone, PartialEq, FromFile)]
struct AppConfig {
/// If the user does not specify a host, use `"127.0.0.1"`.
#[from_file(default = "127.0.0.1")]
host: String,
/// Port number; defaults to `8080`.
#[from_file(default = 8080)]
port: u16,
/// If not set, use `false`. Requires `bool: Default`.
auto_reload: bool,
}
fn main() {
// Simulate file content (e.g., from a JSON file)
let file_content = r#"{ "host": "localhost", "port": 3000 }"#;
// The `AppConfigFile` struct is automatically generated by `#[derive(FromFile)]`.
// It has all fields as `Option<T>`.
let partial_config: AppConfigFile = serde_json::from_str(file_content).unwrap();
let partial_config2 = partial_config.clone();
// Use the generated `from_file` method to get the final config.
// Default values are applied for missing fields.
let config = AppConfig::from_file(Some(partial_config));
// or
let config: AppConfig = partial_config2.into();
assert_eq!(config.host, "localhost");
assert_eq!(config.port, 3000);
assert_eq!(config.auto_reload, false); // `bool::default()` is `false`
println!("Final Config: {:#?}", config);
// Example with no file content (all defaults)
let default_config = AppConfig::from_file(None);
assert_eq!(default_config.host, "127.0.0.1");
assert_eq!(default_config.port, 8080);
assert_eq!(default_config.auto_reload, false);
}
Use cargo run --example <example_name> to execute a specific example. For example:
cargo run --example simple
cargo run --example nested
Full documentation is available at docs.rs.
Contributions are welcome! Please feel free to submit a Pull Request.
This project is dual-licensed under either:
at your option.