dolphindb/types/
temporal.rs1use super::DataType;
2use crate::error::Error;
3
4use chrono::naive::{NaiveDate, NaiveDateTime, NaiveTime};
5use std::any::type_name;
6
7macro_rules! temporal_impl {
8 ($raw_type:tt, $struct_name:ident, $enum_name:ident) => {
9 #[derive(Default, Clone, Copy, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
10 pub struct $struct_name(pub(crate) Option<$raw_type>);
11
12 impl $struct_name {
13 pub const DATA_BYTE: DataType = DataType::$enum_name;
14
15 pub(crate) fn new(val: $raw_type) -> Self {
16 Self(Some(val))
17 }
18
19 pub const fn data_type() -> DataType {
20 Self::DATA_BYTE
21 }
22
23 pub const fn is_null(&self) -> bool {
24 self.0.is_none()
25 }
26
27 pub fn into_inner(self) -> Option<$raw_type> {
28 self.0
29 }
30 }
31 };
32
33 ($(($raw_type:tt, $struct_name:ident, $enum_name:ident)), *) => {
34 $(
35 temporal_impl!($raw_type, $struct_name, $enum_name);
36 )*
37 };
38}
39
40macro_rules! as_ref_impl {
41 ($raw_type:tt, $struct_name:ident, $enum_name:ident) => {
42 impl AsRef<Option<$raw_type>> for $struct_name {
43 fn as_ref(&self) -> &Option<$raw_type> {
44 &self.0
45 }
46 }
47 };
48
49 ($(($raw_type:tt, $struct_name:ident, $enum_name:ident)), *) => {
50 $(
51 as_ref_impl!($raw_type, $struct_name, $enum_name);
52 )*
53 };
54}
55
56macro_rules! from_raw_impl {
57 ($raw_type:tt, $struct_name:ident) => {
58 impl TryFrom<$struct_name> for $raw_type {
59 type Error = Error;
60
61 fn try_from(value: $struct_name) -> Result<Self, Self::Error> {
62 match value.into_inner() {
63 Some(value) => Ok(value),
64 _ => Err(Error::InvalidConvert {
65 from: "null".into(),
66 to: type_name::<$raw_type>().to_string(),
67 }),
68 }
69 }
70 }
71 impl From<$raw_type> for $struct_name {
72 fn from(value: $raw_type) -> Self {
73 Self(Some(value))
74 }
75 }
76 };
77
78 ($(($raw_type:tt, $struct_name:ident, $enum_name:ident)), *) => {
79 $(
80 from_raw_impl!($raw_type, $struct_name);
81 )*
82 };
83}
84
85macro_rules! for_all_types {
86 ($macro:tt) => {
87 $macro!(
88 (NaiveDate, Date, Date),
89 (NaiveDate, Month, Month),
90 (NaiveTime, Time, Time),
91 (NaiveTime, Minute, Minute),
92 (NaiveTime, Second, Second),
93 (NaiveDateTime, DateTime, DateTime),
94 (NaiveDateTime, Timestamp, Timestamp),
95 (NaiveTime, NanoTime, NanoTime),
96 (NaiveDateTime, NanoTimestamp, NanoTimestamp),
97 (NaiveDateTime, DateHour, DateHour)
98 );
99 };
100}
101
102for_all_types!(temporal_impl);
103
104for_all_types!(as_ref_impl);
105
106for_all_types!(from_raw_impl);