Skip to main content

perspective_viewer/config/datetime_column_style/
part_format.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 std::fmt::Display;
14
15use serde::{Deserialize, Deserializer, Serialize, Serializer};
16use strum::EnumIter;
17use ts_rs::TS;
18
19/// A date or time part's numeric form.
20#[derive(Clone, Copy, Debug, Deserialize, EnumIter, Eq, PartialEq, Serialize, TS)]
21pub enum DatetimeNumericPart {
22    #[serde(rename = "numeric")]
23    Numeric,
24
25    #[serde(rename = "2-digit")]
26    TwoDigit,
27}
28
29impl Display for DatetimeNumericPart {
30    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
31        f.write_str(match self {
32            Self::Numeric => "numeric",
33            Self::TwoDigit => "2-digit",
34        })
35    }
36}
37
38/// The month's numeric or named form.
39#[derive(Clone, Copy, Debug, Deserialize, EnumIter, Eq, PartialEq, Serialize, TS)]
40pub enum DatetimeMonthPart {
41    #[serde(rename = "numeric")]
42    Numeric,
43
44    #[serde(rename = "2-digit")]
45    TwoDigit,
46
47    #[serde(rename = "long")]
48    Long,
49
50    #[serde(rename = "short")]
51    Short,
52
53    #[serde(rename = "narrow")]
54    Narrow,
55}
56
57impl Display for DatetimeMonthPart {
58    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
59        f.write_str(match self {
60            Self::Numeric => "numeric",
61            Self::TwoDigit => "2-digit",
62            Self::Long => "long",
63            Self::Short => "short",
64            Self::Narrow => "narrow",
65        })
66    }
67}
68
69/// The weekday's named form.
70#[derive(Clone, Copy, Debug, Deserialize, EnumIter, Eq, PartialEq, Serialize, TS)]
71pub enum DatetimeTextPart {
72    #[serde(rename = "long")]
73    Long,
74
75    #[serde(rename = "short")]
76    Short,
77
78    #[serde(rename = "narrow")]
79    Narrow,
80}
81
82impl Display for DatetimeTextPart {
83    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
84        f.write_str(match self {
85            Self::Long => "long",
86            Self::Short => "short",
87            Self::Narrow => "narrow",
88        })
89    }
90}
91
92/// The number of sub-second digits shown, from 1 to 3.
93#[derive(Clone, Copy, Debug, Eq, PartialEq, TS)]
94#[ts(type = "1 | 2 | 3")]
95pub struct FractionalSecondDigits(u8);
96
97impl FractionalSecondDigits {
98    /// The digit count, or `None` outside 1 to 3.
99    pub fn new(digits: u8) -> Option<Self> {
100        (1..=3).contains(&digits).then_some(Self(digits))
101    }
102
103    /// The digit count.
104    pub fn get(self) -> u8 {
105        self.0
106    }
107}
108
109impl Serialize for FractionalSecondDigits {
110    fn serialize<S: Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
111        serializer.serialize_u8(self.0)
112    }
113}
114
115impl<'de> Deserialize<'de> for FractionalSecondDigits {
116    fn deserialize<D: Deserializer<'de>>(deserializer: D) -> Result<Self, D::Error> {
117        let digits = u8::deserialize(deserializer)?;
118        Self::new(digits).ok_or_else(|| {
119            serde::de::Error::custom(format!(
120                "`fractionalSecondDigits` must be 1, 2 or 3, not {digits}"
121            ))
122        })
123    }
124}