Skip to main content

sim_lib_standard_core/
registry.rs

1//! In-memory registry of installed language profiles.
2
3use std::collections::BTreeMap;
4
5use sim_kernel::{Error, Result, Symbol};
6
7use crate::LanguageProfile;
8
9/// In-memory map of installed [`LanguageProfile`]s keyed by profile symbol.
10#[derive(Clone, Debug, Default)]
11pub struct ProfileRegistry {
12    profiles: BTreeMap<Symbol, LanguageProfile>,
13}
14
15impl ProfileRegistry {
16    /// Create an empty registry.
17    pub fn new() -> Self {
18        Self::default()
19    }
20
21    /// Register `profile`, failing if a profile with the same symbol is already
22    /// installed.
23    pub fn register_profile(&mut self, profile: LanguageProfile) -> Result<()> {
24        let symbol = profile.symbol.clone();
25        if self.profiles.contains_key(&symbol) {
26            return Err(Error::DuplicateExport {
27                kind: "standard-profile",
28                symbol,
29            });
30        }
31        self.profiles.insert(symbol, profile);
32        Ok(())
33    }
34
35    /// Look up an installed profile by symbol.
36    pub fn profile(&self, symbol: &Symbol) -> Option<&LanguageProfile> {
37        self.profiles.get(symbol)
38    }
39
40    /// Iterate the installed profiles in symbol order.
41    pub fn profiles(&self) -> impl Iterator<Item = &LanguageProfile> {
42        self.profiles.values()
43    }
44
45    /// Number of installed profiles.
46    pub fn len(&self) -> usize {
47        self.profiles.len()
48    }
49
50    /// Whether no profiles are installed.
51    pub fn is_empty(&self) -> bool {
52        self.profiles.is_empty()
53    }
54}