1use serde::{Deserialize, Serialize};
4
5use crate::dbms::value::Value;
6use crate::memory::Encode;
7
8mod blob;
9mod boolean;
10mod date;
11mod datetime;
12mod decimal;
13mod integers;
14mod json;
15mod nullable;
16mod text;
17mod uuid;
18
19pub use self::blob::Blob;
20pub use self::boolean::Boolean;
21pub use self::date::Date;
22pub use self::datetime::DateTime;
23pub use self::decimal::Decimal;
24pub use self::integers::{Int8, Int16, Int32, Int64, Uint8, Uint16, Uint32, Uint64};
25pub use self::json::Json;
26pub use self::nullable::Nullable;
27pub use self::text::Text;
28pub use self::uuid::Uuid;
29
30pub trait DataType:
38 Clone
39 + std::fmt::Debug
40 + std::fmt::Display
41 + PartialEq
42 + Eq
43 + Default
44 + PartialOrd
45 + Ord
46 + std::hash::Hash
47 + Encode
48 + Serialize
49 + Into<Value>
50 + for<'de> Deserialize<'de>
51{
52}
53
54pub trait CustomDataType: DataType {
67 const TYPE_TAG: &'static str;
69}
70
71#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
73pub enum DataTypeKind {
74 Blob,
75 Boolean,
76 Date,
77 DateTime,
78 Decimal,
79 Int8,
80 Int16,
81 Int32,
82 Int64,
83 Json,
84 Text,
85 Uint8,
86 Uint16,
87 Uint32,
88 Uint64,
89 Uuid,
90 Custom(&'static str),
91}
92
93#[cfg(test)]
94mod test {
95
96 use std::collections::HashSet;
97
98 use super::*;
99
100 #[test]
101 fn test_should_create_all_data_type_kind_variants() {
102 let kinds = [
103 DataTypeKind::Blob,
104 DataTypeKind::Boolean,
105 DataTypeKind::Date,
106 DataTypeKind::DateTime,
107 DataTypeKind::Decimal,
108 DataTypeKind::Int8,
109 DataTypeKind::Int16,
110 DataTypeKind::Int32,
111 DataTypeKind::Int64,
112 DataTypeKind::Json,
113 DataTypeKind::Text,
114 DataTypeKind::Uint8,
115 DataTypeKind::Uint16,
116 DataTypeKind::Uint32,
117 DataTypeKind::Uint64,
118 DataTypeKind::Uuid,
119 ];
120
121 assert_eq!(kinds.len(), 16);
122 }
123
124 #[test]
125 #[allow(clippy::clone_on_copy)]
126 fn test_should_clone_data_type_kind() {
127 let kind = DataTypeKind::Text;
128 let cloned = kind.clone();
129 assert_eq!(kind, cloned);
130 }
131
132 #[test]
133 fn test_should_copy_data_type_kind() {
134 let kind = DataTypeKind::Uint32;
135 let copied = kind;
136 assert_eq!(kind, copied);
137 }
138
139 #[test]
140 fn test_should_compare_data_type_kinds() {
141 assert_eq!(DataTypeKind::Blob, DataTypeKind::Blob);
142 assert_eq!(DataTypeKind::Boolean, DataTypeKind::Boolean);
143 assert_ne!(DataTypeKind::Blob, DataTypeKind::Boolean);
144 assert_ne!(DataTypeKind::Int32, DataTypeKind::Int64);
145 assert_ne!(DataTypeKind::Uint32, DataTypeKind::Uint64);
146 }
147
148 #[test]
149 fn test_should_hash_data_type_kind() {
150 let mut set = HashSet::new();
151 set.insert(DataTypeKind::Text);
152 set.insert(DataTypeKind::Uint32);
153 set.insert(DataTypeKind::Boolean);
154
155 assert!(set.contains(&DataTypeKind::Text));
156 assert!(set.contains(&DataTypeKind::Uint32));
157 assert!(set.contains(&DataTypeKind::Boolean));
158 assert!(!set.contains(&DataTypeKind::Blob));
159 }
160
161 #[test]
162 fn test_should_debug_data_type_kind() {
163 assert_eq!(format!("{:?}", DataTypeKind::Blob), "Blob");
164 assert_eq!(format!("{:?}", DataTypeKind::Boolean), "Boolean");
165 assert_eq!(format!("{:?}", DataTypeKind::Date), "Date");
166 assert_eq!(format!("{:?}", DataTypeKind::DateTime), "DateTime");
167 assert_eq!(format!("{:?}", DataTypeKind::Decimal), "Decimal");
168 assert_eq!(format!("{:?}", DataTypeKind::Int8), "Int8");
169 assert_eq!(format!("{:?}", DataTypeKind::Int16), "Int16");
170 assert_eq!(format!("{:?}", DataTypeKind::Int32), "Int32");
171 assert_eq!(format!("{:?}", DataTypeKind::Int64), "Int64");
172 assert_eq!(format!("{:?}", DataTypeKind::Json), "Json");
173 assert_eq!(format!("{:?}", DataTypeKind::Text), "Text");
174 assert_eq!(format!("{:?}", DataTypeKind::Uint8), "Uint8");
175 assert_eq!(format!("{:?}", DataTypeKind::Uint16), "Uint16");
176 assert_eq!(format!("{:?}", DataTypeKind::Uint32), "Uint32");
177 assert_eq!(format!("{:?}", DataTypeKind::Uint64), "Uint64");
178 assert_eq!(format!("{:?}", DataTypeKind::Uuid), "Uuid");
179 }
180
181 #[test]
182 fn test_should_use_data_type_kind_as_hashmap_key() {
183 use std::collections::HashMap;
184
185 let mut map = HashMap::new();
186 map.insert(DataTypeKind::Text, "String type");
187 map.insert(DataTypeKind::Uint32, "32-bit unsigned integer");
188
189 assert_eq!(map.get(&DataTypeKind::Text), Some(&"String type"));
190 assert_eq!(
191 map.get(&DataTypeKind::Uint32),
192 Some(&"32-bit unsigned integer")
193 );
194 assert_eq!(map.get(&DataTypeKind::Blob), None);
195 }
196
197 #[test]
198 fn test_should_create_custom_data_type_kind() {
199 let kind = DataTypeKind::Custom("role");
200 assert_eq!(kind, DataTypeKind::Custom("role"));
201 assert_ne!(kind, DataTypeKind::Custom("status"));
202 assert_ne!(kind, DataTypeKind::Text);
203 }
204
205 #[test]
206 fn test_should_copy_custom_data_type_kind() {
207 let kind = DataTypeKind::Custom("role");
208 let copied = kind;
209 assert_eq!(kind, copied);
210 }
211
212 #[test]
213 fn test_should_debug_custom_data_type_kind() {
214 let kind = DataTypeKind::Custom("role");
215 let debug = format!("{kind:?}");
216 assert!(debug.contains("Custom"));
217 assert!(debug.contains("role"));
218 }
219}