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