use std::collections::HashMap;
use crate::{Check, CheckId, FileType, FontspectorError, Profile, Testable, TTF};
#[derive(Default)]
pub struct Registry<'a> {
pub checks: HashMap<CheckId, Check<'a>>,
pub profiles: HashMap<String, Profile>,
pub(crate) filetypes: HashMap<String, FileType<'a>>,
}
impl<'a> Registry<'a> {
pub fn new() -> Registry<'static> {
let mut reg = Registry::default();
reg.register_filetype("TTF", TTF);
reg
}
pub fn iter(&self) -> impl Iterator<Item = &Check<'_>> {
self.checks.values()
}
pub fn iter_profiles(&self) -> impl Iterator<Item = (&String, &Profile)> {
self.profiles.iter()
}
pub fn register_profile(
&mut self,
name: &str,
mut profile: Profile,
validate: bool,
) -> Result<(), FontspectorError> {
if validate {
profile.validate(self)?;
}
self.profiles.insert(name.to_string(), profile);
Ok(())
}
pub fn get_profile(&self, name: &str) -> Option<&Profile> {
self.profiles.get(name)
}
pub fn register_filetype(&mut self, name: &str, filetype: FileType<'a>) {
self.filetypes.insert(name.to_string(), filetype);
}
pub fn register_check(&mut self, check: Check<'a>) {
self.checks.insert(check.id.to_string(), check);
}
pub fn register_simple_profile(
&mut self,
name: &str,
checks: Vec<Check<'a>>,
) -> Result<(), FontspectorError> {
let mut profile = Profile::default();
profile.sections.insert(
name.to_string(),
checks.iter().map(|c| c.id.to_string()).collect(),
);
for check in checks {
self.register_check(check);
}
self.register_profile(name, profile, false)
}
pub fn is_experimental(&self, check_id: &str) -> bool {
self.checks
.get(check_id)
.is_some_and(|c| c.flags.experimental)
}
pub fn is_known_file(&self, file: &Testable) -> bool {
self.filetypes.values().any(|ft| ft.applies(file))
}
}