Skip to main content

rh_foundation/
config.rs

1//! Configuration traits and utilities.
2//!
3//! This module provides a common configuration trait that can be
4//! implemented by configuration types across the workspace.
5
6use crate::error::Result;
7use serde::{Deserialize, Serialize};
8use std::fmt;
9
10/// Configuration trait that all configuration types should implement.
11///
12/// This trait provides a common interface for configuration objects,
13/// ensuring they can be serialized/deserialized and validated.
14///
15/// # Example
16/// ```
17/// use rh_foundation::Config;
18/// use serde::{Deserialize, Serialize};
19///
20/// #[derive(Debug, Serialize, Deserialize)]
21/// struct MyConfig {
22///     name: String,
23///     port: u16,
24/// }
25///
26/// impl Config for MyConfig {
27///     fn validate(&self) -> rh_foundation::Result<()> {
28///         if self.port == 0 {
29///             return Err(rh_foundation::FoundationError::InvalidInput(
30///                 "Port cannot be 0".to_string()
31///             ));
32///         }
33///         Ok(())
34///     }
35/// }
36/// ```
37pub trait Config: for<'de> Deserialize<'de> + Serialize + fmt::Debug {
38    /// Validate the configuration.
39    ///
40    /// Returns `Ok(())` if the configuration is valid, or an error
41    /// describing what is invalid.
42    fn validate(&self) -> Result<()> {
43        Ok(())
44    }
45}