src/config/serde.rs

84c4617332578aaf4e9ee68a0a2d7d6258e48243
youtube-tui/src/config/serde.rs history · blame · raw

198 lines · 6827 bytes

10 symbols in this file
  1//! Structs that impl `Into<T>` because `T` does not impl Serde but is used in config files
  2use crossterm::event::KeyCode;
  3use ratatui::{style::Color, widgets::BorderType};
  4use serde::{Deserialize, Serialize};
  5
  6/// `BorderType` but impl `serde`
  7#[derive(Serialize, Deserialize, Clone, Copy, Default)]
  8pub enum BorderTypeSerde {
  9    Plain,
 10    #[default]
 11    Rounded,
 12    Double,
 13    Thick,
 14}
 15
 16impl From<BorderTypeSerde> for BorderType {
 17    fn from(origianl: BorderTypeSerde) -> BorderType {
 18        match origianl {
 19            BorderTypeSerde::Plain => BorderType::Plain,
 20            BorderTypeSerde::Rounded => BorderType::Rounded,
 21            BorderTypeSerde::Thick => BorderType::Thick,
 22            BorderTypeSerde::Double => BorderType::Double,
 23        }
 24    }
 25}
 26
 27/// all terminal colors
 28#[derive(Serialize, Deserialize, Clone)]
 29pub enum ColorVariantSerde {
 30    Reset,
 31    Black,
 32    Red,
 33    Green,
 34    Yellow,
 35    Blue,
 36    Magenta,
 37    Cyan,
 38    Gray,
 39    DarkGray,
 40    LightRed,
 41    LightGreen,
 42    LightYellow,
 43    LightBlue,
 44    LightMagenta,
 45    LightCyan,
 46    White,
 47}
 48
 49/// a single color
 50#[derive(Serialize, Deserialize, Clone)]
 51#[serde(untagged)]
 52pub enum ColorSerde {
 53    ColorVariant(ColorVariantSerde),
 54    Hex(String),
 55}
 56
 57impl ColorSerde {
 58    pub fn to_color(self) -> Option<Color> {
 59        match self {
 60            Self::ColorVariant(ColorVariantSerde::Reset) => Some(Color::Reset),
 61            Self::ColorVariant(ColorVariantSerde::Black) => Some(Color::Black),
 62            Self::ColorVariant(ColorVariantSerde::Red) => Some(Color::Red),
 63            Self::ColorVariant(ColorVariantSerde::Green) => Some(Color::Green),
 64            Self::ColorVariant(ColorVariantSerde::Yellow) => Some(Color::Yellow),
 65            Self::ColorVariant(ColorVariantSerde::Blue) => Some(Color::Blue),
 66            Self::ColorVariant(ColorVariantSerde::Magenta) => Some(Color::Magenta),
 67            Self::ColorVariant(ColorVariantSerde::Cyan) => Some(Color::Cyan),
 68            Self::ColorVariant(ColorVariantSerde::Gray) => Some(Color::Gray),
 69            Self::ColorVariant(ColorVariantSerde::DarkGray) => Some(Color::DarkGray),
 70            Self::ColorVariant(ColorVariantSerde::LightRed) => Some(Color::LightRed),
 71            Self::ColorVariant(ColorVariantSerde::LightGreen) => Some(Color::LightGreen),
 72            Self::ColorVariant(ColorVariantSerde::LightYellow) => Some(Color::LightYellow),
 73            Self::ColorVariant(ColorVariantSerde::LightBlue) => Some(Color::LightBlue),
 74            Self::ColorVariant(ColorVariantSerde::LightMagenta) => Some(Color::LightMagenta),
 75            Self::ColorVariant(ColorVariantSerde::LightCyan) => Some(Color::LightCyan),
 76            Self::ColorVariant(ColorVariantSerde::White) => Some(Color::White),
 77            Self::Hex(s) => {
 78                if s.len() != 7 || &s[0..1] != "#" {
 79                    return None;
 80                }
 81
 82                Some(Color::Rgb(
 83                    from_hex(&s[1..3])?,
 84                    from_hex(&s[3..5])?,
 85                    from_hex(&s[5..7])?,
 86                ))
 87            }
 88        }
 89    }
 90}
 91
 92/// converts a 2 digit hex number (00 - FF) to u8
 93fn from_hex(s: &str) -> Option<u8> {
 94    Some(from_hex_digit(&s[0..1])? * 16 + from_hex_digit(&s[1..2])?)
 95}
 96
 97/// converts a single hex digit to u8
 98fn from_hex_digit(d: &str) -> Option<u8> {
 99    match d.to_uppercase().as_str() {
100        "0" => Some(0),
101        "1" => Some(1),
102        "2" => Some(2),
103        "3" => Some(3),
104        "4" => Some(4),
105        "5" => Some(5),
106        "6" => Some(6),
107        "7" => Some(7),
108        "8" => Some(8),
109        "9" => Some(9),
110        "A" => Some(10),
111        "B" => Some(11),
112        "C" => Some(12),
113        "D" => Some(13),
114        "E" => Some(14),
115        "F" => Some(15),
116        _ => None,
117    }
118}
119
120/// serde version of a single key code
121#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, Debug)]
122#[serde(untagged)]
123pub enum KeyCodeSerde {
124    Char(char),
125    KeyVariants(KeyVariantsSerde),
126    F(String),
127}
128
129/// values of all default keys (excluding chars and function keys)
130#[derive(Serialize, Deserialize, Clone, Hash, PartialEq, Eq, Debug)]
131pub enum KeyVariantsSerde {
132    Backspace,
133    Enter,
134    Left,
135    Right,
136    Up,
137    Down,
138    Home,
139    End,
140    PageUp,
141    PageDown,
142    Tab,
143    BackTab,
144    Delete,
145    Insert,
146    Null,
147    Esc,
148    CapsLock,
149    ScrollLock,
150    NumLock,
151    PrintScreen,
152    Pause,
153    Menu,
154    KeypadBegin,
155}
156
157impl KeyCodeSerde {
158    pub fn to_keycode(self) -> Option<KeyCode> {
159        match self {
160            Self::KeyVariants(KeyVariantsSerde::Backspace) => Some(KeyCode::Backspace),
161            Self::KeyVariants(KeyVariantsSerde::Enter) => Some(KeyCode::Enter),
162            Self::KeyVariants(KeyVariantsSerde::Left) => Some(KeyCode::Left),
163            Self::KeyVariants(KeyVariantsSerde::Right) => Some(KeyCode::Right),
164            Self::KeyVariants(KeyVariantsSerde::Up) => Some(KeyCode::Up),
165            Self::KeyVariants(KeyVariantsSerde::Down) => Some(KeyCode::Down),
166            Self::KeyVariants(KeyVariantsSerde::Home) => Some(KeyCode::Home),
167            Self::KeyVariants(KeyVariantsSerde::End) => Some(KeyCode::End),
168            Self::KeyVariants(KeyVariantsSerde::PageUp) => Some(KeyCode::PageUp),
169            Self::KeyVariants(KeyVariantsSerde::PageDown) => Some(KeyCode::PageDown),
170            Self::KeyVariants(KeyVariantsSerde::Tab) => Some(KeyCode::Tab),
171            Self::KeyVariants(KeyVariantsSerde::BackTab) => Some(KeyCode::BackTab),
172            Self::KeyVariants(KeyVariantsSerde::Delete) => Some(KeyCode::Delete),
173            Self::KeyVariants(KeyVariantsSerde::Insert) => Some(KeyCode::Insert),
174            Self::KeyVariants(KeyVariantsSerde::Null) => Some(KeyCode::Null),
175            Self::KeyVariants(KeyVariantsSerde::Esc) => Some(KeyCode::Esc),
176            Self::KeyVariants(KeyVariantsSerde::CapsLock) => Some(KeyCode::CapsLock),
177            Self::KeyVariants(KeyVariantsSerde::ScrollLock) => Some(KeyCode::ScrollLock),
178            Self::KeyVariants(KeyVariantsSerde::NumLock) => Some(KeyCode::NumLock),
179            Self::KeyVariants(KeyVariantsSerde::PrintScreen) => Some(KeyCode::PrintScreen),
180            Self::KeyVariants(KeyVariantsSerde::Pause) => Some(KeyCode::Pause),
181            Self::KeyVariants(KeyVariantsSerde::Menu) => Some(KeyCode::Menu),
182            Self::KeyVariants(KeyVariantsSerde::KeypadBegin) => Some(KeyCode::KeypadBegin),
183            Self::Char(c) => Some(KeyCode::Char(c)),
184            Self::F(s) => {
185                if s[0..1].to_uppercase().as_str() != "F" {
186                    return None;
187                }
188
189                let parsed = s[1..].parse::<u8>();
190
191                match parsed {
192                    Ok(f) => Some(KeyCode::F(f)),
193                    Err(_) => None,
194                }
195            }
196        }
197    }
198}