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