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