qubit_config/property.rs
1/*******************************************************************************
2 *
3 * Copyright (c) 2025 - 2026.
4 * Haixing Hu, Qubit Co. Ltd.
5 *
6 * All rights reserved.
7 *
8 ******************************************************************************/
9//! # Configuration Property
10//!
11//! Defines the property structure for configuration items, including name,
12//! value, description, and other information.
13//!
14//! # Author
15//!
16//! Haixing Hu
17
18use serde::{Deserialize, Serialize};
19use std::ops::{Deref, DerefMut};
20
21use qubit_common::DataType;
22use qubit_value::MultiValues;
23
24/// Configuration Property
25///
26/// Represents a configuration item: name, value, description, and whether it is
27/// final.
28///
29/// # Features
30///
31/// - Supports multi-value configuration
32/// - Supports description information
33/// - Supports final value marking (final properties cannot be overridden)
34/// - Supports serialization and deserialization
35///
36/// # Examples
37///
38/// ```rust,ignore
39/// use qubit_config::Property;
40///
41/// let mut port = Property::new("port");
42/// port.set(8080).unwrap(); // Generic method, type auto-inferred
43/// port.set_description(Some("Server port".to_string()));
44/// assert_eq!(port.name(), "port");
45/// assert_eq!(port.count(), 1);
46///
47/// let mut code = Property::new("code");
48/// code.set(42u8).unwrap(); // Generic set, inferred as u8
49/// code.add(1u8).unwrap();
50/// assert_eq!(code.count(), 2);
51/// ```
52///
53/// # Author
54///
55/// Haixing Hu
56///
57#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
58pub struct Property {
59 /// Property name
60 name: String,
61 /// Property value
62 value: MultiValues,
63 /// Property description
64 description: Option<String>,
65 /// Whether this is a final value (cannot be overridden)
66 is_final: bool,
67}
68
69impl Property {
70 /// Creates a new property
71 ///
72 /// Creates an empty property with an initial value of an empty i32 list.
73 ///
74 /// # Parameters
75 ///
76 /// * `name` - Property name
77 ///
78 /// # Returns
79 ///
80 /// Returns a new property instance
81 ///
82 /// # Examples
83 ///
84 /// ```rust,ignore
85 /// use common_rs::util::config::Property;
86 ///
87 /// let prop = Property::new("server.port");
88 /// assert_eq!(prop.name(), "server.port");
89 /// assert!(prop.is_empty());
90 /// ```
91 #[inline]
92 pub fn new(name: impl Into<String>) -> Self {
93 Self {
94 name: name.into(),
95 value: MultiValues::Empty(DataType::Int32),
96 description: None,
97 is_final: false,
98 }
99 }
100
101 /// Creates a property with a value
102 ///
103 /// # Parameters
104 ///
105 /// * `name` - Property name
106 /// * `value` - Property value
107 ///
108 /// # Returns
109 ///
110 /// Returns a new property instance
111 ///
112 /// # Examples
113 ///
114 /// ```rust,ignore
115 /// use qubit_config::Property;
116 /// use qubit_value::MultiValues;
117 ///
118 /// let prop = Property::with_value("port", MultiValues::Int32(vec![8080]));
119 /// assert_eq!(prop.name(), "port");
120 /// assert_eq!(prop.count(), 1);
121 /// ```
122 #[inline]
123 pub fn with_value(name: impl Into<String>, value: MultiValues) -> Self {
124 Self {
125 name: name.into(),
126 value,
127 description: None,
128 is_final: false,
129 }
130 }
131
132 /// Gets the property name
133 ///
134 /// # Returns
135 ///
136 /// Returns the property name as a string slice
137 #[inline]
138 pub fn name(&self) -> &str {
139 &self.name
140 }
141
142 /// Gets a reference to the property value
143 ///
144 /// # Returns
145 ///
146 /// Returns a reference to the property value
147 #[inline]
148 pub fn value(&self) -> &MultiValues {
149 &self.value
150 }
151
152 /// Gets a mutable reference to the property value
153 ///
154 /// # Returns
155 ///
156 /// Returns a mutable reference to the property value
157 #[inline]
158 pub fn value_mut(&mut self) -> &mut MultiValues {
159 &mut self.value
160 }
161
162 /// Sets the property value
163 ///
164 /// # Parameters
165 ///
166 /// * `value` - New property value
167 #[inline]
168 pub fn set_value(&mut self, value: MultiValues) {
169 self.value = value;
170 }
171
172 /// Gets the property description
173 ///
174 /// # Returns
175 ///
176 /// Returns the property description as Option
177 #[inline]
178 pub fn description(&self) -> Option<&str> {
179 self.description.as_deref()
180 }
181
182 /// Sets the property description
183 ///
184 /// # Parameters
185 ///
186 /// * `description` - Property description
187 #[inline]
188 pub fn set_description(&mut self, description: Option<String>) {
189 self.description = description;
190 }
191
192 /// Checks if this is a final value
193 ///
194 /// # Returns
195 ///
196 /// Returns `true` if the property is final
197 #[inline]
198 pub fn is_final(&self) -> bool {
199 self.is_final
200 }
201
202 /// Sets whether this is a final value
203 ///
204 /// # Parameters
205 ///
206 /// * `is_final` - Whether this is final
207 #[inline]
208 pub fn set_final(&mut self, is_final: bool) {
209 self.is_final = is_final;
210 }
211
212 /// Gets the data type
213 ///
214 /// # Returns
215 ///
216 /// Returns the data type of the property value
217 #[inline]
218 pub fn data_type(&self) -> DataType {
219 self.value.data_type()
220 }
221
222 /// Gets the number of values
223 ///
224 /// # Returns
225 ///
226 /// Returns the number of values in the property
227 #[inline]
228 pub fn count(&self) -> usize {
229 self.value.count()
230 }
231
232 /// Checks if the property is empty
233 ///
234 /// # Returns
235 ///
236 /// Returns `true` if the property contains no values
237 #[inline]
238 pub fn is_empty(&self) -> bool {
239 self.value.is_empty()
240 }
241
242 /// Clears the property value
243 ///
244 /// Clears all values in the property but keeps type information
245 #[inline]
246 pub fn clear(&mut self) {
247 self.value.clear();
248 }
249}
250
251impl Deref for Property {
252 type Target = MultiValues;
253
254 /// Dereferences to MultiValues
255 ///
256 /// Allows direct access to all MultiValues methods
257 #[inline]
258 fn deref(&self) -> &Self::Target {
259 &self.value
260 }
261}
262
263impl DerefMut for Property {
264 /// Mutably dereferences to MultiValues
265 ///
266 /// Allows direct mutable access to all MultiValues methods
267 #[inline]
268 fn deref_mut(&mut self) -> &mut Self::Target {
269 &mut self.value
270 }
271}