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    fn insert_themes(&mut self, themes: impl IntoIterator<Item = Theme>) {
12        for theme in themes.into_iter() {
13            self.themes
14                .insert(theme.metadata.name.clone(), Arc::new(theme));
15        }
16    }
17
18    pub fn list_names(&self, _staff: bool) -> impl Iterator<Item = SharedString> + '_ {
19        self.themes.keys().cloned()
20    }
21
22    pub fn list(&self, _staff: bool) -> impl Iterator<Item = ThemeMetadata> + '_ {
23        self.themes.values().map(|theme| theme.metadata.clone())
24    }
25
26    pub fn get(&self, name: &str) -> Result<Arc<Theme>> {
27        self.themes
28            .get(name)
29            .ok_or_else(|| anyhow!("theme not found: {}", name))
30            .cloned()
31    }
32}
33
34impl Default for ThemeRegistry {
35    fn default() -> Self {
36        let mut this = Self {
37            themes: HashMap::default(),
38        };
39
40        this.insert_themes([one_dark()]);
41
42        this
43    }
44}