Skip to main content

qubit_config/
configurable.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # Configurable Interface
10//!
11//! Provides the `Configurable` trait for types to have unified configuration
12//! access and change callback interfaces.
13//!
14//! # Author
15//!
16//! Haixing Hu
17
18use super::Config;
19
20/// Configurable trait
21///
22/// Types that implement this trait can be configured using `Config`.
23///
24/// # Examples
25///
26/// ```rust
27/// use qubit_config::{Config, Configurable};
28///
29/// struct Server { config: Config }
30///
31/// impl Configurable for Server {
32///     fn config(&self) -> &Config {
33///         &self.config
34///     }
35///     fn config_mut(&mut self) -> &mut Config {
36///         &mut self.config
37///     }
38///     fn set_config(&mut self, config: Config) {
39///         self.config = config;
40///         self.on_config_changed();
41///     }
42/// }
43/// ```
44///
45/// ```rust
46/// use qubit_config::{ConfigResult, ConfigError};
47/// ```
48///
49/// # Author
50///
51/// Haixing Hu
52pub trait Configurable {
53    /// Gets a reference to the configuration
54    ///
55    /// # Returns
56    ///
57    /// Returns an immutable reference to the configuration
58    ///
59    /// # Author
60    ///
61    /// Haixing Hu
62    fn config(&self) -> &Config;
63
64    /// Gets a mutable reference to the configuration
65    ///
66    /// # Returns
67    ///
68    /// Returns a mutable reference to the configuration
69    ///
70    /// # Author
71    ///
72    /// Haixing Hu
73    fn config_mut(&mut self) -> &mut Config;
74
75    /// Sets the configuration
76    ///
77    /// # Parameters
78    ///
79    /// * `config` - The new configuration
80    ///
81    /// # Returns
82    ///
83    /// Nothing.
84    ///
85    /// # Author
86    ///
87    /// Haixing Hu
88    fn set_config(&mut self, config: Config);
89
90    /// Callback after configuration changes
91    ///
92    /// This method is called when the configuration is modified. Implementors
93    /// may override it to run side effects after [`Self::set_config`].
94    ///
95    /// # Returns
96    ///
97    /// Nothing.
98    ///
99    /// # Author
100    ///
101    /// Haixing Hu
102    #[inline]
103    fn on_config_changed(&mut self) {
104        // Default implementation is empty
105    }
106}