tinted_builder/
scheme.rs

1mod base16;
2mod color;
3
4use serde::{Deserialize, Serialize};
5use std::{fmt, str::FromStr};
6
7pub use crate::scheme::base16::Base16Scheme;
8pub use crate::scheme::color::Color;
9use crate::TintedBuilderError;
10
11/// Enum representing schemes for different scheme systems. This enum is non-exhaustive, meaning
12/// additional variants may be added in future versions without it being considered a breaking
13/// change.
14#[non_exhaustive]
15#[derive(Debug, Clone)]
16pub enum Scheme {
17    /// Base16 variant with Base16Scheme deserialized content.
18    Base16(Base16Scheme),
19    /// Base24 variant with Base16Scheme deserialized content. Base16Scheme is built to support
20    /// basic supersets of Base16 schemes.
21    Base24(Base16Scheme),
22}
23
24impl Scheme {
25    pub fn get_scheme_author(&self) -> String {
26        match self {
27            Scheme::Base16(scheme) => scheme.author.to_string(),
28            Scheme::Base24(scheme) => scheme.author.to_string(),
29        }
30    }
31    pub fn get_scheme_description(&self) -> String {
32        match self {
33            Scheme::Base16(scheme) => scheme
34                .description
35                .clone()
36                .map(|s| s.to_string())
37                .unwrap_or_default(),
38            Scheme::Base24(scheme) => scheme
39                .description
40                .clone()
41                .map(|s| s.to_string())
42                .unwrap_or_default(),
43        }
44    }
45    pub fn get_scheme_name(&self) -> String {
46        match self {
47            Scheme::Base16(scheme) => scheme.name.to_string(),
48            Scheme::Base24(scheme) => scheme.name.to_string(),
49        }
50    }
51    pub fn get_scheme_slug(&self) -> String {
52        match self {
53            Scheme::Base16(scheme) => scheme.slug.to_string(),
54            Scheme::Base24(scheme) => scheme.slug.to_string(),
55        }
56    }
57    pub fn get_scheme_system(&self) -> SchemeSystem {
58        match self {
59            Scheme::Base16(_) => SchemeSystem::Base16,
60            Scheme::Base24(_) => SchemeSystem::Base24,
61        }
62    }
63    pub fn get_scheme_variant(&self) -> SchemeVariant {
64        match self {
65            Scheme::Base16(scheme) => scheme.variant.clone(),
66            Scheme::Base24(scheme) => scheme.variant.clone(),
67        }
68    }
69}
70
71/// Enum representing the scheme system. This enum is non-exhaustive, meaning additional variants
72/// may be added in future versions without it being considered a breaking change.
73#[non_exhaustive]
74#[derive(Debug, Clone, Default, PartialEq, Deserialize, Serialize)]
75#[serde(rename_all = "lowercase")]
76pub enum SchemeSystem {
77    /// Base16 scheme system, the default.
78    #[default]
79    Base16,
80    /// Base24 scheme system.
81    Base24,
82    List,
83    ListBase16,
84    ListBase24,
85}
86
87impl SchemeSystem {
88    /// Returns the string representation of the `SchemeSystem`.
89    pub fn as_str(&self) -> &str {
90        match self {
91            SchemeSystem::Base16 => "base16",
92            SchemeSystem::Base24 => "base24",
93            SchemeSystem::List => "list",
94            SchemeSystem::ListBase16 => "listbase16",
95            SchemeSystem::ListBase24 => "listbase24",
96        }
97    }
98    pub fn variants() -> &'static [SchemeSystem] {
99        static VARIANTS: &[SchemeSystem] = &[SchemeSystem::Base16, SchemeSystem::Base24];
100        VARIANTS
101    }
102}
103
104impl fmt::Display for SchemeSystem {
105    /// Formats the `SchemeSystem` for display purposes.
106    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
107        write!(f, "{}", self.as_str())?;
108        Ok(())
109    }
110}
111
112impl FromStr for SchemeSystem {
113    type Err = TintedBuilderError;
114
115    /// Parses a string to create a `SchemeSystem`.
116    ///
117    /// # Errors
118    ///
119    /// Returns a `TintedBuilderError` if the input string does not match
120    /// any valid scheme variant.
121    fn from_str(system_str: &str) -> Result<Self, Self::Err> {
122        match system_str {
123            "base16" => Ok(Self::Base16),
124            "base24" => Ok(Self::Base24),
125            _ => Err(TintedBuilderError::InvalidSchemeSystem(
126                system_str.to_string(),
127            )),
128        }
129    }
130}
131
132/// Enum representing variants of a color scheme, such as Dark or Light. This enum is
133/// non-exhaustive, meaning additional variants may be added in future versions without it being
134/// considered a breaking change.
135#[non_exhaustive]
136#[derive(Debug, Clone, Default, Deserialize, PartialEq, Serialize)]
137#[serde(rename_all = "lowercase")]
138pub enum SchemeVariant {
139    /// Dark variant of the color scheme, the default.
140    #[default]
141    Dark,
142    /// Light variant of the color scheme.
143    Light,
144}
145
146impl FromStr for SchemeVariant {
147    type Err = TintedBuilderError;
148
149    /// Parses a string to create a `SchemeVariant`.
150    ///
151    /// # Errors
152    ///
153    /// Returns a `TintedBuilderError` if the input string does not match
154    /// any valid scheme variant.
155    fn from_str(variant_str: &str) -> Result<Self, Self::Err> {
156        match variant_str {
157            "light" => Ok(Self::Light),
158            "dark" => Ok(Self::Dark),
159            _ => Err(TintedBuilderError::InvalidSchemeVariant(
160                variant_str.to_string(),
161            )),
162        }
163    }
164}
165
166impl SchemeVariant {
167    /// Returns the string representation of the `SchemeVariant`.
168    pub fn as_str(&self) -> &str {
169        match self {
170            SchemeVariant::Dark => "dark",
171            SchemeVariant::Light => "light",
172        }
173    }
174}
175
176impl fmt::Display for SchemeVariant {
177    /// Formats the `SchemeVariant` for display purposes.
178    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
179        write!(f, "{}", self.as_str())?;
180        Ok(())
181    }
182}