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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use {
super::ScrollBarStyleDef,
crate::{
minimad::Alignment,
parse_compound_style,
parse_line_style,
parse_styled_char,
LineStyle,
MadSkin,
OrderedItemStyle,
TableBorderChars,
ATTRIBUTES,
},
serde::{
de,
ser::SerializeMap,
Deserialize,
Serialize,
Serializer,
},
std::fmt,
};
impl<'de> de::Deserialize<'de> for MadSkin {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: de::Deserializer<'de>,
{
struct SkinVisitor;
impl<'de> de::Visitor<'de> for SkinVisitor {
type Value = MadSkin;
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("MadSkin")
}
fn visit_map<V>(self, mut map: V) -> Result<Self::Value, V::Error>
where
V: de::MapAccess<'de>,
{
let mut skin = MadSkin::default();
while let Some(key) = map.next_key::<String>()? {
match key.as_str() {
// inline styles
"bold" => {
let value = map.next_value::<String>()?;
let cs = parse_compound_style(&value).map_err(de::Error::custom)?;
skin.bold = cs;
}
"italic" => {
let value = map.next_value::<String>()?;
let cs = parse_compound_style(&value).map_err(de::Error::custom)?;
skin.italic = cs;
}
"strikeout" => {
let value = map.next_value::<String>()?;
let cs = parse_compound_style(&value).map_err(de::Error::custom)?;
skin.strikeout = cs;
}
"inline_code" | "inline-code" => {
let value = map.next_value::<String>()?;
let cs = parse_compound_style(&value).map_err(de::Error::custom)?;
skin.inline_code = cs;
}
"ellipsis" => {
let value = map.next_value::<String>()?;
let cs = parse_compound_style(&value).map_err(de::Error::custom)?;
skin.ellipsis = cs;
}
// marker chars
"bullet" => {
let value = map.next_value::<String>()?;
let sc = parse_styled_char(&value, '*').map_err(de::Error::custom)?;
skin.bullet = sc;
}
"quote_mark" | "quote" | "quote-mark" => {
let value = map.next_value::<String>()?;
let sc = parse_styled_char(&value, '*').map_err(de::Error::custom)?;
skin.quote_mark = sc;
}
"horizontal_rule" | "horizontal-rule" | "rule" => {
let value = map.next_value::<String>()?;
let sc = parse_styled_char(&value, '*').map_err(de::Error::custom)?;
skin.horizontal_rule = sc;
}
// scrollbar
"scrollbar" => {
let def: ScrollBarStyleDef = map.next_value()?;
skin.scrollbar = def.into_scrollbar_style();
}
// line styles
"paragraph" => {
let value = map.next_value::<String>()?;
let ls = parse_line_style(&value).map_err(de::Error::custom)?;
skin.paragraph = ls;
}
"code_block" | "code-block" => {
let value = map.next_value::<String>()?;
let ls = parse_line_style(&value).map_err(de::Error::custom)?;
skin.code_block = ls;
}
"table" => {
let value = map.next_value::<String>()?;
let ls = parse_line_style(&value).map_err(de::Error::custom)?;
skin.table = ls;
}
// headers
"headers" => match map.next_value::<HeadersStyleInfo>()? {
HeadersStyleInfo::Add(ls) => {
for h in &mut skin.headers {
if let Some(fg) = ls.compound_style.get_fg() {
h.compound_style.set_fg(fg);
}
if let Some(bg) = ls.compound_style.get_bg() {
h.compound_style.set_bg(bg);
}
for &attr in ATTRIBUTES {
if ls.compound_style.has_attr(attr) {
h.compound_style.add_attr(attr);
}
}
if ls.align != Alignment::Unspecified {
h.align = ls.align;
}
}
}
HeadersStyleInfo::Levels(mut vls) => {
for (lvl, h) in vls.drain(..).enumerate() {
if lvl < skin.headers.len() {
skin.headers[lvl] = h;
}
}
}
},
// ordered list item style
"ordered_items" | "ordered-items" => {
let styles = map.next_value::<Vec<OrderedItemStyle>>()?;
for (lvl, style) in styles.into_iter().enumerate() {
if let Some(skin_style) = skin.ordered_item_styles.get_mut(lvl) {
*skin_style = style;
}
}
}
// table border chars
// There's currently no way to allow custom table border
// chars. It would require a change in MadSkin: either
// make it require a lifetime, or use a Cow for the border
// chars
"table_border_chars" | "table-border-chars" => {
let key = map.next_value::<String>()?;
if let Some(chars) = TableBorderChars::by_key(&key) {
skin.table_border_chars = chars;
}
}
_ => {
let _ = map.next_value::<String>()?;
println!("unknown key: {key}");
}
}
}
Ok(skin)
}
}
deserializer.deserialize_map(SkinVisitor {})
}
}
impl Serialize for MadSkin {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
let mut skin = serializer.serialize_map(None)?;
// inline styles
skin.serialize_entry("bold", &self.bold)?;
skin.serialize_entry("italic", &self.italic)?;
skin.serialize_entry("strikeout", &self.strikeout)?;
skin.serialize_entry("inline_code", &self.inline_code)?;
skin.serialize_entry("ellipsis", &self.ellipsis)?;
// marker chars
skin.serialize_entry("bullet", &self.bullet)?;
skin.serialize_entry("quote", &self.quote_mark)?;
skin.serialize_entry("horizontal_rule", &self.horizontal_rule)?;
// scrollbar
let def: ScrollBarStyleDef = (&self.scrollbar).into();
skin.serialize_entry("scrollbar", &def)?;
// line styles
skin.serialize_entry("paragraph", &self.paragraph)?;
skin.serialize_entry("code_block", &self.code_block)?;
skin.serialize_entry("table", &self.table)?;
// headers
skin.serialize_entry("headers", &self.headers)?;
// ordered list item style
skin.serialize_entry("ordered_items", &self.ordered_item_styles)?;
// table border chars
// There's currently no way to allow custom
if let Some(key) = self.table_border_chars.key() {
skin.serialize_entry("table_border_chars", key)?;
}
skin.end()
}
}
#[derive(Deserialize)]
#[serde(untagged)]
enum HeadersStyleInfo {
Add(LineStyle),
Levels(Vec<LineStyle>),
}