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