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