/
string_column_style.rs
139 lines (115 loc) · 4.87 KB
/
string_column_style.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// ┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓
// ┃ ██████ ██████ ██████ █ █ █ █ █ █▄ ▀███ █ ┃
// ┃ ▄▄▄▄▄█ █▄▄▄▄▄ ▄▄▄▄▄█ ▀▀▀▀▀█▀▀▀▀▀ █ ▀▀▀▀▀█ ████████▌▐███ ███▄ ▀█ █ ▀▀▀▀▀ ┃
// ┃ █▀▀▀▀▀ █▀▀▀▀▀ █▀██▀▀ ▄▄▄▄▄ █ ▄▄▄▄▄█ ▄▄▄▄▄█ ████████▌▐███ █████▄ █ ▄▄▄▄▄ ┃
// ┃ █ ██████ █ ▀█▄ █ ██████ █ ███▌▐███ ███████▄ █ ┃
// ┣━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┫
// ┃ Copyright (c) 2017, the Perspective Authors. ┃
// ┃ ╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌╌ ┃
// ┃ This file is part of the Perspective library, distributed under the terms ┃
// ┃ of the [Apache License 2.0](https://www.apache.org/licenses/LICENSE-2.0). ┃
// ┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛
use std::fmt::Display;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use strum::EnumIter;
#[derive(Clone, Copy, Debug, Default, Deserialize, Eq, EnumIter, PartialEq, Serialize)]
pub enum StringColorMode {
#[default]
#[serde(rename = "none")]
None,
#[serde(rename = "foreground")]
Foreground,
#[serde(rename = "background")]
Background,
#[serde(rename = "series")]
Series,
}
impl Display for StringColorMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let text = match self {
Self::None => "none",
Self::Foreground => "foreground",
Self::Background => "background",
Self::Series => "series",
};
write!(f, "{}", text)
}
}
impl FromStr for StringColorMode {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" => Ok(Self::None),
"foreground" => Ok(Self::Foreground),
"background" => Ok(Self::Background),
"series" => Ok(Self::Series),
x => Err(format!("Unknown StringColorMode::{}", x)),
}
}
}
impl StringColorMode {
pub fn is_none(&self) -> bool {
self == &Self::None
}
}
#[derive(Clone, Copy, Debug, Default, Deserialize, EnumIter, Eq, PartialEq, Serialize)]
pub enum FormatMode {
#[default]
#[serde(rename = "none")]
None,
#[serde(rename = "link")]
Link,
// #[serde(rename = "image")]
// Image,
#[serde(rename = "bold")]
Bold,
#[serde(rename = "italics")]
Italics,
}
impl Display for FormatMode {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let text = match self {
Self::None => "none",
Self::Link => "link",
// Self::Image => "image",
Self::Bold => "bold",
Self::Italics => "italics",
};
write!(f, "{}", text)
}
}
impl FromStr for FormatMode {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"none" => Ok(Self::None),
"link" => Ok(Self::Link),
// "image" => Ok(Self::Image),
"bold" => Ok(Self::Bold),
"italics" => Ok(Self::Italics),
x => Err(format!("Unknown format mode {}", x)),
}
}
}
impl FormatMode {
fn is_none(&self) -> bool {
self == &Self::None
}
}
#[derive(Debug, Clone, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct StringColumnStyleConfig {
#[serde(skip_serializing_if = "FormatMode::is_none")]
#[serde(default)]
pub format: FormatMode,
#[serde(skip_serializing_if = "StringColorMode::is_none")]
#[serde(default)]
pub string_color_mode: StringColorMode,
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(default)]
pub color: Option<String>,
}
#[derive(Debug, Clone, Default, Deserialize, Eq, PartialEq, Serialize)]
pub struct StringColumnStyleDefaultConfig {
pub color: String,
}