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