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 color_mode;
14mod custom;
15mod custom_format;
16mod simple;
17mod simple_format;
18
19pub use color_mode::*;
20pub use custom::*;
21pub use custom_format::*;
22use serde::{Deserialize, Serialize};
23pub use simple::*;
24pub use simple_format::*;
25use ts_rs::TS;
26
27/// `Simple` case has all default-able keys and must be last!
28#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, TS)]
29// #[serde(tag = "format", content = "date_format")]
30#[serde(untagged)]
31pub enum DatetimeFormatType {
32    // #[serde(rename = "custom")]
33    Custom(CustomDatetimeStyleConfig),
34    Simple(SimpleDatetimeStyleConfig),
35}
36
37impl Default for DatetimeFormatType {
38    fn default() -> Self {
39        Self::Simple(SimpleDatetimeStyleConfig::default())
40    }
41}
42
43impl DatetimeFormatType {
44    fn is_simple(&self) -> bool {
45        self == &Self::Simple(SimpleDatetimeStyleConfig::default())
46    }
47
48    pub fn time_zone(&self) -> &Option<String> {
49        match self {
50            DatetimeFormatType::Custom(x) => &x.time_zone,
51            DatetimeFormatType::Simple(x) => &x.time_zone,
52        }
53    }
54
55    pub fn time_zone_mut(&mut self) -> &mut Option<String> {
56        match self {
57            DatetimeFormatType::Custom(x) => &mut x.time_zone,
58            DatetimeFormatType::Simple(x) => &mut x.time_zone,
59        }
60    }
61}
62
63/// A model for the JSON serialized style configuration for a column of type
64/// `datetime`.
65#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, TS)]
66// #[derive(WasmDescribe!, FromWasmAbi!)]
67pub struct DatetimeColumnStyleConfig {
68    #[serde(default)]
69    #[serde(skip_serializing_if = "DatetimeFormatType::is_simple")]
70    pub date_format: DatetimeFormatType,
71
72    #[serde(default)]
73    #[serde(skip_serializing_if = "DatetimeColorMode::is_none")]
74    pub datetime_color_mode: DatetimeColorMode,
75
76    #[serde(default)]
77    #[serde(skip_serializing_if = "Option::is_none")]
78    #[ts(skip)]
79    pub color: Option<String>,
80}
81
82impl Default for DatetimeColumnStyleConfig {
83    fn default() -> Self {
84        Self {
85            date_format: DatetimeFormatType::Simple(SimpleDatetimeStyleConfig {
86                time_zone: Default::default(),
87                date_style: SimpleDatetimeFormat::Short,
88                time_style: SimpleDatetimeFormat::Medium,
89            }),
90            datetime_color_mode: Default::default(),
91            color: Default::default(),
92        }
93    }
94}
95
96#[derive(Clone, Default, Deserialize, Eq, PartialEq, Serialize, Debug)]
97pub struct DatetimeColumnStyleDefaultConfig {
98    pub color: String,
99}