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 access and change callback interfaces.
12//!
13//! # Author
14//!
15//! Haixing Hu
16
17use super::Config;
18
19/// Configurable trait
20///
21/// Types that implement this trait can be configured using `Config`.
22///
23/// # Examples
24///
25/// ```rust,ignore
26/// use qubit_config::{Config, Configurable};
27///
28/// struct Server { config: Config }
29///
30/// impl Configurable for Server {
31///     fn config(&self) -> &Config { &self.config }
32///     fn config_mut(&mut self) -> &mut Config { &mut self.config }
33///     fn set_config(&mut self, config: Config) { self.config = config; self.on_config_changed(); }
34/// }
35/// ```
36///
37/// ```rust,ignore
38/// use qubit_config::{ConfigResult, ConfigError};
39/// ```
40///
41/// # Author
42///
43/// Haixing Hu
44pub trait Configurable {
45    /// Gets a reference to the configuration
46    ///
47    /// # Returns
48    ///
49    /// Returns an immutable reference to the configuration
50    ///
51    /// # Author
52    ///
53    /// Haixing Hu
54    fn config(&self) -> &Config;
55
56    /// Gets a mutable reference to the configuration
57    ///
58    /// # Returns
59    ///
60    /// Returns a mutable reference to the configuration
61    ///
62    /// # Author
63    ///
64    /// Haixing Hu
65    fn config_mut(&mut self) -> &mut Config;
66
67    /// Sets the configuration
68    ///
69    /// # Parameters
70    ///
71    /// * `config` - The new configuration
72    ///
73    /// # Author
74    ///
75    /// Haixing Hu
76    fn set_config(&mut self, config: Config);
77
78    /// Callback after configuration changes
79    ///
80    /// This method is called when the configuration is modified. Subclasses can override this method to perform additional operations.
81    ///
82    /// # Author
83    ///
84    /// Haixing Hu
85    fn on_config_changed(&mut self) {
86        // Default implementation is empty
87    }
88}