Skip to main content

armour_typ/
get_type.rs

1use std::{
2    collections::{BTreeMap, HashMap},
3    net::{IpAddr, Ipv6Addr, SocketAddrV6},
4    num::{NonZeroU32, NonZeroU64},
5};
6
7use arrayvec::{ArrayString, ArrayVec};
8#[cfg(feature = "std")]
9use bytes::Bytes;
10#[cfg(feature = "rust_decimal")]
11use rust_decimal::Decimal;
12#[cfg(feature = "std")]
13use serde_json::Value;
14#[cfg(feature = "smallvec")]
15use smallvec::SmallVec;
16use uuid::Uuid;
17
18use crate::scalar::ScalarTyp;
19use crate::typ::{EnumType, Fields, StructType, Typ};
20
21pub trait GetType {
22    const TYPE: Typ;
23}
24
25impl GetType for bool {
26    const TYPE: Typ = Typ::Scalar(ScalarTyp::Bool);
27}
28
29impl GetType for u8 {
30    const TYPE: Typ = Typ::Scalar(ScalarTyp::U8);
31}
32
33impl GetType for u16 {
34    const TYPE: Typ = Typ::Scalar(ScalarTyp::U16);
35}
36
37impl GetType for u32 {
38    const TYPE: Typ = Typ::Scalar(ScalarTyp::U32);
39}
40
41impl GetType for u64 {
42    const TYPE: Typ = Typ::Scalar(ScalarTyp::U64);
43}
44
45impl GetType for usize {
46    const TYPE: Typ = Typ::Scalar(ScalarTyp::U32);
47}
48
49impl GetType for i32 {
50    const TYPE: Typ = Typ::Scalar(ScalarTyp::I32);
51}
52
53impl GetType for i64 {
54    const TYPE: Typ = Typ::Scalar(ScalarTyp::I64);
55}
56
57impl GetType for f32 {
58    const TYPE: Typ = Typ::Scalar(ScalarTyp::F32);
59}
60
61impl GetType for f64 {
62    const TYPE: Typ = Typ::Scalar(ScalarTyp::F64);
63}
64
65impl GetType for NonZeroU32 {
66    const TYPE: Typ = Typ::Scalar(ScalarTyp::U32);
67}
68
69impl GetType for NonZeroU64 {
70    const TYPE: Typ = Typ::Scalar(ScalarTyp::U64);
71}
72
73impl GetType for String {
74    const TYPE: Typ = Typ::Scalar(ScalarTyp::Str);
75}
76
77impl GetType for time::Duration {
78    const TYPE: Typ = Typ::Scalar(ScalarTyp::I64);
79}
80
81impl GetType for time::OffsetDateTime {
82    const TYPE: Typ = Typ::Scalar(ScalarTyp::Datetime);
83}
84
85impl GetType for time::PrimitiveDateTime {
86    const TYPE: Typ = Typ::Scalar(ScalarTyp::Datetime);
87}
88
89impl GetType for time::Date {
90    const TYPE: Typ = Typ::Scalar(ScalarTyp::I32);
91}
92
93impl GetType for time::Time {
94    const TYPE: Typ = Typ::Scalar(ScalarTyp::U32);
95}
96
97impl<T: GetType> GetType for Vec<T> {
98    const TYPE: Typ = Typ::Vec(&T::TYPE);
99}
100
101impl<T: GetType, const CAP: usize> GetType for ArrayVec<T, CAP> {
102    const TYPE: Typ = Typ::Vec(&T::TYPE);
103}
104
105impl<const CAP: usize> GetType for ArrayString<CAP> {
106    const TYPE: Typ = Typ::Scalar(ScalarTyp::Str);
107}
108
109#[cfg(feature = "smallvec")]
110impl<T: GetType, const CAP: usize> GetType for SmallVec<[T; CAP]> {
111    const TYPE: Typ = Typ::Vec(&T::TYPE);
112}
113
114#[cfg(feature = "rust_decimal")]
115impl GetType for Decimal {
116    const TYPE: Typ = Typ::Scalar(ScalarTyp::Decimal);
117}
118
119impl<T: GetType> GetType for Option<T> {
120    const TYPE: Typ = Typ::Optional(&T::TYPE);
121}
122
123impl<T: GetType> GetType for Box<T> {
124    const TYPE: Typ = T::TYPE;
125}
126
127impl<T1: GetType, T2: GetType> GetType for (T1, T2) {
128    const TYPE: Typ = {
129        let fields = &[T1::TYPE, T2::TYPE];
130
131        Typ::Struct(StructType {
132            name: "",
133            fields: Fields::Unnamed(fields),
134        })
135    };
136}
137
138impl<T1: GetType, T2: GetType, T3: GetType> GetType for (T1, T2, T3) {
139    const TYPE: Typ = {
140        let fields = &[T1::TYPE, T2::TYPE, T3::TYPE];
141
142        Typ::Struct(StructType {
143            name: "",
144            fields: Fields::Unnamed(fields),
145        })
146    };
147}
148
149impl<T1: GetType, T2: GetType, T3: GetType, T4: GetType> GetType for (T1, T2, T3, T4) {
150    const TYPE: Typ = {
151        let fields = &[T1::TYPE, T2::TYPE, T3::TYPE, T4::TYPE];
152
153        Typ::Struct(StructType {
154            name: "",
155            fields: Fields::Unnamed(fields),
156        })
157    };
158}
159
160impl<K: GetType, V: GetType> GetType for HashMap<K, V> {
161    const TYPE: Typ = Typ::Vec(&<(K, V)>::TYPE);
162}
163
164impl<K: GetType, V: GetType> GetType for BTreeMap<K, V> {
165    const TYPE: Typ = Typ::Vec(&<(K, V)>::TYPE);
166}
167
168impl<const CAP: usize> GetType for [u8; CAP] {
169    const TYPE: Typ = Typ::Scalar(ScalarTyp::ArrayBytes(CAP as u32));
170}
171
172impl GetType for Ipv6Addr {
173    const TYPE: Typ = Typ::Scalar(ScalarTyp::ArrayBytes(16));
174}
175
176impl GetType for IpAddr {
177    const TYPE: Typ = {
178        let variants = &[
179            (0, ("Ipv4Addr", Typ::Scalar(ScalarTyp::ArrayBytes(4)))),
180            (1, ("Ipv6Addr", Ipv6Addr::TYPE)),
181        ];
182
183        Typ::Enum(EnumType {
184            name: "IpAddr",
185            variants,
186        })
187    };
188}
189
190impl GetType for SocketAddrV6 {
191    const TYPE: Typ = {
192        let fields = Fields::Unnamed(&[Ipv6Addr::TYPE, u16::TYPE]);
193        let socket_addr_v6 = StructType {
194            name: "SocketAddrV6",
195            fields,
196        };
197        Typ::Struct(socket_addr_v6)
198    };
199}
200
201// impl<const CAP: usize, T: GetType> GetType for [T; CAP] {
202//     const TYPE: Typ = Typ::Array(CAP as u32, &T::TYPE);
203// }
204
205#[cfg(feature = "std")]
206impl GetType for Bytes {
207    const TYPE: Typ = Typ::Scalar(ScalarTyp::Bytes);
208}
209
210#[cfg(feature = "std")]
211impl GetType for Value {
212    const TYPE: Typ = Typ::Scalar(ScalarTyp::RustJson);
213}
214
215impl GetType for Uuid {
216    const TYPE: Typ = Typ::Scalar(ScalarTyp::ArrayBytes(16));
217}
218
219#[cfg(feature = "solana")]
220impl GetType for solana_pubkey::Pubkey {
221    const TYPE: Typ = Typ::Scalar(ScalarTyp::ArrayBytes(32));
222}
223
224#[cfg(feature = "solana")]
225impl GetType for solana_signature::Signature {
226    const TYPE: Typ = Typ::Scalar(ScalarTyp::ArrayBytes(64));
227}
228
229#[cfg(feature = "compact_str")]
230impl GetType for compact_str::CompactString {
231    const TYPE: Typ = Typ::Scalar(ScalarTyp::Str);
232}
233
234#[cfg(feature = "smol_str")]
235impl GetType for smol_str::SmolStr {
236    const TYPE: Typ = Typ::Scalar(ScalarTyp::Str);
237}