Skip to main content

qubit_mixin/
info_with_entity.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 with entity association
11//!
12
13use chrono::{
14    DateTime,
15    Utc,
16};
17use serde::{
18    Deserialize,
19    Serialize,
20};
21
22use crate::{
23    Deletable,
24    Identifiable,
25    Info,
26    WithCode,
27    WithEntity,
28    WithName,
29};
30
31/// Represents the basic information of a deletable object with entity
32/// association
33///
34/// # Type Parameters
35///
36/// * `E` - The type of the associated entity
37///
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
39pub struct InfoWithEntity<E>
40where
41    E: Clone,
42{
43    /// Basic information
44    #[serde(flatten)]
45    pub info: Info,
46
47    /// Associated entity
48    pub entity: Option<E>,
49}
50
51impl<E> InfoWithEntity<E>
52where
53    E: Clone,
54{
55    /// Creates a new `InfoWithEntity` object
56    ///
57    /// # Parameters
58    ///
59    /// * `id` - Unique identifier
60    /// * `code` - Code
61    /// * `name` - Name
62    /// * `delete_time` - Mark deletion time
63    /// * `entity` - Associated entity
64    ///
65    /// # Returns
66    ///
67    /// The newly created `InfoWithEntity` object
68    pub fn new(
69        id: Option<i64>,
70        code: String,
71        name: String,
72        delete_time: Option<DateTime<Utc>>,
73        entity: Option<E>,
74    ) -> Self {
75        Self {
76            info: Info::new(id, code, name, delete_time),
77            entity,
78        }
79    }
80}
81
82impl<E> Default for InfoWithEntity<E>
83where
84    E: Clone,
85{
86    fn default() -> Self {
87        Self {
88            info: Info::default(),
89            entity: None,
90        }
91    }
92}
93
94impl<E> Identifiable for InfoWithEntity<E>
95where
96    E: Clone,
97{
98    fn id(&self) -> Option<i64> {
99        self.info.id()
100    }
101
102    fn set_id(&mut self, id: Option<i64>) {
103        self.info.set_id(id);
104    }
105}
106
107impl<E> WithCode for InfoWithEntity<E>
108where
109    E: Clone,
110{
111    fn code(&self) -> &str {
112        self.info.code()
113    }
114
115    fn set_code(&mut self, code: &str) {
116        self.info.set_code(code);
117    }
118}
119
120impl<E> WithName for InfoWithEntity<E>
121where
122    E: Clone,
123{
124    fn name(&self) -> &str {
125        self.info.name()
126    }
127
128    fn set_name(&mut self, name: &str) {
129        self.info.set_name(name);
130    }
131}
132
133impl<E> Deletable for InfoWithEntity<E>
134where
135    E: Clone,
136{
137    fn delete_time(&self) -> Option<DateTime<Utc>> {
138        self.info.delete_time()
139    }
140
141    fn set_delete_time(&mut self, time: Option<DateTime<Utc>>) {
142        self.info.set_delete_time(time);
143    }
144}
145
146impl<E> WithEntity<E> for InfoWithEntity<E>
147where
148    E: Clone,
149{
150    fn entity(&self) -> Option<&E> {
151        self.entity.as_ref()
152    }
153
154    fn set_entity(&mut self, entity: Option<E>) {
155        self.entity = entity;
156    }
157}