perspective_client/config/viewer/optional_update.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 serde::{Deserialize, Deserializer, Serialize};
14use serde_json::Value;
15use ts_rs::TS;
16
17use super::{ColumnConfigMap, TableRef};
18
19/// One field of a field-wise update: absent leaves the field unchanged,
20/// `null` resets it to its default and a value replaces it.
21#[derive(Clone, Debug, Default, Serialize, PartialEq, TS)]
22#[serde(untagged)]
23pub enum OptionalUpdate<T: Clone> {
24 /// The field was `null`: reset it to its default.
25 #[ts(skip)]
26 SetDefault,
27
28 /// The field was absent: leave it unchanged.
29 #[default]
30 Missing,
31
32 /// The field was present: replace it with this value.
33 Update(T),
34}
35
36/// A `plugin` update.
37pub type PluginUpdate = OptionalUpdate<String>;
38
39/// A `settings` update.
40pub type SettingsUpdate = OptionalUpdate<bool>;
41
42/// A `theme` update.
43pub type ThemeUpdate = OptionalUpdate<String>;
44
45/// A `title` update.
46pub type TitleUpdate = OptionalUpdate<String>;
47
48/// A `table` update.
49pub type TableUpdate = OptionalUpdate<TableRef>;
50
51/// A `version` update.
52pub type VersionUpdate = OptionalUpdate<String>;
53
54/// A `columns_config` update.
55pub type ColumnConfigUpdate = OptionalUpdate<ColumnConfigMap>;
56
57/// A `plugin_config` update.
58pub type PluginConfigUpdate = OptionalUpdate<serde_json::Map<String, Value>>;
59
60impl<T: Clone> From<Option<T>> for OptionalUpdate<T> {
61 fn from(opt: Option<T>) -> Self {
62 match opt {
63 Some(v) => Self::Update(v),
64 None => Self::SetDefault,
65 }
66 }
67}
68
69impl<'a, T> Deserialize<'a> for OptionalUpdate<T>
70where
71 T: Deserialize<'a> + Clone,
72{
73 fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
74 where
75 D: Deserializer<'a>,
76 {
77 Option::deserialize(deserializer).map(Into::into)
78 }
79}