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
use std::{fmt::Display, str::FromStr};

use serde::{Deserialize, Serialize};
use thiserror::Error;


#[derive(Error, Debug)]
pub enum ThemeError {
    #[error("unsupported theme: {0}")]
    Unsupported(String)
}


#[derive(Debug, Clone, Deserialize, Serialize)]
pub enum Theme {
    Light,
    Dark,
    Scientific,
    Vintage,
    HighContrast,
    None,
}

impl Default for Theme {
    fn default() -> Self {
        Self::Light
    }
}

impl FromStr for Theme {
    type Err = ThemeError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "light" => Ok(Self::Light),
            "dark" => Ok(Self::Dark),
            "scientific" => Ok(Self::Scientific),
            "vintage" => Ok(Self::Vintage),
            "high-contrast" => Ok(Self::HighContrast),
            "none" => Ok(Self::None),

            _ => Err(ThemeError::Unsupported(String::from(s))),
        }
    }
}

impl Display for Theme {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let s = match self {
            Self::Light => "light",
            Self::Dark => "dark",
            Self::Scientific => "scientific",
            Self::Vintage => "vintage",
            Self::HighContrast => "high-contrast",
            Self::None => "none",
        };

        f.write_str(s)
    }
}