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