systemprompt_identifiers/db_value/
to_value.rs

1use chrono::{DateTime, Utc};
2
3use super::DbValue;
4
5pub trait ToDbValue: Send + Sync {
6    fn to_db_value(&self) -> DbValue;
7
8    #[must_use]
9    fn null_db_value() -> DbValue
10    where
11        Self: Sized,
12    {
13        DbValue::NullString
14    }
15}
16
17impl ToDbValue for &str {
18    fn to_db_value(&self) -> DbValue {
19        DbValue::String((*self).to_string())
20    }
21
22    fn null_db_value() -> DbValue {
23        DbValue::NullString
24    }
25}
26
27impl ToDbValue for String {
28    fn to_db_value(&self) -> DbValue {
29        DbValue::String(self.clone())
30    }
31
32    fn null_db_value() -> DbValue {
33        DbValue::NullString
34    }
35}
36
37impl ToDbValue for &String {
38    fn to_db_value(&self) -> DbValue {
39        DbValue::String((*self).clone())
40    }
41
42    fn null_db_value() -> DbValue {
43        DbValue::NullString
44    }
45}
46
47impl ToDbValue for i32 {
48    fn to_db_value(&self) -> DbValue {
49        DbValue::Int(i64::from(*self))
50    }
51
52    fn null_db_value() -> DbValue {
53        DbValue::NullInt
54    }
55}
56
57impl ToDbValue for i64 {
58    fn to_db_value(&self) -> DbValue {
59        DbValue::Int(*self)
60    }
61
62    fn null_db_value() -> DbValue {
63        DbValue::NullInt
64    }
65}
66
67impl ToDbValue for u32 {
68    fn to_db_value(&self) -> DbValue {
69        DbValue::Int(i64::from(*self))
70    }
71
72    fn null_db_value() -> DbValue {
73        DbValue::NullInt
74    }
75}
76
77impl ToDbValue for u64 {
78    fn to_db_value(&self) -> DbValue {
79        DbValue::Int(i64::try_from(*self).unwrap_or(i64::MAX))
80    }
81
82    fn null_db_value() -> DbValue {
83        DbValue::NullInt
84    }
85}
86
87impl ToDbValue for f32 {
88    fn to_db_value(&self) -> DbValue {
89        DbValue::Float(f64::from(*self))
90    }
91
92    fn null_db_value() -> DbValue {
93        DbValue::NullFloat
94    }
95}
96
97impl ToDbValue for f64 {
98    fn to_db_value(&self) -> DbValue {
99        DbValue::Float(*self)
100    }
101
102    fn null_db_value() -> DbValue {
103        DbValue::NullFloat
104    }
105}
106
107impl ToDbValue for &f64 {
108    fn to_db_value(&self) -> DbValue {
109        DbValue::Float(**self)
110    }
111
112    fn null_db_value() -> DbValue {
113        DbValue::NullFloat
114    }
115}
116
117impl ToDbValue for &i32 {
118    fn to_db_value(&self) -> DbValue {
119        DbValue::Int(i64::from(**self))
120    }
121
122    fn null_db_value() -> DbValue {
123        DbValue::NullInt
124    }
125}
126
127impl ToDbValue for &i64 {
128    fn to_db_value(&self) -> DbValue {
129        DbValue::Int(**self)
130    }
131
132    fn null_db_value() -> DbValue {
133        DbValue::NullInt
134    }
135}
136
137impl ToDbValue for &bool {
138    fn to_db_value(&self) -> DbValue {
139        DbValue::Bool(**self)
140    }
141
142    fn null_db_value() -> DbValue {
143        DbValue::NullBool
144    }
145}
146
147impl ToDbValue for bool {
148    fn to_db_value(&self) -> DbValue {
149        DbValue::Bool(*self)
150    }
151
152    fn null_db_value() -> DbValue {
153        DbValue::NullBool
154    }
155}
156
157impl ToDbValue for Vec<u8> {
158    fn to_db_value(&self) -> DbValue {
159        DbValue::Bytes(self.clone())
160    }
161
162    fn null_db_value() -> DbValue {
163        DbValue::NullBytes
164    }
165}
166
167impl ToDbValue for &[u8] {
168    fn to_db_value(&self) -> DbValue {
169        DbValue::Bytes(self.to_vec())
170    }
171
172    fn null_db_value() -> DbValue {
173        DbValue::NullBytes
174    }
175}
176
177impl<T: ToDbValue> ToDbValue for Option<T> {
178    fn to_db_value(&self) -> DbValue {
179        self.as_ref()
180            .map_or_else(T::null_db_value, ToDbValue::to_db_value)
181    }
182}
183
184impl ToDbValue for DateTime<Utc> {
185    fn to_db_value(&self) -> DbValue {
186        DbValue::Timestamp(*self)
187    }
188
189    fn null_db_value() -> DbValue {
190        DbValue::NullTimestamp
191    }
192}
193
194impl ToDbValue for &DateTime<Utc> {
195    fn to_db_value(&self) -> DbValue {
196        DbValue::Timestamp(**self)
197    }
198
199    fn null_db_value() -> DbValue {
200        DbValue::NullTimestamp
201    }
202}
203
204impl ToDbValue for Vec<String> {
205    fn to_db_value(&self) -> DbValue {
206        DbValue::StringArray(self.clone())
207    }
208
209    fn null_db_value() -> DbValue {
210        DbValue::NullStringArray
211    }
212}
213
214impl ToDbValue for &Vec<String> {
215    fn to_db_value(&self) -> DbValue {
216        DbValue::StringArray((*self).clone())
217    }
218
219    fn null_db_value() -> DbValue {
220        DbValue::NullStringArray
221    }
222}
223
224impl ToDbValue for &[String] {
225    fn to_db_value(&self) -> DbValue {
226        DbValue::StringArray(self.to_vec())
227    }
228
229    fn null_db_value() -> DbValue {
230        DbValue::NullStringArray
231    }
232}