registry.rs

 1use crate::{themes::one_dark, Theme, ThemeMetadata};
 2use anyhow::{anyhow, Result};
 3use gpui2::SharedString;
 4use std::{collections::HashMap, sync::Arc};
 5
 6pub struct ThemeRegistry {
 7    themes: HashMap<SharedString, Arc<Theme>>,
 8}
 9
10impl ThemeRegistry {
11    pub fn new() -> Self {
12        let mut this = Self {
13            themes: HashMap::default(),
14        };
15
16        this.insert_themes([one_dark()]);
17
18        this
19    }
20
21    fn insert_themes(&mut self, themes: impl IntoIterator<Item = Theme>) {
22        for theme in themes.into_iter() {
23            self.themes
24                .insert(theme.metadata.name.clone(), Arc::new(theme));
25        }
26    }
27
28    pub fn list_names(&self, _staff: bool) -> impl Iterator<Item = SharedString> + '_ {
29        self.themes.keys().cloned()
30    }
31
32    pub fn list(&self, _staff: bool) -> impl Iterator<Item = ThemeMetadata> + '_ {
33        self.themes.values().map(|theme| theme.metadata.clone())
34    }
35
36    pub fn get(&self, name: impl Into<SharedString>) -> Result<Arc<Theme>> {
37        let name = name.into();
38        self.themes
39            .get(&name)
40            .ok_or_else(|| anyhow!("theme not found: {}", name))
41            .cloned()
42    }
43}