perspective_viewer/config/datetime_column_style/
custom_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;
14use std::str::FromStr;
15
16use serde::{Deserialize, Serialize};
17use ts_rs::TS;
18
19#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize, TS)]
20pub enum CustomDatetimeFormat {
21    #[serde(rename = "long")]
22    Long,
23
24    #[serde(rename = "short")]
25    Short,
26
27    #[serde(rename = "narrow")]
28    Narrow,
29
30    #[serde(rename = "numeric")]
31    Numeric,
32
33    #[serde(rename = "2-digit")]
34    TwoDigit,
35
36    #[serde(rename = "disabled")]
37    Disabled,
38}
39
40impl CustomDatetimeFormat {
41    pub const fn values() -> &'static [Self] {
42        &[
43            Self::Long,
44            Self::Short,
45            Self::Narrow,
46            Self::Numeric,
47            Self::TwoDigit,
48            Self::Disabled,
49        ]
50    }
51
52    pub fn values_1() -> &'static [Self] {
53        &[Self::Numeric, Self::TwoDigit, Self::Disabled]
54    }
55
56    pub fn values_2() -> &'static [Self] {
57        &[Self::Long, Self::Narrow, Self::Short, Self::Disabled]
58    }
59}
60
61impl Display for CustomDatetimeFormat {
62    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
63        let text = match self {
64            Self::Long => "long",
65            Self::Short => "short",
66            Self::Narrow => "narrow",
67            Self::Numeric => "numeric",
68            Self::TwoDigit => "2-digit",
69            Self::Disabled => "disabled",
70        };
71
72        write!(f, "{}", text)
73    }
74}
75
76impl FromStr for CustomDatetimeFormat {
77    type Err = String;
78
79    fn from_str(s: &str) -> Result<Self, Self::Err> {
80        match s {
81            "long" => Ok(Self::Long),
82            "short" => Ok(Self::Short),
83            "narrow" => Ok(Self::Narrow),
84            "numeric" => Ok(Self::Numeric),
85            "2-digit" => Ok(Self::TwoDigit),
86            "disabled" => Ok(Self::Disabled),
87            x => Err(format!("Unknown DatetimeFormat::{}", x)),
88        }
89    }
90}