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