Skip to main content

qubit_common/lang/
data_type.rs

1/*******************************************************************************
2 *
3 *    Copyright (c) 2025 - 2026.
4 *    Haixing Hu, Qubit Co. Ltd.
5 *
6 *    All rights reserved.
7 *
8 ******************************************************************************/
9//! # Data Type Definitions (Language Layer)
10//!
11//! Provides cross-module reusable common data type enum `DataType` and type mapping `DataTypeOf`.
12//!
13//! # Author
14//!
15//! Haixing Hu
16
17use bigdecimal::BigDecimal;
18use chrono::{
19    DateTime,
20    NaiveDate,
21    NaiveDateTime,
22    NaiveTime,
23    Utc,
24};
25use num_bigint::BigInt;
26use serde::{
27    Deserialize,
28    Serialize,
29};
30use serde_json;
31use std::collections::HashMap;
32use std::time::Duration;
33
34/// Universal data type enumeration for cross-module type representation
35///
36/// Defines all basic data types and composite types supported by the system.
37/// This enum provides a unified way to represent and work with different data types
38/// across various modules and components.
39///
40/// `DataType` serves as a bridge between Rust's type system and runtime type
41/// information, enabling dynamic type handling, serialization, validation,
42/// and other type-aware operations.
43///
44/// # Features
45///
46/// - **Comprehensive Coverage**: Supports all basic Rust types plus common third-party types
47/// - **String Representation**: Each variant has a consistent string representation
48/// - **Serialization Support**: Implements `Serialize` and `Deserialize` for JSON/YAML support
49/// - **Type Mapping**: Works with `DataTypeOf` trait for compile-time type mapping
50///
51/// # Use Cases
52///
53/// - **Dynamic Type Handling**: Runtime type checking and conversion
54/// - **Serialization/Deserialization**: Type-aware data format conversion
55/// - **Validation Systems**: Type-based input validation
56/// - **Generic Programming**: Type-safe generic operations
57/// - **API Documentation**: Automatic type information generation
58///
59/// # Examples
60///
61/// ## Basic Usage
62///
63/// ```rust,ignore
64/// use qubit_common::lang::DataType;
65///
66/// let data_type = DataType::Int32;
67/// assert_eq!(data_type.to_string(), "int32");
68/// assert_eq!(data_type.as_str(), "int32");
69/// ```
70///
71/// ## Type Checking
72///
73/// ```rust,ignore
74/// use qubit_common::lang::DataType;
75///
76/// fn is_numeric(data_type: DataType) -> bool {
77///     matches!(data_type,
78///         DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 | DataType::Int128 |
79///         DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 | DataType::UInt128 |
80///         DataType::Float32 | DataType::Float64 | DataType::BigInteger | DataType::BigDecimal
81///     )
82/// }
83///
84/// assert!(is_numeric(DataType::Int32));
85/// assert!(!is_numeric(DataType::String));
86/// ```
87///
88/// ## Serialization
89///
90/// ```rust,ignore
91/// use qubit_common::lang::DataType;
92/// use serde_json;
93///
94/// let data_type = DataType::Float64;
95/// let json = serde_json::to_string(&data_type).unwrap();
96/// assert_eq!(json, "\"float64\"");
97///
98/// let deserialized: DataType = serde_json::from_str(&json).unwrap();
99/// assert_eq!(deserialized, DataType::Float64);
100/// ```
101///
102/// # Author
103///
104/// Haixing Hu
105///
106#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
107pub enum DataType {
108    /// Boolean type
109    Bool,
110    /// Character type
111    Char,
112    /// 8-bit signed integer
113    Int8,
114    /// 16-bit signed integer
115    Int16,
116    /// 32-bit signed integer
117    Int32,
118    /// 64-bit signed integer
119    Int64,
120    /// 128-bit signed integer
121    Int128,
122    /// 8-bit unsigned integer
123    UInt8,
124    /// 16-bit unsigned integer
125    UInt16,
126    /// 32-bit unsigned integer
127    UInt32,
128    /// 64-bit unsigned integer
129    UInt64,
130    /// 128-bit unsigned integer
131    UInt128,
132    /// 32-bit floating point number
133    Float32,
134    /// 64-bit floating point number
135    Float64,
136    /// String type
137    String,
138    /// Date type (NaiveDate)
139    Date,
140    /// Time type (NaiveTime)
141    Time,
142    /// DateTime type (NaiveDateTime)
143    DateTime,
144    /// UTC time point (equivalent to Java Instant) (`DateTime<Utc>`)
145    Instant,
146    /// Big integer type (BigInt)
147    BigInteger,
148    /// Big decimal type (BigDecimal)
149    BigDecimal,
150    /// Platform-dependent signed integer (isize)
151    IntSize,
152    /// Platform-dependent unsigned integer (usize)
153    UIntSize,
154    /// Duration type (std::time::Duration)
155    Duration,
156    /// URL type (url::Url)
157    Url,
158    /// String map type (HashMap<String, String>)
159    StringMap,
160    /// JSON value type (serde_json::Value)
161    Json,
162}
163
164impl DataType {
165    /// Get the string representation of the data type
166    ///
167    /// # Returns
168    ///
169    /// Returns the name string of the data type
170    ///
171    /// # Examples
172    ///
173    /// ```rust,ignore
174    /// use qubit_common::lang::DataType;
175    ///
176    /// assert_eq!(DataType::Int32.as_str(), "int32");
177    /// assert_eq!(DataType::String.as_str(), "string");
178    /// ```
179    pub const fn as_str(&self) -> &'static str {
180        match self {
181            DataType::Bool => "bool",
182            DataType::Char => "char",
183            DataType::Int8 => "int8",
184            DataType::Int16 => "int16",
185            DataType::Int32 => "int32",
186            DataType::Int64 => "int64",
187            DataType::Int128 => "int128",
188            DataType::UInt8 => "uint8",
189            DataType::UInt16 => "uint16",
190            DataType::UInt32 => "uint32",
191            DataType::UInt64 => "uint64",
192            DataType::UInt128 => "uint128",
193            DataType::Float32 => "float32",
194            DataType::Float64 => "float64",
195            DataType::String => "string",
196            DataType::Date => "date",
197            DataType::Time => "time",
198            DataType::DateTime => "datetime",
199            DataType::Instant => "instant",
200            DataType::BigInteger => "biginteger",
201            DataType::BigDecimal => "bigdecimal",
202            DataType::IntSize => "intsize",
203            DataType::UIntSize => "uintsize",
204            DataType::Duration => "duration",
205            DataType::Url => "url",
206            DataType::StringMap => "stringmap",
207            DataType::Json => "json",
208        }
209    }
210}
211
212impl std::fmt::Display for DataType {
213    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
214        write!(f, "{}", self.as_str())
215    }
216}
217
218// =============================================================================
219// Compile-time mapping from types to DataType
220// =============================================================================
221
222/// Marker trait for mapping concrete Rust types to `DataType`
223///
224/// Provides an associated constant to know the corresponding `DataType` at compile time,
225/// facilitating static type-to-data-type queries in generic code based on `T`.
226///
227/// This trait enables compile-time type-to-data-type mapping, allowing generic code
228/// to determine the appropriate `DataType` for any type that implements this trait.
229/// This is particularly useful for serialization frameworks, validation systems,
230/// and other scenarios where you need to know the data type at compile time.
231///
232/// # Usage
233///
234/// The trait is automatically implemented for all basic Rust types and common
235/// third-party types. You can use it in generic functions to determine the
236/// corresponding `DataType` for any type.
237///
238/// # Examples
239///
240/// ## Basic Usage
241///
242/// ```rust,ignore
243/// use qubit_common::lang::{DataType, DataTypeOf};
244///
245/// // Get the data type for a specific type
246/// assert_eq!(i32::DATA_TYPE, DataType::Int32);
247/// assert_eq!(String::DATA_TYPE, DataType::String);
248/// assert_eq!(bool::DATA_TYPE, DataType::Bool);
249/// ```
250///
251/// ## Generic Function Example
252///
253/// ```rust,ignore
254/// use qubit_common::lang::{DataType, DataTypeOf};
255///
256/// fn get_type_name<T: DataTypeOf>() -> &'static str {
257///     T::DATA_TYPE.as_str()
258/// }
259///
260/// assert_eq!(get_type_name::<i32>(), "int32");
261/// assert_eq!(get_type_name::<String>(), "string");
262/// assert_eq!(get_type_name::<f64>(), "float64");
263/// ```
264///
265/// ## Generic Value Container Example
266///
267/// ```rust,ignore
268/// use qubit_common::lang::{DataType, DataTypeOf};
269///
270/// struct TypedValue<T: DataTypeOf> {
271///     value: T,
272///     data_type: DataType,
273/// }
274///
275/// impl<T: DataTypeOf> TypedValue<T> {
276///     fn new(value: T) -> Self {
277///         Self {
278///             value,
279///             data_type: T::DATA_TYPE,
280///         }
281///     }
282///
283///     fn get_data_type(&self) -> DataType {
284///         self.data_type
285///     }
286/// }
287///
288/// let typed_value = TypedValue::new(42i32);
289/// assert_eq!(typed_value.get_data_type(), DataType::Int32);
290/// ```
291///
292/// ## Type Validation Example
293///
294/// ```rust,ignore
295/// use qubit_common::lang::{DataType, DataTypeOf};
296///
297/// fn validate_numeric_type<T: DataTypeOf>() -> bool {
298///     matches!(T::DATA_TYPE,
299///         DataType::Int8 | DataType::Int16 | DataType::Int32 | DataType::Int64 | DataType::Int128 |
300///         DataType::UInt8 | DataType::UInt16 | DataType::UInt32 | DataType::UInt64 | DataType::UInt128 |
301///         DataType::Float32 | DataType::Float64 | DataType::BigInteger | DataType::BigDecimal
302///     )
303/// }
304///
305/// assert!(validate_numeric_type::<i32>());
306/// assert!(validate_numeric_type::<f64>());
307/// assert!(!validate_numeric_type::<String>());
308/// ```
309///
310/// # Supported Types
311///
312/// The following types have `DataTypeOf` implementations:
313///
314/// - **Basic Types**: `bool`, `char`, `i8`, `i16`, `i32`, `i64`, `i128`, `u8`, `u16`, `u32`, `u64`, `u128`, `f32`, `f64`
315/// - **String Types**: `String`
316/// - **Date/Time Types**: `NaiveDate`, `NaiveTime`, `NaiveDateTime`, `DateTime<Utc>`
317/// - **Big Number Types**: `BigInt`, `BigDecimal`
318///
319/// # Author
320///
321/// Haixing Hu
322///
323pub trait DataTypeOf {
324    /// The `DataType` corresponding to this Rust type
325    const DATA_TYPE: DataType;
326}
327
328// Basic scalar types
329impl DataTypeOf for bool {
330    const DATA_TYPE: DataType = DataType::Bool;
331}
332impl DataTypeOf for char {
333    const DATA_TYPE: DataType = DataType::Char;
334}
335impl DataTypeOf for i8 {
336    const DATA_TYPE: DataType = DataType::Int8;
337}
338impl DataTypeOf for i16 {
339    const DATA_TYPE: DataType = DataType::Int16;
340}
341impl DataTypeOf for i32 {
342    const DATA_TYPE: DataType = DataType::Int32;
343}
344impl DataTypeOf for i64 {
345    const DATA_TYPE: DataType = DataType::Int64;
346}
347impl DataTypeOf for i128 {
348    const DATA_TYPE: DataType = DataType::Int128;
349}
350impl DataTypeOf for u8 {
351    const DATA_TYPE: DataType = DataType::UInt8;
352}
353impl DataTypeOf for u16 {
354    const DATA_TYPE: DataType = DataType::UInt16;
355}
356impl DataTypeOf for u32 {
357    const DATA_TYPE: DataType = DataType::UInt32;
358}
359impl DataTypeOf for u64 {
360    const DATA_TYPE: DataType = DataType::UInt64;
361}
362impl DataTypeOf for u128 {
363    const DATA_TYPE: DataType = DataType::UInt128;
364}
365impl DataTypeOf for f32 {
366    const DATA_TYPE: DataType = DataType::Float32;
367}
368impl DataTypeOf for f64 {
369    const DATA_TYPE: DataType = DataType::Float64;
370}
371
372// String types
373impl DataTypeOf for String {
374    const DATA_TYPE: DataType = DataType::String;
375}
376
377// Date and time types (chrono)
378impl DataTypeOf for NaiveDate {
379    const DATA_TYPE: DataType = DataType::Date;
380}
381impl DataTypeOf for NaiveTime {
382    const DATA_TYPE: DataType = DataType::Time;
383}
384impl DataTypeOf for NaiveDateTime {
385    const DATA_TYPE: DataType = DataType::DateTime;
386}
387impl DataTypeOf for DateTime<Utc> {
388    const DATA_TYPE: DataType = DataType::Instant;
389}
390
391// Big number types
392impl DataTypeOf for BigInt {
393    const DATA_TYPE: DataType = DataType::BigInteger;
394}
395impl DataTypeOf for BigDecimal {
396    const DATA_TYPE: DataType = DataType::BigDecimal;
397}
398
399// Platform-dependent integer types
400impl DataTypeOf for isize {
401    const DATA_TYPE: DataType = DataType::IntSize;
402}
403impl DataTypeOf for usize {
404    const DATA_TYPE: DataType = DataType::UIntSize;
405}
406
407// Standard library types
408impl DataTypeOf for Duration {
409    const DATA_TYPE: DataType = DataType::Duration;
410}
411
412// String map type
413impl DataTypeOf for HashMap<String, String> {
414    const DATA_TYPE: DataType = DataType::StringMap;
415}
416
417// JSON value type
418impl DataTypeOf for serde_json::Value {
419    const DATA_TYPE: DataType = DataType::Json;
420}