Feat: begin slight refactoring
Some checks failed
Deploy Website / deploy (push) Failing after 4m46s
Some checks failed
Deploy Website / deploy (push) Failing after 4m46s
This commit is contained in:
@@ -1,65 +1,47 @@
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};
|
||||
|
||||
use crate::theme::ThemeColors;
|
||||
use crate::theme::{ThemeColors, THEMES};
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
|
||||
pub enum ColorScheme {
|
||||
#[default]
|
||||
CatppuccinMocha,
|
||||
CatppuccinLatte,
|
||||
Nord,
|
||||
Dracula,
|
||||
GruvboxDark,
|
||||
Monokai,
|
||||
PitchBlack,
|
||||
}
|
||||
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
|
||||
pub struct ColorScheme(usize);
|
||||
|
||||
impl ColorScheme {
|
||||
pub fn label(self) -> &'static str {
|
||||
match self {
|
||||
Self::CatppuccinMocha => "Catppuccin Mocha",
|
||||
Self::CatppuccinLatte => "Catppuccin Latte",
|
||||
Self::Nord => "Nord",
|
||||
Self::Dracula => "Dracula",
|
||||
Self::GruvboxDark => "Gruvbox Dark",
|
||||
Self::Monokai => "Monokai",
|
||||
Self::PitchBlack => "Pitch Black",
|
||||
}
|
||||
THEMES[self.0].label
|
||||
}
|
||||
|
||||
pub fn next(self) -> Self {
|
||||
match self {
|
||||
Self::CatppuccinMocha => Self::CatppuccinLatte,
|
||||
Self::CatppuccinLatte => Self::Nord,
|
||||
Self::Nord => Self::Dracula,
|
||||
Self::Dracula => Self::GruvboxDark,
|
||||
Self::GruvboxDark => Self::Monokai,
|
||||
Self::Monokai => Self::PitchBlack,
|
||||
Self::PitchBlack => Self::CatppuccinMocha,
|
||||
}
|
||||
Self((self.0 + 1) % THEMES.len())
|
||||
}
|
||||
|
||||
pub fn prev(self) -> Self {
|
||||
match self {
|
||||
Self::CatppuccinMocha => Self::PitchBlack,
|
||||
Self::CatppuccinLatte => Self::CatppuccinMocha,
|
||||
Self::Nord => Self::CatppuccinLatte,
|
||||
Self::Dracula => Self::Nord,
|
||||
Self::GruvboxDark => Self::Dracula,
|
||||
Self::Monokai => Self::GruvboxDark,
|
||||
Self::PitchBlack => Self::Monokai,
|
||||
}
|
||||
Self((self.0 + THEMES.len() - 1) % THEMES.len())
|
||||
}
|
||||
|
||||
pub fn to_theme(self) -> ThemeColors {
|
||||
match self {
|
||||
Self::CatppuccinMocha => ThemeColors::catppuccin_mocha(),
|
||||
Self::CatppuccinLatte => ThemeColors::catppuccin_latte(),
|
||||
Self::Nord => ThemeColors::nord(),
|
||||
Self::Dracula => ThemeColors::dracula(),
|
||||
Self::GruvboxDark => ThemeColors::gruvbox_dark(),
|
||||
Self::Monokai => ThemeColors::monokai(),
|
||||
Self::PitchBlack => ThemeColors::pitch_black(),
|
||||
}
|
||||
(THEMES[self.0].colors)()
|
||||
}
|
||||
}
|
||||
|
||||
impl Serialize for ColorScheme {
|
||||
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
|
||||
where
|
||||
S: Serializer,
|
||||
{
|
||||
serializer.serialize_str(THEMES[self.0].id)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'de> Deserialize<'de> for ColorScheme {
|
||||
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
|
||||
where
|
||||
D: Deserializer<'de>,
|
||||
{
|
||||
let s = String::deserialize(deserializer)?;
|
||||
THEMES
|
||||
.iter()
|
||||
.position(|t| t.id == s)
|
||||
.map(ColorScheme)
|
||||
.ok_or_else(|| de::Error::custom(format!("unknown theme: {s}")))
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user