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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::{error::ConversionError, Attribute, Value};
use iced::Theme;
/// A wrapper around the `Theme` enum that provides additional functionality,
/// such as converting a string representation of a theme into its corresponding
/// `Theme` variant.
pub struct SnowcapTheme(pub Theme);
impl SnowcapTheme {
/// Returns a reference to the inner `Theme`.
///
/// # Returns
///
/// A reference to the `Theme` enum stored in this `SnowcapTheme` wrapper.
///
/// # Examples
///
/// ```
/// use iced::Theme;
/// use snowcap::SnowcapTheme;
/// let theme = SnowcapTheme(Theme::Light);
/// assert_eq!(theme.theme(), &Theme::Light);
/// ```
pub fn theme(&self) -> &Theme {
&self.0
}
}
impl TryFrom<&str> for SnowcapTheme {
type Error = ConversionError;
/// Attempts to convert a string into a `SnowcapTheme`.
///
/// This method is case-insensitive and will convert a given string to
/// its corresponding `Theme` variant. If the string doesn't match any of
/// the predefined theme names, an error will be returned.
///
/// # Parameters
///
/// * `theme_name`: A string slice that represents the name of the theme.
///
/// # Returns
///
/// If the string matches a valid theme name, a `SnowcapTheme` is returned
/// wrapped in a `Result`. If no match is found, a `ConversionError::Unknown`
/// is returned.
///
/// # Errors
///
/// Returns a `ConversionError::Unknown` if the input string does not match
/// any valid theme names.
///
/// # Examples
///
/// ```
/// use iced::Theme;
/// use snowcap::{SnowcapTheme,ConversionError};
/// let theme: Result<SnowcapTheme, ConversionError> = SnowcapTheme::try_from("dracula");
/// assert_eq!(theme.unwrap().theme(), &Theme::Dracula);
///
/// let invalid_theme = SnowcapTheme::try_from("unknown_theme");
/// assert!(invalid_theme.is_err());
/// ```
fn try_from(theme_name: &str) -> Result<Self, ConversionError> {
let theme = match theme_name.to_lowercase().as_str() {
"light" => SnowcapTheme(Theme::Light),
"dark" => SnowcapTheme(Theme::Dark),
"dracula" => SnowcapTheme(Theme::Dracula),
"nord" => SnowcapTheme(Theme::Nord),
"solarizedlight" => SnowcapTheme(Theme::SolarizedLight),
"solarizeddark" => SnowcapTheme(Theme::SolarizedDark),
"gruvboxlight" => SnowcapTheme(Theme::GruvboxLight),
"gruvboxdark" => SnowcapTheme(Theme::GruvboxDark),
"catppuccinlatte" => SnowcapTheme(Theme::CatppuccinLatte),
"catppuccinfrappe" => SnowcapTheme(Theme::CatppuccinFrappe),
"catppuccinmacchiato" => SnowcapTheme(Theme::CatppuccinMacchiato),
"catppuccinmocha" => SnowcapTheme(Theme::CatppuccinMocha),
"tokyonight" => SnowcapTheme(Theme::TokyoNight),
"tokyonightstorm" => SnowcapTheme(Theme::TokyoNightStorm),
"tokyonightlight" => SnowcapTheme(Theme::TokyoNightLight),
"kanagawawave" => SnowcapTheme(Theme::KanagawaWave),
"kanagawadragon" => SnowcapTheme(Theme::KanagawaDragon),
"kanagawalotus" => SnowcapTheme(Theme::KanagawaLotus),
"moonfly" => SnowcapTheme(Theme::Moonfly),
"nightfly" => SnowcapTheme(Theme::Nightfly),
"oxocarbon" => SnowcapTheme(Theme::Oxocarbon),
"ferra" => SnowcapTheme(Theme::Ferra),
_ => {
return Err(ConversionError::Unknown(format!(
"Unknown theme '{theme_name}'"
)))
}
};
Ok(theme)
}
}
impl TryInto<Theme> for &Value {
type Error = ConversionError;
/// Attempts to convert a `Value` reference into a `Theme`.
///
/// This implementation first tries to convert the `Value` into a string
/// representation of the theme name. It then uses the `SnowcapTheme` type
/// to convert that string into the appropriate `Theme` variant.
///
/// # Returns
///
/// If successful, returns a `Theme` variant. If the conversion fails due
/// to an invalid `Value` or an unrecognized theme name, a `ConversionError`
/// is returned.
///
/// # Errors
///
/// This function returns a `ConversionError` in two cases:
/// 1. If the `Value` cannot be converted into a string.
/// 2. If the resulting string is not a recognized theme name.
///
/// # Examples
///
/// ```
/// use iced::Theme;
/// use snowcap::{Value,ConversionError};
/// let value = Value::String(String::from("dracula"));
/// let theme: Result<Theme, ConversionError> = (&value).try_into();
/// assert_eq!(theme.unwrap(), Theme::Dracula);
///
/// let invalid_value = Value::Number(42.into());
/// let result: Result<Theme, ConversionError> = (&invalid_value).try_into();
/// assert!(result.is_err());
/// ```
fn try_into(self) -> Result<Theme, Self::Error> {
let theme_name: &String = self.try_into()?;
let wrapped_theme = SnowcapTheme::try_from(theme_name.as_str())?;
// Return the inner iced::Theme
Ok(wrapped_theme.0)
}
}
impl TryInto<Theme> for &Attribute {
type Error = ConversionError;
fn try_into(self) -> Result<Theme, Self::Error> {
(&*self.value()).try_into()
}
}
#[cfg(test)]
mod test {
use super::SnowcapTheme;
#[test]
pub fn from_string() {
let _theme = SnowcapTheme::try_from("Light").unwrap().theme();
}
}