mirror of
https://github.com/kristoferssolo/filecaster.git
synced 2025-10-21 19:00:34 +00:00
fix: clippy needless doctest main warning
This commit is contained in:
parent
9d365a9593
commit
89732ff8e2
53
README.md
53
README.md
@ -5,7 +5,7 @@ Procedural macro to derive configuration from files, with optional merging capab
|
|||||||
## Features
|
## Features
|
||||||
|
|
||||||
- **Derive Configuration:** Easily load configuration from files into your Rust structs.
|
- **Derive Configuration:** Easily load configuration from files into your Rust structs.
|
||||||
- **Default Values:** Specify default values for struct fields using the `#[default = "..."]` attribute.
|
- **Default Values:** Specify default values for struct fields using the `#[from_file(default = "...")]` attribute.
|
||||||
- **Optional Merging:** When the `merge` feature is enabled, allows merging multiple configuration sources.
|
- **Optional Merging:** When the `merge` feature is enabled, allows merging multiple configuration sources.
|
||||||
|
|
||||||
## Usage
|
## Usage
|
||||||
@ -18,29 +18,46 @@ filecaster = "0.2"
|
|||||||
```rust
|
```rust
|
||||||
use filecaster::FromFile;
|
use filecaster::FromFile;
|
||||||
|
|
||||||
#[derive(Debug, Clone, FromFile)]
|
#[derive(Debug, Clone, PartialEq, FromFile)]
|
||||||
pub struct MyConfig {
|
struct AppConfig {
|
||||||
#[from_file(default = "localhost")]
|
/// If the user does not specify a host, use `"127.0.0.1"`.
|
||||||
pub host: String,
|
#[from_file(default = "127.0.0.1")]
|
||||||
|
host: String,
|
||||||
|
|
||||||
|
/// Port number; defaults to `8080`.
|
||||||
#[from_file(default = 8080)]
|
#[from_file(default = 8080)]
|
||||||
pub port: u16,
|
port: u16,
|
||||||
#[from_file(default = false)]
|
|
||||||
pub enabled: bool,
|
/// If not set, use `false`. Requires `bool: Default`.
|
||||||
|
auto_reload: bool,
|
||||||
}
|
}
|
||||||
|
|
||||||
fn main() {
|
fn main() {
|
||||||
// Simulate loading from a file (e.g., JSON, YAML, TOML)
|
// Simulate file content (e.g., from a JSON file)
|
||||||
let file_content = r#"
|
let file_content = r#"{ "host": "localhost", "port": 3000 }"#;
|
||||||
{
|
|
||||||
"host": "localhost"
|
|
||||||
}
|
|
||||||
"#;
|
|
||||||
|
|
||||||
let config_from_file: MyConfig = serde_json::from_str(file_content).unwrap();
|
// The `AppConfigFile` struct is automatically generated by `#[derive(FromFile)]`.
|
||||||
let config = MyConfig::from_file(Some(config_from_file));
|
// It has all fields as `Option<T>`.
|
||||||
|
let partial_config: AppConfigFile = serde_json::from_str(file_content).unwrap();
|
||||||
|
let partial_config2 = partial_config.clone();
|
||||||
|
|
||||||
println!("Config: {:?}", config);
|
// Use the generated `from_file` method to get the final config.
|
||||||
// Expected output: Config { host: "localhost", port: 8080, enabled: false }
|
// 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); // `Default::default()` for bool 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);
|
||||||
}
|
}
|
||||||
```
|
```
|
||||||
|
|
||||||
|
|||||||
@ -34,39 +34,38 @@
|
|||||||
//!
|
//!
|
||||||
//! ```rust
|
//! ```rust
|
||||||
//! use filecaster::FromFile;
|
//! use filecaster::FromFile;
|
||||||
//! use serde::{Deserialize, Serialize};
|
|
||||||
//!
|
//!
|
||||||
//! #[derive(Debug, Clone, PartialEq, FromFile, Serialize, Deserialize)]
|
//! #[derive(Debug, Clone, PartialEq, FromFile)]
|
||||||
//! struct AppConfig {
|
//! struct AppConfig {
|
||||||
//! /// If the user does not specify a host, use `"127.0.0.1"`.
|
//! /// If the user does not specify a host, use `"127.0.0.1"`.
|
||||||
//! #[from_file(default = "127.0.0.1")]
|
//! #[from_file(default = "127.0.0.1")]
|
||||||
//! host: String,
|
//! host: String,
|
||||||
//!
|
//!
|
||||||
//! /// Number of worker threads; defaults to `4`.
|
//! /// Port number; defaults to `8080`.
|
||||||
//! #[from_file(default = 4)]
|
//! #[from_file(default = 8080)]
|
||||||
//! workers: usize,
|
//! port: u16,
|
||||||
//!
|
//!
|
||||||
//! /// If not set, use `false`. Requires `bool: Default`.
|
//! /// If not set, use `false`. Requires `bool: Default`.
|
||||||
//! auto_reload: bool,
|
//! auto_reload: bool,
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn example() {
|
||||||
//! // Simulate file content (e.g., from a JSON file)
|
//! // Simulate file content (e.g., from a JSON file)
|
||||||
//! let file_content = r#"{
|
//! let file_content = r#"{ "host": "localhost", "port": 3000 }"#;
|
||||||
//! "host": "localhost",
|
|
||||||
//! "workers": 8
|
|
||||||
//! }"#;
|
|
||||||
//!
|
//!
|
||||||
//! // The `AppConfigFile` struct is automatically generated by `#[derive(FromFile)]`.
|
//! // The `AppConfigFile` struct is automatically generated by `#[derive(FromFile)]`.
|
||||||
//! // It has all fields as `Option<T>`.
|
//! // It has all fields as `Option<T>`.
|
||||||
//! let partial_config: AppConfigFile = serde_json::from_str(file_content).unwrap();
|
//! 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.
|
//! // Use the generated `from_file` method to get the final config.
|
||||||
//! // Default values are applied for missing fields.
|
//! // Default values are applied for missing fields.
|
||||||
//! let config = AppConfig::from_file(Some(partial_config));
|
//! let config = AppConfig::from_file(Some(partial_config));
|
||||||
|
//! // or
|
||||||
|
//! let config: AppConfig = partial_config2.into();
|
||||||
//!
|
//!
|
||||||
//! assert_eq!(config.host, "localhost");
|
//! assert_eq!(config.host, "localhost");
|
||||||
//! assert_eq!(config.workers, 8);
|
//! assert_eq!(config.port, 3000);
|
||||||
//! assert_eq!(config.auto_reload, false); // `Default::default()` for bool is `false`
|
//! assert_eq!(config.auto_reload, false); // `Default::default()` for bool is `false`
|
||||||
//!
|
//!
|
||||||
//! println!("Final Config: {:#?}", config);
|
//! println!("Final Config: {:#?}", config);
|
||||||
@ -74,7 +73,7 @@
|
|||||||
//! // Example with no file content (all defaults)
|
//! // Example with no file content (all defaults)
|
||||||
//! let default_config = AppConfig::from_file(None);
|
//! let default_config = AppConfig::from_file(None);
|
||||||
//! assert_eq!(default_config.host, "127.0.0.1");
|
//! assert_eq!(default_config.host, "127.0.0.1");
|
||||||
//! assert_eq!(default_config.workers, 4);
|
//! assert_eq!(default_config.port, 8080);
|
||||||
//! assert_eq!(default_config.auto_reload, false);
|
//! assert_eq!(default_config.auto_reload, false);
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
|
|||||||
@ -40,7 +40,7 @@
|
|||||||
//! struct AppConfig {
|
//! struct AppConfig {
|
||||||
//! host: String,
|
//! host: String,
|
||||||
//! port: u16,
|
//! port: u16,
|
||||||
//! enabled: bool,
|
//! auto_reload: bool,
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! // The `Shadow` type is automatically generated by `filecaster-derive`
|
//! // The `Shadow` type is automatically generated by `filecaster-derive`
|
||||||
@ -49,12 +49,12 @@
|
|||||||
//! struct AppConfigFile {
|
//! struct AppConfigFile {
|
||||||
//! host: Option<String>,
|
//! host: Option<String>,
|
||||||
//! port: Option<u16>,
|
//! port: Option<u16>,
|
||||||
//! enabled: Option<bool>,
|
//! auto_reload: Option<bool>,
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! // The `FromFile` implementation is also automatically generated.
|
//! // The `FromFile` implementation is also automatically generated.
|
||||||
//! // For demonstration, here's a simplified manual implementation:
|
//! // For demonstration, here's a simplified manual implementation:
|
||||||
//! impl FromFile for AppConfig {
|
//! impl FromFile for AppConfig {
|
||||||
//! type Shadow = AppConfigFile;
|
//! type Shadow = AppConfigFile;
|
||||||
//!
|
//!
|
||||||
//! fn from_file(file: Option<Self::Shadow>) -> Self {
|
//! fn from_file(file: Option<Self::Shadow>) -> Self {
|
||||||
@ -62,12 +62,12 @@
|
|||||||
//! AppConfig {
|
//! AppConfig {
|
||||||
//! host: file.host.unwrap_or_else(|| "127.0.0.1".to_string()),
|
//! host: file.host.unwrap_or_else(|| "127.0.0.1".to_string()),
|
||||||
//! port: file.port.unwrap_or(8080),
|
//! port: file.port.unwrap_or(8080),
|
||||||
//! enabled: file.enabled.unwrap_or(true),
|
//! auto_reload: file.auto_reload.unwrap_or(true),
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//! }
|
//! }
|
||||||
//!
|
//!
|
||||||
//! fn main() {
|
//! fn example() {
|
||||||
//! // Simulate deserializing from a file
|
//! // Simulate deserializing from a file
|
||||||
//! let file_content = r#"{ "host": "localhost", "port": 3000 }"#;
|
//! let file_content = r#"{ "host": "localhost", "port": 3000 }"#;
|
||||||
//! let partial_config: AppConfigFile = serde_json::from_str(file_content).unwrap();
|
//! let partial_config: AppConfigFile = serde_json::from_str(file_content).unwrap();
|
||||||
@ -77,9 +77,15 @@
|
|||||||
//!
|
//!
|
||||||
//! assert_eq!(config.host, "localhost");
|
//! assert_eq!(config.host, "localhost");
|
||||||
//! assert_eq!(config.port, 3000);
|
//! assert_eq!(config.port, 3000);
|
||||||
//! assert_eq!(config.enabled, true); // Default value applied
|
//! assert_eq!(config.auto_reload, false); // `Default::default()` for bool is `false`
|
||||||
//!
|
//!
|
||||||
//! println!("Final Config: {:?}", config);
|
//! 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);
|
||||||
//! }
|
//! }
|
||||||
//! ```
|
//! ```
|
||||||
//!
|
//!
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user