elasticsearch_dsl/search/params/
date.rs

1use chrono::{DateTime, Utc};
2use std::time::SystemTime;
3
4/// [`DateTime<Utc>`] type alias
5pub type ChronoTime = DateTime<Utc>;
6
7/// Time variants to serialize
8#[derive(Clone, Copy, Serialize)]
9#[serde(untagged)]
10pub enum Date {
11    /// System time
12    System(SystemTime),
13
14    /// Chrono time
15    Chrono(ChronoTime),
16}
17
18impl std::fmt::Debug for Date {
19    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
20        match self {
21            Self::System(value) => value.fmt(f),
22            Self::Chrono(value) => value.fmt(f),
23        }
24    }
25}
26
27impl From<SystemTime> for Date {
28    fn from(value: SystemTime) -> Self {
29        Self::System(value)
30    }
31}
32
33impl From<ChronoTime> for Date {
34    fn from(value: ChronoTime) -> Self {
35        Self::Chrono(value)
36    }
37}
38
39impl From<&SystemTime> for Date {
40    fn from(value: &SystemTime) -> Self {
41        Self::System(*value)
42    }
43}
44
45impl From<&ChronoTime> for Date {
46    fn from(value: &ChronoTime) -> Self {
47        Self::Chrono(*value)
48    }
49}
50
51impl PartialEq for Date {
52    fn eq(&self, other: &Self) -> bool {
53        match (self, other) {
54            (Date::System(s), Date::System(o)) => s.eq(o),
55            (Date::System(s), Date::Chrono(o)) => ChronoTime::from(*s).eq(o),
56            (Date::Chrono(s), Date::System(o)) => s.eq(&ChronoTime::from(*o)),
57            (Date::Chrono(s), Date::Chrono(o)) => s.eq(o),
58        }
59    }
60}
61
62impl PartialEq<SystemTime> for Date {
63    fn eq(&self, other: &SystemTime) -> bool {
64        match self {
65            Self::System(s) => s.eq(other),
66            Self::Chrono(s) => s.eq(&ChronoTime::from(*other)),
67        }
68    }
69}
70
71impl PartialEq<ChronoTime> for Date {
72    fn eq(&self, other: &ChronoTime) -> bool {
73        match self {
74            Self::Chrono(s) => s.eq(other),
75            Self::System(s) => ChronoTime::from(*s).eq(other),
76        }
77    }
78}
79
80impl Eq for Date {}
81
82impl PartialOrd for Date {
83    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
84        Some(self.cmp(other))
85    }
86}
87
88impl PartialOrd<SystemTime> for Date {
89    fn partial_cmp(&self, other: &SystemTime) -> Option<std::cmp::Ordering> {
90        match self {
91            Date::System(s) => s.partial_cmp(other),
92            Date::Chrono(s) => s.partial_cmp(&ChronoTime::from(*other)),
93        }
94    }
95}
96
97impl PartialOrd<ChronoTime> for Date {
98    fn partial_cmp(&self, other: &ChronoTime) -> Option<std::cmp::Ordering> {
99        match self {
100            Self::Chrono(s) => s.partial_cmp(other),
101            Self::System(s) => ChronoTime::from(*s).partial_cmp(other),
102        }
103    }
104}
105
106impl Ord for Date {
107    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
108        match (self, other) {
109            (Date::System(s), Date::System(o)) => s.cmp(o),
110            (Date::System(s), Date::Chrono(o)) => ChronoTime::from(*s).cmp(o),
111            (Date::Chrono(s), Date::System(o)) => s.cmp(&ChronoTime::from(*o)),
112            (Date::Chrono(s), Date::Chrono(o)) => s.cmp(o),
113        }
114    }
115}