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