Skip to main content

good_ormning/sqlite/
types.rs

1use quote::{
2    quote,
3};
4use crate::utils::RustTypes;
5
6#[derive(Clone, PartialEq, Eq, Debug)]
7pub enum SimpleSimpleType {
8    U32,
9    I32,
10    I64,
11    F32,
12    F64,
13    Bool,
14    String,
15    Bytes,
16    /// Time with second granularity, stored as int
17    #[cfg(feature = "chrono")]
18    UtcTimeSChrono,
19    /// Time with millisecond granularity, stored as string
20    #[cfg(feature = "chrono")]
21    UtcTimeMsChrono,
22    /// Time with millisecond granularity, stored as string
23    #[cfg(feature = "chrono")]
24    FixedOffsetTimeMsChrono,
25    /// Time with second granularity, stored as int
26    #[cfg(feature = "jiff")]
27    UtcTimeSJiff,
28    /// Time with millisecond granularity, stored as string
29    #[cfg(feature = "jiff")]
30    UtcTimeMsJiff,
31}
32
33#[doc(hidden)]
34pub fn to_sql_type(t: &SimpleSimpleType) -> &'static str {
35    match t {
36        SimpleSimpleType::U32 => "integer",
37        SimpleSimpleType::I32 => "integer",
38        SimpleSimpleType::I64 => "integer",
39        SimpleSimpleType::F32 => "real",
40        SimpleSimpleType::F64 => "real",
41        SimpleSimpleType::Bool => "integer",
42        SimpleSimpleType::String => "text",
43        SimpleSimpleType::Bytes => "blob",
44        #[cfg(feature = "chrono")]
45        SimpleSimpleType::UtcTimeSChrono => "integer",
46        #[cfg(feature = "chrono")]
47        SimpleSimpleType::UtcTimeMsChrono => "text",
48        #[cfg(feature = "chrono")]
49        SimpleSimpleType::FixedOffsetTimeMsChrono => "text",
50        #[cfg(feature = "jiff")]
51        SimpleSimpleType::UtcTimeSJiff => "integer",
52        #[cfg(feature = "jiff")]
53        SimpleSimpleType::UtcTimeMsJiff => "text",
54    }
55}
56
57pub fn to_rust_types(t: &SimpleSimpleType) -> RustTypes {
58    match t {
59        SimpleSimpleType::U32 => RustTypes {
60            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomU32),
61            ret_type: quote!(u32),
62            arg_type: quote!(u32),
63        },
64        SimpleSimpleType::I32 => RustTypes {
65            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomI32),
66            ret_type: quote!(i32),
67            arg_type: quote!(i32),
68        },
69        SimpleSimpleType::I64 => RustTypes {
70            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomI64),
71            ret_type: quote!(i64),
72            arg_type: quote!(i64),
73        },
74        SimpleSimpleType::F32 => RustTypes {
75            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomF32),
76            ret_type: quote!(f32),
77            arg_type: quote!(f32),
78        },
79        SimpleSimpleType::F64 => RustTypes {
80            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomF64),
81            ret_type: quote!(f64),
82            arg_type: quote!(f64),
83        },
84        SimpleSimpleType::Bool => RustTypes {
85            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomBool),
86            ret_type: quote!(bool),
87            arg_type: quote!(bool),
88        },
89        SimpleSimpleType::String => RustTypes {
90            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomString),
91            ret_type: quote!(String),
92            arg_type: quote!(&str),
93        },
94        SimpleSimpleType::Bytes => RustTypes {
95            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomBytes),
96            ret_type: quote!(Vec < u8 >),
97            arg_type: quote!(&[u8]),
98        },
99        #[cfg(feature = "chrono")]
100        SimpleSimpleType::UtcTimeSChrono => RustTypes {
101            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomUtcTimeChrono),
102            ret_type: quote!(chrono:: DateTime < chrono:: Utc >),
103            arg_type: quote!(chrono:: DateTime < chrono:: Utc >),
104        },
105        #[cfg(feature = "chrono")]
106        SimpleSimpleType::UtcTimeMsChrono => RustTypes {
107            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomUtcTimeChrono),
108            ret_type: quote!(chrono:: DateTime < chrono:: Utc >),
109            arg_type: quote!(chrono:: DateTime < chrono:: Utc >),
110        },
111        #[cfg(feature = "chrono")]
112        SimpleSimpleType::FixedOffsetTimeMsChrono => RustTypes {
113            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomFixedOffsetTimeChrono),
114            ret_type: quote!(chrono:: DateTime < chrono:: FixedOffset >),
115            arg_type: quote!(chrono:: DateTime < chrono:: FixedOffset >),
116        },
117        #[cfg(feature = "jiff")]
118        SimpleSimpleType::UtcTimeSJiff => RustTypes {
119            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomUtcTimeJiff),
120            ret_type: quote!(jiff::Timestamp),
121            arg_type: quote!(jiff::Timestamp),
122        },
123        #[cfg(feature = "jiff")]
124        SimpleSimpleType::UtcTimeMsJiff => RustTypes {
125            custom_trait: quote!(good_ormning_runtime::sqlite::GoodOrmningCustomUtcTimeJiff),
126            ret_type: quote!(jiff::Timestamp),
127            arg_type: quote!(jiff::Timestamp),
128        },
129    }
130}
131
132#[derive(Clone, PartialEq, Eq, Debug)]
133pub struct SimpleType {
134    pub type_: SimpleSimpleType,
135    pub custom: Option<String>,
136}
137
138#[derive(Clone, PartialEq, Eq, Debug)]
139pub struct Type {
140    pub type_: SimpleType,
141    pub opt: bool,
142    pub array: bool,
143}
144
145pub struct TypeBuilder {
146    t: SimpleSimpleType,
147    opt: bool,
148    array: bool,
149    custom: Option<String>,
150}
151
152impl TypeBuilder {
153    fn new(t: SimpleSimpleType) -> TypeBuilder {
154        TypeBuilder {
155            t: t,
156            opt: false,
157            array: false,
158            custom: None,
159        }
160    }
161
162    /// Make this value an array.
163    pub fn array(mut self) -> TypeBuilder {
164        self.array = true;
165        self
166    }
167
168    /// Make this value optional.
169    pub fn opt(mut self) -> TypeBuilder {
170        self.opt = true;
171        self
172    }
173
174    /// Use a custom Rust type for this type. This must be the full path to the type,
175    /// like `crate::abcdef::MyType`.
176    pub fn custom(mut self, type_: impl ToString) -> TypeBuilder {
177        self.custom = Some(type_.to_string());
178        self
179    }
180
181    pub fn build(self) -> Type {
182        Type {
183            type_: SimpleType {
184                custom: self.custom,
185                type_: self.t,
186            },
187            opt: self.opt,
188            array: self.array,
189        }
190    }
191}
192
193pub fn type_bool() -> TypeBuilder {
194    TypeBuilder::new(SimpleSimpleType::Bool)
195}
196
197pub fn type_i32() -> TypeBuilder {
198    TypeBuilder::new(SimpleSimpleType::I32)
199}
200
201pub fn type_i64() -> TypeBuilder {
202    TypeBuilder::new(SimpleSimpleType::I64)
203}
204
205pub fn type_u32() -> TypeBuilder {
206    TypeBuilder::new(SimpleSimpleType::U32)
207}
208
209pub fn type_f32() -> TypeBuilder {
210    TypeBuilder::new(SimpleSimpleType::F32)
211}
212
213pub fn type_f64() -> TypeBuilder {
214    TypeBuilder::new(SimpleSimpleType::F64)
215}
216
217pub fn type_str() -> TypeBuilder {
218    TypeBuilder::new(SimpleSimpleType::String)
219}
220
221pub fn type_bytes() -> TypeBuilder {
222    TypeBuilder::new(SimpleSimpleType::Bytes)
223}
224
225#[cfg(feature = "chrono")]
226pub fn type_utctime_s() -> TypeBuilder {
227    TypeBuilder::new(SimpleSimpleType::UtcTimeSChrono)
228}
229
230#[cfg(feature = "chrono")]
231pub fn type_utctime_ms() -> TypeBuilder {
232    TypeBuilder::new(SimpleSimpleType::UtcTimeMsChrono)
233}
234
235#[cfg(feature = "chrono")]
236pub fn type_fixedoffsettime_ms() -> TypeBuilder {
237    TypeBuilder::new(SimpleSimpleType::FixedOffsetTimeMsChrono)
238}
239
240#[cfg(feature = "jiff")]
241pub fn type_utctime_s_jiff() -> TypeBuilder {
242    TypeBuilder::new(SimpleSimpleType::UtcTimeSJiff)
243}
244
245#[cfg(feature = "jiff")]
246pub fn type_utctime_ms_jiff() -> TypeBuilder {
247    TypeBuilder::new(SimpleSimpleType::UtcTimeMsJiff)
248}