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
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
|
use super::generated_api::api::style::{
color::Payload as ProtobufColorPayload, Color as ProtobufColor, ColorType as ProtobufColorType,
Palette as ProtobufPalette, RgbColorPayload as ProtobufRgbColorPayload, Style as ProtobufStyle,
Styling as ProtobufStyling, ThemeHue as ProtobufThemeHue,
};
use crate::data::{
MultiplayerColors, Palette, PaletteColor, Style, StyleDeclaration, Styling, ThemeHue,
};
use crate::errors::prelude::*;
use std::convert::TryFrom;
impl TryFrom<ProtobufStyle> for Style {
type Error = &'static str;
fn try_from(protobuf_style: ProtobufStyle) -> Result<Self, &'static str> {
let s = protobuf_style
.styling
.ok_or("malformed style payload")?
.try_into()?;
Ok(Style {
colors: s,
rounded_corners: protobuf_style.rounded_corners,
hide_session_name: protobuf_style.hide_session_name,
// deliberately not carried over the plugin protobuf boundary; see
// the field docs on Style. Plugins never need the frame's title
// alignment, and adding a proto field would mean regenerating the
// committed prost assets.
pane_title_alignment: Default::default(),
pane_gap_x: Default::default(),
pane_gap_y: Default::default(),
pane_padding: Default::default(),
})
}
}
#[allow(deprecated)]
impl TryFrom<Style> for ProtobufStyle {
type Error = &'static str;
fn try_from(style: Style) -> Result<Self, &'static str> {
let s = ProtobufStyling::try_from(style.colors)?;
let palette = Palette::try_from(style.colors).map_err(|_| "malformed style payload")?;
Ok(ProtobufStyle {
palette: Some(palette.try_into()?),
rounded_corners: style.rounded_corners,
hide_session_name: style.hide_session_name,
styling: Some(s),
})
}
}
fn to_array<T, const N: usize>(v: Vec<T>) -> std::result::Result<[T; N], &'static str> {
v.try_into()
.map_err(|_| "Could not obtain array from protobuf field")
}
fn to_style_declaration(
parsed_array: Result<[PaletteColor; 6], &'static str>,
) -> Result<StyleDeclaration, &'static str> {
parsed_array.map(|arr| StyleDeclaration {
base: arr[0],
background: arr[1],
emphasis_0: arr[2],
emphasis_1: arr[3],
emphasis_2: arr[4],
emphasis_3: arr[5],
})
}
fn to_multiplayer_colors(
parsed_array: Result<[PaletteColor; 10], &'static str>,
) -> Result<MultiplayerColors, &'static str> {
parsed_array.map(|arr| MultiplayerColors {
player_1: arr[0],
player_2: arr[1],
player_3: arr[2],
player_4: arr[3],
player_5: arr[4],
player_6: arr[5],
player_7: arr[6],
player_8: arr[7],
player_9: arr[8],
player_10: arr[9],
})
}
#[macro_export]
macro_rules! color_definitions {
($proto:expr, $declaration:ident, $size:expr) => {
to_style_declaration(to_array::<PaletteColor, $size>(
$proto
.$declaration
.into_iter()
.map(PaletteColor::try_from)
.collect::<Result<Vec<PaletteColor>, _>>()?,
))?
};
}
#[macro_export]
macro_rules! multiplayer_colors {
($proto:expr, $size: expr) => {
to_multiplayer_colors(to_array::<PaletteColor, $size>(
$proto
.multiplayer_user_colors
.into_iter()
.map(PaletteColor::try_from)
.collect::<Result<Vec<PaletteColor>, _>>()?,
))?
};
}
impl TryFrom<ProtobufStyling> for Styling {
type Error = &'static str;
fn try_from(proto: ProtobufStyling) -> std::result::Result<Self, Self::Error> {
let frame_unselected = if proto.frame_unselected.len() > 0 {
Some(color_definitions!(proto, frame_unselected, 6))
} else {
None
};
Ok(Styling {
text_unselected: color_definitions!(proto, text_unselected, 6),
text_selected: color_definitions!(proto, text_selected, 6),
ribbon_unselected: color_definitions!(proto, ribbon_unselected, 6),
ribbon_selected: color_definitions!(proto, ribbon_selected, 6),
table_title: color_definitions!(proto, table_title, 6),
table_cell_unselected: color_definitions!(proto, table_cell_unselected, 6),
table_cell_selected: color_definitions!(proto, table_cell_selected, 6),
list_unselected: color_definitions!(proto, list_unselected, 6),
list_selected: color_definitions!(proto, list_selected, 6),
frame_unselected,
frame_selected: color_definitions!(proto, frame_selected, 6),
frame_highlight: color_definitions!(proto, frame_highlight, 6),
exit_code_success: color_definitions!(proto, exit_code_success, 6),
exit_code_error: color_definitions!(proto, exit_code_error, 6),
multiplayer_user_colors: multiplayer_colors!(proto, 10),
})
}
}
impl TryFrom<StyleDeclaration> for Vec<ProtobufColor> {
type Error = &'static str;
fn try_from(colors: StyleDeclaration) -> std::result::Result<Self, Self::Error> {
Ok(vec![
colors.base.try_into()?,
colors.background.try_into()?,
colors.emphasis_0.try_into()?,
colors.emphasis_1.try_into()?,
colors.emphasis_2.try_into()?,
colors.emphasis_3.try_into()?,
])
}
}
impl TryFrom<MultiplayerColors> for Vec<ProtobufColor> {
type Error = &'static str;
fn try_from(value: MultiplayerColors) -> std::result::Result<Self, Self::Error> {
Ok(vec![
value.player_1.try_into()?,
value.player_2.try_into()?,
value.player_3.try_into()?,
value.player_4.try_into()?,
value.player_5.try_into()?,
value.player_6.try_into()?,
value.player_7.try_into()?,
value.player_8.try_into()?,
value.player_9.try_into()?,
value.player_10.try_into()?,
])
}
}
impl TryFrom<Styling> for ProtobufStyling {
type Error = &'static str;
fn try_from(style: Styling) -> std::result::Result<Self, Self::Error> {
let frame_unselected_vec = match style.frame_unselected {
None => Ok(Vec::new()),
Some(frame_unselected) => frame_unselected.try_into(),
};
Ok(ProtobufStyling {
text_unselected: style.text_unselected.try_into()?,
text_selected: style.text_selected.try_into()?,
ribbon_unselected: style.ribbon_unselected.try_into()?,
ribbon_selected: style.ribbon_selected.try_into()?,
table_title: style.table_title.try_into()?,
table_cell_unselected: style.table_cell_unselected.try_into()?,
table_cell_selected: style.table_cell_selected.try_into()?,
list_unselected: style.list_unselected.try_into()?,
list_selected: style.list_selected.try_into()?,
frame_unselected: frame_unselected_vec?,
frame_selected: style.frame_selected.try_into()?,
frame_highlight: style.frame_highlight.try_into()?,
exit_code_success: style.exit_code_success.try_into()?,
exit_code_error: style.exit_code_error.try_into()?,
multiplayer_user_colors: style.multiplayer_user_colors.try_into()?,
})
}
}
impl TryFrom<ProtobufPalette> for Palette {
type Error = &'static str;
fn try_from(protobuf_palette: ProtobufPalette) -> Result<Self, &'static str> {
Ok(Palette {
theme_hue: ProtobufThemeHue::from_i32(protobuf_palette.theme_hue)
.ok_or("malformed theme_hue payload for Palette")?
.try_into()?,
fg: protobuf_palette
.fg
.ok_or("malformed palette payload")?
.try_into()?,
bg: protobuf_palette
.bg
.ok_or("malformed palette payload")?
.try_into()?,
black: protobuf_palette
.black
.ok_or("malformed palette payload")?
.try_into()?,
red: protobuf_palette
.red
.ok_or("malformed palette payload")?
.try_into()?,
green: protobuf_palette
.green
.ok_or("malformed palette payload")?
.try_into()?,
yellow: protobuf_palette
.yellow
.ok_or("malformed palette payload")?
.try_into()?,
blue: protobuf_palette
.blue
.ok_or("malformed palette payload")?
.try_into()?,
magenta: protobuf_palette
.magenta
.ok_or("malformed palette payload")?
.try_into()?,
cyan: protobuf_palette
.cyan
.ok_or("malformed palette payload")?
.try_into()?,
white: protobuf_palette
.white
.ok_or("malformed palette payload")?
.try_into()?,
orange: protobuf_palette
.orange
.ok_or("malformed palette payload")?
.try_into()?,
gray: protobuf_palette
.gray
.ok_or("malformed palette payload")?
.try_into()?,
purple: protobuf_palette
.purple
.ok_or("malformed palette payload")?
.try_into()?,
gold: protobuf_palette
.gold
.ok_or("malformed palette payload")?
.try_into()?,
silver: protobuf_palette
.silver
.ok_or("malformed palette payload")?
.try_into()?,
pink: protobuf_palette
.pink
.ok_or("malformed palette payload")?
.try_into()?,
brown: protobuf_palette
.brown
.ok_or("malformed palette payload")?
.try_into()?,
..Default::default()
})
}
}
impl TryFrom<Palette> for ProtobufPalette {
type Error = &'static str;
fn try_from(palette: Palette) -> Result<Self, &'static str> {
let theme_hue: ProtobufThemeHue = palette
.theme_hue
.try_into()
.map_err(|_| "malformed payload for palette")?;
Ok(ProtobufPalette {
theme_hue: theme_hue as i32,
fg: Some(palette.fg.try_into()?),
bg: Some(palette.bg.try_into()?),
black: Some(palette.black.try_into()?),
red: Some(palette.red.try_into()?),
green: Some(palette.green.try_into()?),
yellow: Some(palette.yellow.try_into()?),
blue: Some(palette.blue.try_into()?),
magenta: Some(palette.magenta.try_into()?),
cyan: Some(palette.cyan.try_into()?),
white: Some(palette.white.try_into()?),
orange: Some(palette.orange.try_into()?),
gray: Some(palette.gray.try_into()?),
purple: Some(palette.purple.try_into()?),
gold: Some(palette.gold.try_into()?),
silver: Some(palette.silver.try_into()?),
pink: Some(palette.pink.try_into()?),
brown: Some(palette.brown.try_into()?),
..Default::default()
})
}
}
impl TryFrom<ProtobufColor> for PaletteColor {
type Error = &'static str;
fn try_from(protobuf_color: ProtobufColor) -> Result<Self, &'static str> {
match ProtobufColorType::from_i32(protobuf_color.color_type) {
Some(ProtobufColorType::Rgb) => match protobuf_color.payload {
Some(ProtobufColorPayload::RgbColorPayload(rgb_color_payload)) => {
Ok(PaletteColor::Rgb((
rgb_color_payload.red as u8,
rgb_color_payload.green as u8,
rgb_color_payload.blue as u8,
)))
},
_ => Err("malformed payload for Rgb color"),
},
Some(ProtobufColorType::EightBit) => match protobuf_color.payload {
Some(ProtobufColorPayload::EightBitColorPayload(eight_bit_payload)) => {
Ok(PaletteColor::EightBit(eight_bit_payload as u8))
},
_ => Err("malformed payload for 8bit color"),
},
None => Err("malformed payload for Color"),
}
}
}
impl TryFrom<PaletteColor> for ProtobufColor {
type Error = &'static str;
fn try_from(color: PaletteColor) -> Result<Self, &'static str> {
match color {
PaletteColor::Rgb((red, green, blue)) => {
let red = red as u32;
let green = green as u32;
let blue = blue as u32;
Ok(ProtobufColor {
color_type: ProtobufColorType::Rgb as i32,
payload: Some(ProtobufColorPayload::RgbColorPayload(
ProtobufRgbColorPayload { red, green, blue },
)),
})
},
PaletteColor::EightBit(color) => Ok(ProtobufColor {
color_type: ProtobufColorType::EightBit as i32,
payload: Some(ProtobufColorPayload::EightBitColorPayload(color as u32)),
}),
}
}
}
impl TryFrom<ThemeHue> for ProtobufThemeHue {
type Error = &'static str;
fn try_from(theme_hue: ThemeHue) -> Result<Self, &'static str> {
match theme_hue {
ThemeHue::Light => Ok(ProtobufThemeHue::Light),
ThemeHue::Dark => Ok(ProtobufThemeHue::Dark),
}
}
}
impl TryFrom<ProtobufThemeHue> for ThemeHue {
type Error = &'static str;
fn try_from(protobuf_theme_hue: ProtobufThemeHue) -> Result<Self, &'static str> {
match protobuf_theme_hue {
ProtobufThemeHue::Light => Ok(ThemeHue::Light),
ProtobufThemeHue::Dark => Ok(ThemeHue::Dark),
}
}
}
|