Skip to main content

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