qubit_mixin/info.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//! Basic information structure
11//!
12
13use chrono::{
14 DateTime,
15 Utc,
16};
17use serde::{
18 Deserialize,
19 Serialize,
20};
21
22use crate::{
23 Deletable,
24 Emptyful,
25 Identifiable,
26 Normalizable,
27 WithCode,
28 WithName,
29};
30
31/// Represents the basic information of a deletable object
32///
33/// This structure records:
34/// - Unique identifier (ID)
35/// - Code (usually globally unique)
36/// - Name
37/// - Mark deletion time
38///
39/// # Example
40///
41/// ```rust
42/// use qubit_mixin::Info;
43///
44/// let info = Info::new(
45/// Some(1),
46/// "CODE001".to_string(),
47/// "Test".to_string(),
48/// None,
49/// );
50/// assert_eq!(info.id, Some(1));
51/// assert_eq!(info.code, "CODE001");
52/// ```
53///
54#[derive(Debug, Clone, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
55pub struct Info {
56 /// Unique identifier
57 pub id: Option<i64>,
58
59 /// Code, usually globally unique
60 pub code: String,
61
62 /// Name
63 pub name: String,
64
65 /// Mark deletion time
66 pub delete_time: Option<DateTime<Utc>>,
67}
68
69impl Info {
70 /// Creates a new `Info` object
71 ///
72 /// # Parameters
73 ///
74 /// * `id` - Unique identifier, `None` indicates that no ID has been
75 /// assigned yet
76 /// * `code` - Code
77 /// * `name` - Name
78 /// * `delete_time` - Mark deletion time, `None` indicates not deleted
79 ///
80 /// # Returns
81 ///
82 /// The newly created `Info` object
83 pub fn new(
84 id: Option<i64>,
85 code: String,
86 name: String,
87 delete_time: Option<DateTime<Utc>>,
88 ) -> Self {
89 Self {
90 id,
91 code,
92 name,
93 delete_time,
94 }
95 }
96
97 /// Creates an `Info` object by ID
98 ///
99 /// # Parameters
100 ///
101 /// * `id` - Object ID
102 ///
103 /// # Returns
104 ///
105 /// An `Info` object with the specified ID, other fields are default
106 /// values
107 pub fn of_id(id: i64) -> Self {
108 Self {
109 id: Some(id),
110 code: String::new(),
111 name: String::new(),
112 delete_time: None,
113 }
114 }
115
116 /// Creates an `Info` object by code
117 ///
118 /// # Parameters
119 ///
120 /// * `code` - Object code
121 ///
122 /// # Returns
123 ///
124 /// An `Info` object with the specified code, other fields are default
125 /// values
126 pub fn of_code(code: String) -> Self {
127 Self {
128 id: None,
129 code,
130 name: String::new(),
131 delete_time: None,
132 }
133 }
134
135 /// Creates an `Info` object by name
136 ///
137 /// # Parameters
138 ///
139 /// * `name` - Object name
140 ///
141 /// # Returns
142 ///
143 /// An `Info` object with the specified name, other fields are default
144 /// values
145 pub fn of_name(name: String) -> Self {
146 Self {
147 id: None,
148 code: String::new(),
149 name,
150 delete_time: None,
151 }
152 }
153}
154
155impl Identifiable for Info {
156 fn id(&self) -> Option<i64> {
157 self.id
158 }
159
160 fn set_id(&mut self, id: Option<i64>) {
161 self.id = id;
162 }
163}
164
165impl WithCode for Info {
166 fn code(&self) -> &str {
167 &self.code
168 }
169
170 fn set_code(&mut self, code: &str) {
171 self.code = code.to_string();
172 }
173}
174
175impl WithName for Info {
176 fn name(&self) -> &str {
177 &self.name
178 }
179
180 fn set_name(&mut self, name: &str) {
181 self.name = name.to_string();
182 }
183}
184
185impl Deletable for Info {
186 fn delete_time(&self) -> Option<DateTime<Utc>> {
187 self.delete_time
188 }
189
190 fn set_delete_time(&mut self, time: Option<DateTime<Utc>>) {
191 self.delete_time = time;
192 }
193}
194
195impl Emptyful for Info {
196 fn is_empty(&self) -> bool {
197 self.id.is_none()
198 && self.code.is_empty()
199 && self.name.is_empty()
200 && self.delete_time.is_none()
201 }
202}
203
204impl Normalizable for Info {
205 fn normalize(&mut self) {
206 self.code = self.code.trim().to_string();
207 self.name = self.name.trim().to_string();
208 }
209}