use std::collections::HashMap;
use std::sync::OnceLock;
use super::ElementWrapper;
use crate::error::WebDynproError;
pub type FactoryFn =
for<'a> fn(String, scraper::ElementRef<'a>) -> Result<ElementWrapper<'a>, WebDynproError>;
pub struct ElementRegistration {
pub control_id: &'static str,
pub from_ref_fn: FactoryFn,
}
impl ElementRegistration {
pub const fn new(control_id: &'static str, from_ref_fn: FactoryFn) -> Self {
Self {
control_id,
from_ref_fn,
}
}
}
inventory::collect!(ElementRegistration);
pub fn registry_map() -> &'static HashMap<&'static str, FactoryFn> {
static MAP: OnceLock<HashMap<&'static str, FactoryFn>> = OnceLock::new();
MAP.get_or_init(|| {
let mut map = HashMap::with_capacity(32);
for reg in inventory::iter::<ElementRegistration> {
map.insert(reg.control_id, reg.from_ref_fn);
}
map
})
}
pub struct TextisableRegistration {
pub to_string_fn: for<'a> fn(&ElementWrapper<'a>) -> Option<String>,
}
inventory::collect!(TextisableRegistration);