Skip to main content

vvv_core/lang/
registry.rs

1use std::path::Path;
2use std::sync::Arc;
3
4use super::{Language, LanguageId};
5
6/// The set of languages an engine knows about. Filled at startup; read-only after.
7#[derive(Clone, Default)]
8pub struct LanguageRegistry {
9    languages: Vec<Arc<dyn Language>>,
10}
11
12impl LanguageRegistry {
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    pub fn with(mut self, language: impl Language + 'static) -> Self {
18        self.languages.push(Arc::new(language));
19        self
20    }
21
22    pub fn get(&self, id: &LanguageId) -> Option<Arc<dyn Language>> {
23        self.languages.iter().find(|l| &l.id() == id).cloned()
24    }
25
26    pub fn for_path(&self, path: &Path) -> Option<Arc<dyn Language>> {
27        let ext = path.extension()?.to_str()?;
28        self.languages
29            .iter()
30            .find(|l| l.extensions().contains(&ext))
31            .cloned()
32    }
33
34    pub fn iter(&self) -> impl Iterator<Item = &Arc<dyn Language>> {
35        self.languages.iter()
36    }
37
38    pub fn is_empty(&self) -> bool {
39        self.languages.is_empty()
40    }
41}
42
43impl std::fmt::Debug for LanguageRegistry {
44    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
45        f.debug_list()
46            .entries(self.languages.iter().map(|l| l.id()))
47            .finish()
48    }
49}