Skip to main content

perspective_viewer/config/
datetime_column_style.rs

1// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
2// ┃ ██████ ██████ ██████       █      █      █      █      █ █▄  ▀███ █       ┃
3// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█  ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄  ▀█ █ ▀▀▀▀▀ ┃
4// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄   █ ▄▄▄▄▄ ┃
5// ┃ █      ██████ █  ▀█▄       █ ██████      █      ███▌▐███ ███████▄ █       ┃
6// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
7// ┃ Copyright (c) 2017, the Perspective Authors.                              ┃
8// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
9// ┃ This file is part of the Perspective library, distributed under the terms ┃
10// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
11// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
12
13mod part_format;
14mod simple_format;
15
16pub use part_format::*;
17use serde::{Deserialize, Serialize};
18pub use simple_format::*;
19use ts_rs::TS;
20
21/// A datetime column's `date_format`, a subset of `Intl.DateTimeFormatOptions`
22/// whose display fields inherit as one group and whose `timeZone` and `hour12`
23/// inherit individually.
24#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize, TS)]
25#[serde(rename_all = "camelCase", deny_unknown_fields)]
26pub struct DatetimeFormat {
27    /// An IANA time zone name such as `"America/New_York"`.
28    #[serde(default, skip_serializing_if = "Option::is_none")]
29    #[ts(optional)]
30    pub time_zone: Option<String>,
31
32    /// Whether hours use a 12-hour clock.
33    #[serde(default, skip_serializing_if = "Option::is_none")]
34    #[ts(optional)]
35    pub hour12: Option<bool>,
36
37    /// The date preset, which excludes every per-part field.
38    #[serde(default, skip_serializing_if = "Option::is_none")]
39    #[ts(optional)]
40    pub date_style: Option<SimpleDatetimeFormat>,
41
42    /// The time preset, which excludes every per-part field.
43    #[serde(default, skip_serializing_if = "Option::is_none")]
44    #[ts(optional)]
45    pub time_style: Option<SimpleDatetimeFormat>,
46
47    /// The weekday's form.
48    #[serde(default, skip_serializing_if = "Option::is_none")]
49    #[ts(optional)]
50    pub weekday: Option<DatetimeTextPart>,
51
52    /// The year's form.
53    #[serde(default, skip_serializing_if = "Option::is_none")]
54    #[ts(optional)]
55    pub year: Option<DatetimeNumericPart>,
56
57    /// The month's form.
58    #[serde(default, skip_serializing_if = "Option::is_none")]
59    #[ts(optional)]
60    pub month: Option<DatetimeMonthPart>,
61
62    /// The day's form.
63    #[serde(default, skip_serializing_if = "Option::is_none")]
64    #[ts(optional)]
65    pub day: Option<DatetimeNumericPart>,
66
67    /// The hour's form.
68    #[serde(default, skip_serializing_if = "Option::is_none")]
69    #[ts(optional)]
70    pub hour: Option<DatetimeNumericPart>,
71
72    /// The minute's form.
73    #[serde(default, skip_serializing_if = "Option::is_none")]
74    #[ts(optional)]
75    pub minute: Option<DatetimeNumericPart>,
76
77    /// The second's form.
78    #[serde(default, skip_serializing_if = "Option::is_none")]
79    #[ts(optional)]
80    pub second: Option<DatetimeNumericPart>,
81
82    /// Sub-second digits shown.
83    #[serde(default, skip_serializing_if = "Option::is_none")]
84    #[ts(optional)]
85    pub fractional_second_digits: Option<FractionalSecondDigits>,
86}
87
88impl DatetimeFormat {
89    /// The viewer's own default, below every plugin and override layer.
90    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    /// The per-part shape a switch from preset to per-part display starts from.
99    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    /// Whether this format sets `dateStyle` or `timeStyle`.
113    pub fn has_preset(&self) -> bool {
114        self.date_style.is_some() || self.time_style.is_some()
115    }
116
117    /// Whether this format sets any per-part display field.
118    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    /// Whether this format sets any display field.
130    pub fn has_shape(&self) -> bool {
131        self.has_preset() || self.has_parts()
132    }
133
134    /// Whether this format sets nothing.
135    pub fn is_empty(&self) -> bool {
136        *self == Self::default()
137    }
138
139    /// This format's display fields alone.
140    pub fn shape(&self) -> Self {
141        Self {
142            time_zone: None,
143            hour12: None,
144            ..self.clone()
145        }
146    }
147
148    /// This format with its display fields replaced by `shape`'s.
149    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    /// `upper` layered over this format: the display fields of whichever sets
158    /// any, `upper` first, and each of `timeZone` and `hour12` from whichever
159    /// sets it.
160    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    /// The fields of this format that differ from `inherited`, keeping or
170    /// dropping the display fields as one group.
171    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    /// `Err` when this format sets both preset and per-part display fields,
189    /// which `Intl` rejects.
190    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/// The `date_format` key of a datetime column's config entry.
204#[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}