Skip to main content

wasm_dbms_api/dbms/
types.rs

1//! This module exposes the data types used in the DBMS.
2
3use 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
30/// A trait representing a data type that can be stored in the DBMS.
31///
32/// This is an umbrella trait that combines several other traits to ensure that
33/// any type implementing [`DataType`] can be cloned, compared, hashed, encoded,
34/// and serialized/deserialized using Serde.
35///
36/// Also it is used by the DBMS to compare and sort values of different data types.
37pub 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
54/// A trait for user-defined custom data types.
55///
56/// Custom types are stored as type-erased [`CustomValue`](crate::dbms::custom_value::CustomValue)
57/// inside `Value::Custom`. The `TYPE_TAG` constant uniquely identifies the type
58/// and must be stable across versions.
59///
60/// # Ordering contract
61///
62/// For custom types used with range filters (`Gt`, `Lt`, `Ge`, `Le`) or `ORDER BY`,
63/// the [`Encode`](crate::memory::Encode) output must be order-preserving: if `a < b`,
64/// then `a.encode() < b.encode()` lexicographically.
65/// Equality filters (`Eq`, `Ne`, `In`) only require canonical encoding.
66pub trait CustomDataType: DataType {
67    /// Unique string identifier for this type (e.g., `"principal"`, `"role"`).
68    const TYPE_TAG: &'static str;
69}
70
71/// An enumeration of all supported data type kinds in the DBMS.
72#[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}