Skip to main content

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(id: Option<i64>, code: String, name: String, delete_time: Option<DateTime<Utc>>) -> Self {
84        Self {
85            id,
86            code,
87            name,
88            delete_time,
89        }
90    }
91
92    /// Creates an `Info` object by ID
93    ///
94    /// # Parameters
95    ///
96    /// * `id` - Object ID
97    ///
98    /// # Returns
99    ///
100    /// An `Info` object with the specified ID, other fields are default
101    /// values
102    pub fn of_id(id: i64) -> Self {
103        Self {
104            id: Some(id),
105            code: String::new(),
106            name: String::new(),
107            delete_time: None,
108        }
109    }
110
111    /// Creates an `Info` object by code
112    ///
113    /// # Parameters
114    ///
115    /// * `code` - Object code
116    ///
117    /// # Returns
118    ///
119    /// An `Info` object with the specified code, other fields are default
120    /// values
121    pub fn of_code(code: String) -> Self {
122        Self {
123            id: None,
124            code,
125            name: String::new(),
126            delete_time: None,
127        }
128    }
129
130    /// Creates an `Info` object by name
131    ///
132    /// # Parameters
133    ///
134    /// * `name` - Object name
135    ///
136    /// # Returns
137    ///
138    /// An `Info` object with the specified name, other fields are default
139    /// values
140    pub fn of_name(name: String) -> Self {
141        Self {
142            id: None,
143            code: String::new(),
144            name,
145            delete_time: None,
146        }
147    }
148}
149
150impl Identifiable for Info {
151    fn id(&self) -> Option<i64> {
152        self.id
153    }
154
155    fn set_id(&mut self, id: Option<i64>) {
156        self.id = id;
157    }
158}
159
160impl WithCode for Info {
161    fn code(&self) -> &str {
162        &self.code
163    }
164
165    fn set_code(&mut self, code: &str) {
166        self.code = code.to_string();
167    }
168}
169
170impl WithName for Info {
171    fn name(&self) -> &str {
172        &self.name
173    }
174
175    fn set_name(&mut self, name: &str) {
176        self.name = name.to_string();
177    }
178}
179
180impl Deletable for Info {
181    fn delete_time(&self) -> Option<DateTime<Utc>> {
182        self.delete_time
183    }
184
185    fn set_delete_time(&mut self, time: Option<DateTime<Utc>>) {
186        self.delete_time = time;
187    }
188}
189
190impl Emptyful for Info {
191    fn is_empty(&self) -> bool {
192        self.id.is_none() && self.code.is_empty() && self.name.is_empty() && self.delete_time.is_none()
193    }
194}
195
196impl Normalizable for Info {
197    fn normalize(&mut self) {
198        self.code = self.code.trim().to_string();
199        self.name = self.name.trim().to_string();
200    }
201}