1use std::{fmt::Display, str::FromStr};
2
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6
7#[derive(Error, Debug)]
8pub enum ThemeError {
9 #[error("unsupported theme: {0}")]
10 Unsupported(String)
11}
12
13
14#[derive(Debug, Clone, Deserialize, Serialize)]
15pub enum Theme {
16 Light,
17 Dark,
18 Scientific,
19 Vintage,
20 HighContrast,
21 None,
22}
23
24impl Default for Theme {
25 fn default() -> Self {
26 Self::Light
27 }
28}
29
30impl FromStr for Theme {
31 type Err = ThemeError;
32
33 fn from_str(s: &str) -> Result<Self, Self::Err> {
34 match s.to_lowercase().as_str() {
35 "light" => Ok(Self::Light),
36 "dark" => Ok(Self::Dark),
37 "scientific" => Ok(Self::Scientific),
38 "vintage" => Ok(Self::Vintage),
39 "high-contrast" => Ok(Self::HighContrast),
40 "none" => Ok(Self::None),
41
42 _ => Err(ThemeError::Unsupported(String::from(s))),
43 }
44 }
45}
46
47impl Display for Theme {
48 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49 let s = match self {
50 Self::Light => "light",
51 Self::Dark => "dark",
52 Self::Scientific => "scientific",
53 Self::Vintage => "vintage",
54 Self::HighContrast => "high-contrast",
55 Self::None => "none",
56 };
57
58 f.write_str(s)
59 }
60}