Skip to main content

perspective_viewer/config/datetime_column_style/
custom.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
13use serde::{Deserialize, Serialize};
14use ts_rs::TS;
15
16use super::custom_format::CustomDatetimeFormat;
17
18#[derive(Clone, Debug, Default, Deserialize, Eq, PartialEq, Serialize, TS)]
19pub enum FormatUnit {
20    #[serde(rename = "custom")]
21    #[default]
22    FormatUnit,
23}
24
25const fn is_zero(x: &u32) -> bool {
26    *x == 0
27}
28
29const fn is_true(x: &bool) -> bool {
30    *x
31}
32
33const fn is_disabled(x: &CustomDatetimeFormat) -> bool {
34    matches!(x, CustomDatetimeFormat::Disabled)
35}
36
37const fn is_numeric(x: &CustomDatetimeFormat) -> bool {
38    matches!(x, CustomDatetimeFormat::Numeric)
39}
40
41const fn is_two_digit(x: &CustomDatetimeFormat) -> bool {
42    matches!(x, CustomDatetimeFormat::TwoDigit)
43}
44
45const fn second_default() -> CustomDatetimeFormat {
46    CustomDatetimeFormat::Numeric
47}
48
49const fn weekday_default() -> CustomDatetimeFormat {
50    CustomDatetimeFormat::Disabled
51}
52
53const fn year_default() -> CustomDatetimeFormat {
54    CustomDatetimeFormat::TwoDigit
55}
56
57const fn hour12_default() -> bool {
58    true
59}
60
61const fn numeric_default() -> u32 {
62    0
63}
64
65/// A datetime column's `date_format` in its `Custom` form: per-part
66/// `Intl.DateTimeFormatOptions` overrides, discriminated from the `Simple`
67/// preset form by the required `format: "custom"` key. Each part is a
68/// [`CustomDatetimeFormat`] variant valid for that `Intl` option;
69/// `"disabled"` omits the part from the formatted output entirely.
70#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, TS)]
71pub struct CustomDatetimeStyleConfig {
72    format: FormatUnit,
73
74    /// An IANA time zone name (e.g. `"America/New_York"`); defaults to the
75    /// browser's local time zone.
76    #[serde(default)]
77    #[serde(rename = "timeZone", skip_serializing_if = "Option::is_none")]
78    pub time_zone: Option<String>,
79
80    /// Sub-second digits to display, 0-3 (0 = none).
81    #[serde(
82        skip_serializing_if = "is_zero",
83        rename = "fractionalSecondDigits",
84        default = "numeric_default"
85    )]
86    pub fractional_seconds: u32,
87
88    /// Defaults to `"numeric"`.
89    #[serde(skip_serializing_if = "is_numeric", default = "second_default")]
90    pub second: CustomDatetimeFormat,
91
92    /// Defaults to `"numeric"`.
93    #[serde(skip_serializing_if = "is_numeric", default = "second_default")]
94    pub minute: CustomDatetimeFormat,
95
96    /// Defaults to `"numeric"`.
97    #[serde(skip_serializing_if = "is_numeric", default = "second_default")]
98    pub hour: CustomDatetimeFormat,
99
100    /// Defaults to `"numeric"`.
101    #[serde(skip_serializing_if = "is_numeric", default = "second_default")]
102    pub day: CustomDatetimeFormat,
103
104    /// A name form (`"long"`/`"short"`/`"narrow"`); defaults to
105    /// `"disabled"` (weekday not shown).
106    #[serde(skip_serializing_if = "is_disabled", default = "weekday_default")]
107    pub weekday: CustomDatetimeFormat,
108
109    /// Numeric or name form; defaults to `"numeric"`.
110    #[serde(skip_serializing_if = "is_numeric", default = "second_default")]
111    pub month: CustomDatetimeFormat,
112
113    /// Defaults to `"2-digit"`.
114    #[serde(skip_serializing_if = "is_two_digit", default = "year_default")]
115    pub year: CustomDatetimeFormat,
116
117    /// 12-hour clock; defaults to `true`.
118    #[serde(skip_serializing_if = "is_true", default = "hour12_default")]
119    pub hour12: bool,
120}
121
122impl Default for CustomDatetimeStyleConfig {
123    fn default() -> Self {
124        CustomDatetimeStyleConfig {
125            format: FormatUnit::FormatUnit,
126            fractional_seconds: 0,
127            time_zone: None,
128            second: CustomDatetimeFormat::Numeric,
129            minute: CustomDatetimeFormat::Numeric,
130            hour: CustomDatetimeFormat::Numeric,
131            weekday: CustomDatetimeFormat::Disabled,
132            day: CustomDatetimeFormat::Numeric,
133            month: CustomDatetimeFormat::Numeric,
134            year: CustomDatetimeFormat::TwoDigit,
135            hour12: true,
136        }
137    }
138}