perspective_viewer/config/
columns_config.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::collections::HashMap;
14
15use serde::{Deserialize, Serialize};
16use serde_with::skip_serializing_none;
17use ts_rs::TS;
18
19use super::{
20    CustomNumberFormatConfig, DatetimeColumnStyleConfig, DatetimeColumnStyleDefaultConfig,
21    NumberColumnStyleConfig, NumberColumnStyleDefaultConfig, StringColumnStyleConfig,
22    StringColumnStyleDefaultConfig,
23};
24
25/// The value de/serialized and stored in the viewer config.
26/// Also passed to the plugin via `plugin.save()`.
27#[skip_serializing_none]
28#[derive(Clone, Debug, Default, Deserialize, PartialEq, Serialize, TS)]
29pub struct ColumnConfigValues {
30    #[serde(default)]
31    #[serde(skip_serializing_if = "HashMap::is_empty")]
32    pub symbols: HashMap<String, String>,
33
34    #[serde(flatten)]
35    pub datagrid_number_style: NumberColumnStyleConfig,
36
37    #[serde(flatten)]
38    pub datagrid_string_style: StringColumnStyleConfig,
39
40    #[serde(flatten)]
41    pub datagrid_datetime_style: DatetimeColumnStyleConfig,
42
43    #[serde(default)]
44    #[serde(skip_serializing_if = "Option::is_none")]
45    pub number_format: Option<CustomNumberFormatConfig>,
46}
47
48#[derive(Debug)]
49pub enum ColumnConfigValueUpdate {
50    DatagridNumberStyle(Option<NumberColumnStyleConfig>),
51    DatagridStringStyle(Option<StringColumnStyleConfig>),
52    DatagridDatetimeStyle(Option<DatetimeColumnStyleConfig>),
53    Symbols(Option<HashMap<String, String>>),
54    CustomNumberStringFormat(Option<CustomNumberFormatConfig>),
55}
56
57impl ColumnConfigValues {
58    pub fn update(self, update: ColumnConfigValueUpdate) -> Self {
59        match update {
60            ColumnConfigValueUpdate::DatagridNumberStyle(update) => Self {
61                datagrid_number_style: update.unwrap_or_default(),
62                ..self
63            },
64            ColumnConfigValueUpdate::DatagridStringStyle(update) => Self {
65                datagrid_string_style: update.unwrap_or_default(),
66                ..self
67            },
68            ColumnConfigValueUpdate::DatagridDatetimeStyle(update) => Self {
69                datagrid_datetime_style: update.unwrap_or_default(),
70                ..self
71            },
72            ColumnConfigValueUpdate::Symbols(update) => Self {
73                symbols: update.unwrap_or_default(),
74                ..self
75            },
76            ColumnConfigValueUpdate::CustomNumberStringFormat(update) => Self {
77                number_format: update.filter(|x| x != &CustomNumberFormatConfig::default()),
78                ..self
79            },
80        }
81    }
82
83    pub fn is_empty(&self) -> bool {
84        self == &Self::default()
85    }
86}
87
88/// The controls returned by plugin.column_style_controls.
89///
90/// This is a way to fill out default values for the given controls.
91/// If a control is not given, it will not be rendered. I would like to
92/// eventually show these as inactive values.
93#[derive(Serialize, Deserialize, PartialEq, Debug, Default)]
94pub struct ColumnStyleOpts {
95    pub datagrid_number_style: Option<NumberColumnStyleDefaultConfig>,
96    pub datagrid_string_style: Option<StringColumnStyleDefaultConfig>,
97    pub datagrid_datetime_style: Option<DatetimeColumnStyleDefaultConfig>,
98    pub symbols: Option<KeyValueOpts>,
99    pub number_string_format: Option<bool>,
100}
101
102#[derive(Serialize, Deserialize, Debug, PartialEq, Clone, Copy)]
103#[serde(rename_all = "snake_case")]
104pub enum KvPairKeyValue {
105    Row,
106}
107
108#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
109#[serde(untagged)]
110pub enum KvPairKeys {
111    String(KvPairKeyValue),
112    Vec(Vec<String>),
113}
114
115#[derive(Serialize, Deserialize, Debug, PartialEq, Clone)]
116pub struct KeyValueOpts {
117    pub keys: KvPairKeys,
118    pub values: Vec<String>,
119}