perspective_viewer/config/
datetime_column_style.rs1mod part_format;
14mod simple_format;
15
16pub use part_format::*;
17use serde::{Deserialize, Serialize};
18pub use simple_format::*;
19use ts_rs::TS;
20
21#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize, TS)]
25#[serde(rename_all = "camelCase", deny_unknown_fields)]
26pub struct DatetimeFormat {
27 #[serde(default, skip_serializing_if = "Option::is_none")]
29 #[ts(optional)]
30 pub time_zone: Option<String>,
31
32 #[serde(default, skip_serializing_if = "Option::is_none")]
34 #[ts(optional)]
35 pub hour12: Option<bool>,
36
37 #[serde(default, skip_serializing_if = "Option::is_none")]
39 #[ts(optional)]
40 pub date_style: Option<SimpleDatetimeFormat>,
41
42 #[serde(default, skip_serializing_if = "Option::is_none")]
44 #[ts(optional)]
45 pub time_style: Option<SimpleDatetimeFormat>,
46
47 #[serde(default, skip_serializing_if = "Option::is_none")]
49 #[ts(optional)]
50 pub weekday: Option<DatetimeTextPart>,
51
52 #[serde(default, skip_serializing_if = "Option::is_none")]
54 #[ts(optional)]
55 pub year: Option<DatetimeNumericPart>,
56
57 #[serde(default, skip_serializing_if = "Option::is_none")]
59 #[ts(optional)]
60 pub month: Option<DatetimeMonthPart>,
61
62 #[serde(default, skip_serializing_if = "Option::is_none")]
64 #[ts(optional)]
65 pub day: Option<DatetimeNumericPart>,
66
67 #[serde(default, skip_serializing_if = "Option::is_none")]
69 #[ts(optional)]
70 pub hour: Option<DatetimeNumericPart>,
71
72 #[serde(default, skip_serializing_if = "Option::is_none")]
74 #[ts(optional)]
75 pub minute: Option<DatetimeNumericPart>,
76
77 #[serde(default, skip_serializing_if = "Option::is_none")]
79 #[ts(optional)]
80 pub second: Option<DatetimeNumericPart>,
81
82 #[serde(default, skip_serializing_if = "Option::is_none")]
84 #[ts(optional)]
85 pub fractional_second_digits: Option<FractionalSecondDigits>,
86}
87
88impl DatetimeFormat {
89 pub fn base(with_time: bool) -> Self {
91 Self {
92 date_style: Some(SimpleDatetimeFormat::Short),
93 time_style: with_time.then_some(SimpleDatetimeFormat::Medium),
94 ..Self::default()
95 }
96 }
97
98 pub fn parts_base(with_time: bool) -> Self {
100 let time = with_time.then_some(DatetimeNumericPart::Numeric);
101 Self {
102 year: Some(DatetimeNumericPart::TwoDigit),
103 month: Some(DatetimeMonthPart::Numeric),
104 day: Some(DatetimeNumericPart::Numeric),
105 hour: time,
106 minute: time,
107 second: time,
108 ..Self::default()
109 }
110 }
111
112 pub fn has_preset(&self) -> bool {
114 self.date_style.is_some() || self.time_style.is_some()
115 }
116
117 pub fn has_parts(&self) -> bool {
119 self.weekday.is_some()
120 || self.year.is_some()
121 || self.month.is_some()
122 || self.day.is_some()
123 || self.hour.is_some()
124 || self.minute.is_some()
125 || self.second.is_some()
126 || self.fractional_second_digits.is_some()
127 }
128
129 pub fn has_shape(&self) -> bool {
131 self.has_preset() || self.has_parts()
132 }
133
134 pub fn is_empty(&self) -> bool {
136 *self == Self::default()
137 }
138
139 pub fn shape(&self) -> Self {
141 Self {
142 time_zone: None,
143 hour12: None,
144 ..self.clone()
145 }
146 }
147
148 pub fn with_shape(&self, shape: &Self) -> Self {
150 Self {
151 time_zone: self.time_zone.clone(),
152 hour12: self.hour12,
153 ..shape.shape()
154 }
155 }
156
157 pub fn overlay(&self, upper: &Self) -> Self {
161 let shape = if upper.has_shape() { upper } else { self };
162 Self {
163 time_zone: upper.time_zone.clone().or_else(|| self.time_zone.clone()),
164 hour12: upper.hour12.or(self.hour12),
165 ..shape.shape()
166 }
167 }
168
169 pub fn diff(&self, inherited: &Self) -> Self {
172 let shape = if self.has_shape() && self.shape() != inherited.shape() {
173 self.shape()
174 } else {
175 Self::default()
176 };
177
178 Self {
179 time_zone: self
180 .time_zone
181 .clone()
182 .filter(|tz| Some(tz) != inherited.time_zone.as_ref()),
183 hour12: self.hour12.filter(|h| Some(*h) != inherited.hour12),
184 ..shape
185 }
186 }
187
188 pub fn validate(&self) -> Result<(), String> {
191 if self.has_preset() && self.has_parts() {
192 Err(
193 "`dateStyle`/`timeStyle` cannot be combined with `weekday`, `year`, `month`, \
194 `day`, `hour`, `minute`, `second` or `fractionalSecondDigits`"
195 .to_owned(),
196 )
197 } else {
198 Ok(())
199 }
200 }
201}
202
203#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize, TS)]
205pub struct DatetimeColumnStyleConfig {
206 #[serde(default)]
207 #[serde(skip_serializing_if = "Option::is_none")]
208 #[ts(optional, as = "Option<_>")]
209 pub date_format: Option<DatetimeFormat>,
210}