Skip to main content

qubit_config/
configured.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # Base Implementation of Configurable Objects
10//!
11//! Provides a base structure that implements the `Configurable` trait.
12//!
13//! # Author
14//!
15//! Haixing Hu
16
17use super::{Config, Configurable};
18
19/// Base implementation of configurable objects
20///
21/// This is a base structure that implements the `Configurable` trait and can be used as a base for other structures that need configuration.
22///
23/// # Features
24///
25/// - Automatically implements the `Configurable` trait
26/// - Provides configuration change callback mechanism
27/// - Can be inherited and extended
28///
29/// # Examples
30///
31/// ```rust,ignore
32/// use qubit_config::{Config, Configured};
33///
34/// let mut configured = Configured::new();
35/// configured.config_mut().set("port", 8080)?;
36/// let port: i32 = configured.config().get("port")?;
37/// assert_eq!(port, 8080);
38/// ```
39///
40/// ```rust,ignore
41/// // Or compose it into other structures
42/// struct Server {
43///     configured: Configured,
44///     // Other fields...
45/// }
46///
47/// impl Server {
48///     fn new() -> Self {
49///         Self {
50///             configured: Configured::new(),
51///         }
52///     }
53///
54///     fn config(&self) -> &Config {
55///         self.configured.config()
56///     }
57///
58///     fn config_mut(&mut self) -> &mut Config {
59///         self.configured.config_mut()
60///     }
61/// }
62/// ```
63///
64/// # Author
65///
66/// Haixing Hu
67///
68#[derive(Debug, Clone, PartialEq)]
69pub struct Configured {
70    /// Configuration object
71    config: Config,
72}
73
74impl Configured {
75    /// Creates a new configurable object
76    ///
77    /// # Returns
78    ///
79    /// Returns a new configurable object instance
80    ///
81    /// # Examples
82    ///
83    /// ```rust,ignore
84    /// use common_rs::util::config::Configured;
85    ///
86    /// let configured = Configured::new();
87    /// assert!(configured.config().is_empty());
88    /// ```
89    pub fn new() -> Self {
90        Self {
91            config: Config::new(),
92        }
93    }
94
95    /// Creates a configurable object with the specified configuration
96    ///
97    /// # Parameters
98    ///
99    /// * `config` - Configuration object
100    ///
101    /// # Returns
102    ///
103    /// Returns a new configurable object instance
104    ///
105    /// # Examples
106    ///
107    /// ```rust,ignore
108    /// use common_rs::util::config::{Config, Configured};
109    ///
110    /// let mut configured = Configured::with_config(Config::new());
111    /// assert!(configured.config().is_empty());
112    /// ```
113    pub fn with_config(config: Config) -> Self {
114        Self { config }
115    }
116}
117
118impl Configurable for Configured {
119    fn config(&self) -> &Config {
120        &self.config
121    }
122
123    fn config_mut(&mut self) -> &mut Config {
124        &mut self.config
125    }
126
127    fn set_config(&mut self, config: Config) {
128        self.config = config;
129        self.on_config_changed();
130    }
131
132    fn on_config_changed(&mut self) {
133        // Default implementation is empty, subclasses can override
134    }
135}
136
137impl Default for Configured {
138    fn default() -> Self {
139        Self::new()
140    }
141}