Skip to main content

qubit_config/
configurable.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026 Haixing Hu.
4 *
5 *    SPDX-License-Identifier: Apache-2.0
6 *
7 *    Licensed under the Apache License, Version 2.0.
8 *
9 ******************************************************************************/
10//! # Configurable Interface
11//!
12//! Provides the `Configurable` trait for types to have unified configuration
13//! access and change callback interfaces.
14//!
15
16use super::Config;
17
18/// Configurable trait
19///
20/// Types that implement this trait can be configured using `Config`.
21///
22/// # Examples
23///
24/// ```rust
25/// use qubit_config::{Config, Configurable};
26///
27/// struct Server { config: Config }
28///
29/// impl Configurable for Server {
30///     fn config(&self) -> &Config {
31///         &self.config
32///     }
33///     fn config_mut(&mut self) -> &mut Config {
34///         &mut self.config
35///     }
36///     fn set_config(&mut self, config: Config) {
37///         self.config = config;
38///         self.on_config_changed();
39///     }
40/// }
41/// ```
42///
43/// ```rust
44/// use qubit_config::{ConfigResult, ConfigError};
45/// ```
46///
47pub trait Configurable {
48    /// Gets a reference to the configuration
49    ///
50    /// # Returns
51    ///
52    /// Returns an immutable reference to the configuration
53    ///
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    fn config_mut(&mut self) -> &mut Config;
63
64    /// Sets the configuration
65    ///
66    /// # Parameters
67    ///
68    /// * `config` - The new configuration
69    ///
70    /// # Returns
71    ///
72    /// Nothing.
73    ///
74    fn set_config(&mut self, config: Config);
75
76    /// Callback after configuration changes
77    ///
78    /// This method is called when the configuration is modified. Implementors
79    /// may override it to run side effects after [`Self::set_config`].
80    ///
81    /// # Returns
82    ///
83    /// Nothing.
84    ///
85    #[inline]
86    fn on_config_changed(&mut self) {
87        // Default implementation is empty
88    }
89}