chore: audit components
This commit is contained in:
+31
-14
@@ -2,22 +2,39 @@
|
||||
|
||||
use crate::definition::ThemeDefinition;
|
||||
|
||||
const TOKYO_NIGHT: &str = include_str!("../presets/tokyo-night.toml");
|
||||
/// The bundled presets: `(name, TOML source)`. Enumeration and lookup both
|
||||
/// read this one table.
|
||||
const PRESETS: &[(&str, &str)] = &[("tokyo-night", include_str!("../presets/tokyo-night.toml"))];
|
||||
|
||||
pub fn preset_names() -> &'static [&'static str] {
|
||||
&["tokyo-night"]
|
||||
/// Every bundled preset's name.
|
||||
pub fn preset_names() -> impl Iterator<Item = &'static str> {
|
||||
PRESETS.iter().map(|(name, _)| *name)
|
||||
}
|
||||
|
||||
/// Parses the named preset.
|
||||
pub fn builtin(name: &str) -> Option<ThemeDefinition> {
|
||||
match name {
|
||||
"tokyo-night" => match ThemeDefinition::parse_toml(TOKYO_NIGHT) {
|
||||
Ok(def) => Some(def),
|
||||
Err(err) => {
|
||||
eprintln!("bundled preset tokyo-night failed to parse: {err}");
|
||||
None
|
||||
}
|
||||
},
|
||||
_ => None,
|
||||
/// Parses the named preset, returning its static name and definition.
|
||||
pub fn builtin(name: &str) -> Option<(&'static str, ThemeDefinition)> {
|
||||
let (name, source) = PRESETS
|
||||
.iter()
|
||||
.find(|(candidate, _)| *candidate == name)
|
||||
.copied()?;
|
||||
match ThemeDefinition::parse_toml(source) {
|
||||
Ok(definition) => Some((name, definition)),
|
||||
Err(error) => {
|
||||
log::error!("bundled preset {name} failed to parse: {error}");
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
#[test]
|
||||
fn the_name_table_and_lookup_agree() {
|
||||
for name in preset_names() {
|
||||
assert!(builtin(name).is_some(), "{name} fails to parse");
|
||||
}
|
||||
assert!(builtin("no-such-preset").is_none());
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user