1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
#[cfg(feature = "time")]
use tea_time::TimeUnitTrait;
#[cfg(feature = "time")]
use tea_time::{DateTime, TimeDelta, TimeUnit};

#[derive(PartialEq, Eq, Debug)]
pub enum DataType {
    Bool,
    F32,
    F64,
    I32,
    I64,
    U8,
    U64,
    Usize,
    Str,
    String,
    Object,
    OptBool,
    OptF32,
    OptF64,
    OptI32,
    OptI64,
    OptUsize,
    VecUsize,
    #[cfg(feature = "time")]
    DateTime(TimeUnit),
    #[cfg(feature = "time")]
    TimeDelta,
}

pub trait GetDataType {
    fn dtype() -> DataType
    where
        Self: Sized;
}

macro_rules! impl_datatype {
    ($tyname:ident, $physical:ty) => {
        impl GetDataType for $physical {
            #[inline(always)]
            fn dtype() -> DataType {
                DataType::$tyname
            }
        }
    };
}

impl DataType {
    #[inline(always)]
    pub fn is_float(&self) -> bool {
        use DataType::*;
        matches!(self, F32 | F64 | OptF32 | OptF64)
    }

    #[inline(always)]
    pub fn is_int(&self) -> bool {
        use DataType::*;
        matches!(self, I32 | I64 | Usize | U64 | OptUsize | OptI32 | OptI64)
    }

    #[inline]
    pub fn is_time(&self) -> bool {
        #[cfg(feature = "time")]
        {
            use DataType::*;
            matches!(self, DateTime(_))
        }
        #[cfg(not(feature = "time"))]
        {
            false
        }
    }

    pub fn float(self) -> Self {
        use DataType::*;
        match self {
            F32 => F32,
            I32 => F32,
            I64 => F64,
            Usize => F64,
            U64 => F64,
            OptUsize => F64,
            OptI32 => OptF32,
            OptI64 => OptF64,
            OptF32 => OptF32,
            OptF64 => OptF64,
            _ => F64,
        }
    }

    pub fn int(self) -> Self {
        use DataType::*;
        match self {
            I32 => I32,
            F32 => I32,
            F64 => I64,
            Usize => Usize,
            U64 => I64,
            OptUsize => OptUsize,
            OptI32 => OptI32,
            OptI64 => OptI64,
            OptF32 => OptI32,
            OptF64 => OptI64,
            _ => I64,
        }
    }
}

impl_datatype!(Bool, bool);
impl_datatype!(U8, u8);
impl_datatype!(F32, f32);
impl_datatype!(F64, f64);
impl_datatype!(I32, i32);
impl_datatype!(I64, i64);
impl_datatype!(U64, u64);
impl_datatype!(Usize, usize);
impl_datatype!(String, String);
impl_datatype!(OptBool, Option<bool>);
impl_datatype!(OptF32, Option<f32>);
impl_datatype!(OptF64, Option<f64>);
impl_datatype!(OptI32, Option<i32>);
impl_datatype!(OptI64, Option<i64>);
impl_datatype!(OptUsize, Option<usize>);
impl_datatype!(VecUsize, Vec<usize>);

#[cfg(feature = "time")]
impl<U: TimeUnitTrait> GetDataType for DateTime<U> {
    #[inline(always)]
    fn dtype() -> DataType {
        DataType::DateTime(U::unit())
    }
}

#[cfg(feature = "time")]
impl_datatype!(TimeDelta, TimeDelta);

impl<'a> GetDataType for &'a str {
    // type Physical = &'a str;
    #[inline(always)]
    fn dtype() -> DataType {
        DataType::Str
    }
}