Skip to content

Commit fd3d75a

Browse files
committed
Load default themes via RawThemes, remove Theme deserialization
1 parent a275808 commit fd3d75a

File tree

1 file changed

+9
-59
lines changed

1 file changed

+9
-59
lines changed

helix-view/src/theme.rs

Lines changed: 9 additions & 59 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,14 @@ use toml::Value;
1313
pub use crate::graphics::{Color, Modifier, Style};
1414

1515
pub static DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
16-
toml::from_slice(include_bytes!("../../theme.toml")).expect("Failed to parse default theme")
16+
let raw_theme: RawTheme = toml::from_slice(include_bytes!("../../theme.toml"))
17+
.expect("Failed to parse default theme");
18+
Theme::from(raw_theme)
1719
});
1820
pub static BASE16_DEFAULT_THEME: Lazy<Theme> = Lazy::new(|| {
19-
toml::from_slice(include_bytes!("../../base16_theme.toml"))
20-
.expect("Failed to parse base 16 default theme")
21+
let raw_theme: RawTheme = toml::from_slice(include_bytes!("../../base16_theme.toml"))
22+
.expect("Failed to parse base 16 default theme");
23+
Theme::from(raw_theme)
2124
});
2225

2326
#[derive(Clone, Debug)]
@@ -53,11 +56,6 @@ impl Loader {
5356
}
5457

5558
Ok(Theme::from(raw_theme))
56-
57-
// let path = self.path(name);
58-
// let data = std::fs::read(&path)?;
59-
60-
// toml::from_slice(data.as_slice()).context("Faled to deserialize theme")
6159
}
6260

6361
pub fn read_names(path: &Path) -> Vec<String> {
@@ -88,13 +86,11 @@ impl Loader {
8886
let filename = format!("{}.toml", name);
8987

9088
let user_path = self.user_dir.join(&filename);
91-
let path = if user_path.exists() {
89+
if user_path.exists() {
9290
user_path
9391
} else {
9492
self.default_dir.join(filename)
95-
};
96-
97-
path
93+
}
9894
}
9995

10096
/// Lists all theme names available in default and user directory
@@ -171,7 +167,7 @@ impl<'de> Deserialize<'de> for RawTheme {
171167

172168
let inherits_from = values
173169
.remove("inherits_from")
174-
.map(|value| value.to_string().replace("\"", ""));
170+
.map(|value| value.to_string().replace('\"', ""));
175171

176172
Ok(Self {
177173
values,
@@ -216,52 +212,6 @@ impl From<RawTheme> for Theme {
216212
}
217213
}
218214

219-
impl<'de> Deserialize<'de> for Theme {
220-
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
221-
where
222-
D: Deserializer<'de>,
223-
{
224-
let mut styles = HashMap::new();
225-
let mut scopes = Vec::new();
226-
let mut highlights = Vec::new();
227-
228-
if let Ok(mut colors) = HashMap::<String, Value>::deserialize(deserializer) {
229-
// TODO: alert user of parsing failures in editor
230-
let palette = colors
231-
.remove("palette")
232-
.map(|value| {
233-
ThemePalette::try_from(value).unwrap_or_else(|err| {
234-
warn!("{}", err);
235-
ThemePalette::default()
236-
})
237-
})
238-
.unwrap_or_default();
239-
240-
styles.reserve(colors.len());
241-
scopes.reserve(colors.len());
242-
highlights.reserve(colors.len());
243-
244-
for (name, style_value) in colors {
245-
let mut style = Style::default();
246-
if let Err(err) = palette.parse_style(&mut style, style_value) {
247-
warn!("{}", err);
248-
}
249-
250-
// these are used both as UI and as highlights
251-
styles.insert(name.clone(), style);
252-
scopes.push(name);
253-
highlights.push(style);
254-
}
255-
}
256-
257-
Ok(Self {
258-
scopes,
259-
styles,
260-
highlights,
261-
})
262-
}
263-
}
264-
265215
impl Theme {
266216
#[inline]
267217
pub fn highlight(&self, index: usize) -> Style {

0 commit comments

Comments
 (0)