use super::{Block, BlockError, BlockField};
pub struct BlockRegistration {
pub factory: fn() -> Box<dyn Block>,
pub origin: &'static str,
}
impl BlockRegistration {
#[must_use]
pub fn is_builtin(&self) -> bool {
self.origin == "rustango_cms" || self.origin.starts_with("rustango_cms::")
}
}
inventory::collect!(BlockRegistration);
#[macro_export]
macro_rules! register_block {
($ty:ty) => {
$crate::inventory::submit! {
$crate::block::BlockRegistration {
factory: || Box::new(<$ty>::default()),
origin: ::core::module_path!(),
}
}
};
($factory:expr) => {
$crate::inventory::submit! {
$crate::block::BlockRegistration {
factory: $factory,
origin: ::core::module_path!(),
}
}
};
}
fn resolved() -> &'static [&'static BlockRegistration] {
static RESOLVED: std::sync::OnceLock<Vec<&'static BlockRegistration>> = std::sync::OnceLock::new();
RESOLVED.get_or_init(|| {
let all: Vec<&'static BlockRegistration> = inventory::iter::<BlockRegistration>.into_iter().collect();
let mut out: Vec<&'static BlockRegistration> = Vec::with_capacity(all.len());
for reg in all {
let name = (reg.factory)().type_name();
match out.iter().position(|r| (r.factory)().type_name() == name) {
None => out.push(reg),
Some(i) => {
let kept = out[i];
if kept.is_builtin() && !reg.is_builtin() {
tracing::info!(target: "rustango_cms::block", block = name, by = reg.origin, "a site block replaces the built-in block of the same name");
out[i] = reg;
} else if !kept.is_builtin() && reg.is_builtin() {
tracing::info!(target: "rustango_cms::block", block = name, by = kept.origin, "a site block replaces the built-in block of the same name");
} else {
tracing::error!(
target: "rustango_cms::block",
block = name, kept = kept.origin, ignored = reg.origin,
"two blocks are registered with the same name; the first one is used — rename one of them"
);
}
}
}
}
out
})
}
pub fn registered_blocks() -> impl Iterator<Item = Box<dyn Block>> {
resolved().iter().map(|r| (r.factory)())
}
#[must_use]
pub fn find_block(type_name: &str) -> Option<Box<dyn Block>> {
registered_blocks().find(|b| b.type_name() == type_name)
}
#[must_use]
pub fn duplicate_block_names() -> Vec<&'static str> {
let mut seen: std::collections::HashMap<&'static str, bool> = std::collections::HashMap::new();
let mut dups = Vec::new();
for reg in inventory::iter::<BlockRegistration> {
let name = (reg.factory)().type_name();
match seen.get(name) {
None => {
seen.insert(name, reg.is_builtin());
}
Some(&builtin) if builtin != reg.is_builtin() => {}
Some(_) => {
if !dups.contains(&name) {
dups.push(name);
}
}
}
}
dups
}
pub fn check_block_references() -> Result<(), BlockError> {
let names: std::collections::HashSet<&'static str> =
registered_blocks().map(|b| b.type_name()).collect();
for block in registered_blocks() {
let owner = block.type_name();
for field in block.fields() {
match field {
BlockField::Stream { allowed, .. } => {
for ref_name in &allowed {
if !names.contains(ref_name.as_str()) {
let leaked: &'static str = Box::leak(ref_name.clone().into_boxed_str());
return Err(BlockError::DanglingReference {
owner,
referred: leaked,
});
}
}
}
BlockField::Repeat { item_type, .. } => {
if !names.contains(item_type.as_str()) {
let leaked: &'static str = Box::leak(item_type.clone().into_boxed_str());
return Err(BlockError::DanglingReference {
owner,
referred: leaked,
});
}
}
_ => {}
}
}
}
Ok(())
}
pub fn validate_block_registry() {
let dups = duplicate_block_names();
assert!(
dups.is_empty(),
"rustango_cms: duplicate block registrations: {dups:?}",
);
if let Err(e) = check_block_references() {
panic!("rustango_cms: block registry has a dangling reference: {e}");
}
}