use {
super::{Component, Entireness, ProcessedComponent},
crate::{
fh::{ComponentInformation, ComponentType, FileHierarchy, Level},
LewpError,
},
std::{collections::HashMap, rc::Rc, sync::Arc},
};
pub struct RegisterOptions {}
impl RegisterOptions {
pub fn new() -> Self {
Self {}
}
}
pub struct Register {
fh: Rc<FileHierarchy>,
options: RegisterOptions,
components: HashMap<Rc<ComponentInformation>, ProcessedComponent>,
}
impl Register {
pub fn new(fh: FileHierarchy, options: RegisterOptions) -> Self {
Self {
fh: Rc::new(fh),
options,
components: HashMap::new(),
}
}
pub fn query(
&self,
component_information: Rc<ComponentInformation>,
entity: Entireness,
) -> Option<Arc<String>> {
let ref_css = self.components.get(&component_information)?;
Some(ref_css.render_critical())
}
pub fn load_process_components(&mut self) -> Result<(), LewpError> {
self.load_process_modules()?;
self.load_process_pages()
}
fn load_process_modules(&mut self) -> Result<(), LewpError> {
let module_ids = self
.fh
.collect_component_ids(ComponentType::Css, Level::Module)?;
for id in module_ids {
let component_information = Rc::new(ComponentInformation {
id: id.clone(),
level: Level::Module,
kind: ComponentType::Css,
});
let c =
Component::new(component_information.clone(), self.fh.clone());
let c = ProcessedComponent::from(&c)?;
self.components.insert(component_information.clone(), c);
}
Ok(())
}
fn load_process_pages(&mut self) -> Result<(), LewpError> {
let page_ids = self
.fh
.collect_component_ids(ComponentType::Css, Level::Page)?;
for id in page_ids {
let component_information = Rc::new(ComponentInformation {
id: id.clone(),
level: Level::Page,
kind: ComponentType::Css,
});
let c =
Component::new(component_information.clone(), self.fh.clone());
let c = ProcessedComponent::from(&c)?;
self.components.insert(component_information.clone(), c);
}
Ok(())
}
}
impl Default for Register {
fn default() -> Self {
Self::new(FileHierarchy::new(), RegisterOptions::new())
}
}