perspective_viewer/config/
string_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
13use std::fmt::Display;
14use std::str::FromStr;
15
16use serde::{Deserialize, Serialize};
17use strum::EnumIter;
18use ts_rs::TS;
19
20#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, EnumIter, PartialEq, Serialize, TS)]
21pub enum StringColorMode {
22    #[default]
23    #[serde(rename = "none")]
24    None,
25
26    #[serde(rename = "foreground")]
27    Foreground,
28
29    #[serde(rename = "background")]
30    Background,
31
32    #[serde(rename = "series")]
33    Series,
34}
35
36impl Display for StringColorMode {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        let text = match self {
39            Self::None => "none",
40            Self::Foreground => "foreground",
41            Self::Background => "background",
42            Self::Series => "series",
43        };
44
45        write!(f, "{text}")
46    }
47}
48
49impl FromStr for StringColorMode {
50    type Err = String;
51
52    fn from_str(s: &str) -> Result<Self, Self::Err> {
53        match s {
54            "none" => Ok(Self::None),
55            "foreground" => Ok(Self::Foreground),
56            "background" => Ok(Self::Background),
57            "series" => Ok(Self::Series),
58            x => Err(format!("Unknown StringColorMode::{x}")),
59        }
60    }
61}
62
63impl StringColorMode {
64    pub fn is_none(&self) -> bool {
65        self == &Self::None
66    }
67}
68
69#[derive(Clone, Copy, Debug, Default, Deserialize, EnumIter, Eq, PartialEq, Serialize, TS)]
70pub enum FormatMode {
71    #[default]
72    #[serde(rename = "none")]
73    None,
74
75    #[serde(rename = "link")]
76    Link,
77
78    // #[serde(rename = "image")]
79    // Image,
80    #[serde(rename = "bold")]
81    Bold,
82
83    #[serde(rename = "italics")]
84    Italics,
85}
86
87impl Display for FormatMode {
88    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
89        let text = match self {
90            Self::None => "none",
91            Self::Link => "link",
92            // Self::Image => "image",
93            Self::Bold => "bold",
94            Self::Italics => "italics",
95        };
96
97        write!(f, "{text}")
98    }
99}
100
101impl FromStr for FormatMode {
102    type Err = String;
103
104    fn from_str(s: &str) -> Result<Self, Self::Err> {
105        match s {
106            "none" => Ok(Self::None),
107            "link" => Ok(Self::Link),
108            // "image" => Ok(Self::Image),
109            "bold" => Ok(Self::Bold),
110            "italics" => Ok(Self::Italics),
111            x => Err(format!("Unknown format mode {x}")),
112        }
113    }
114}
115
116impl FormatMode {
117    fn is_none(&self) -> bool {
118        self == &Self::None
119    }
120}
121
122#[derive(Debug, Clone, Default, Deserialize, Eq, PartialEq, Serialize, TS)]
123pub struct StringColumnStyleConfig {
124    #[serde(skip_serializing_if = "FormatMode::is_none")]
125    #[serde(default)]
126    pub format: FormatMode,
127
128    #[serde(skip_serializing_if = "StringColorMode::is_none")]
129    #[serde(default)]
130    pub string_color_mode: StringColorMode,
131
132    #[serde(skip_serializing_if = "Option::is_none")]
133    #[serde(default)]
134    pub color: Option<String>,
135}
136
137#[derive(Debug, Clone, Default, Deserialize, Eq, PartialEq, Serialize)]
138pub struct StringColumnStyleDefaultConfig {
139    pub color: String,
140}