Skip to main content

systemprompt_identifiers/db_value/
to_value.rs

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