use crate::{EntitySchemaInfo, Schema, SchemaBuilder};
use tracing::debug;
#[derive(derive_more::Debug)]
pub struct EntityRegistry {
pub module_path: &'static str,
#[debug(skip)]
pub schema_info: fn(&Schema) -> EntitySchemaInfo,
}
inventory::collect!(EntityRegistry);
pub use inventory::submit as register_entity;
impl EntityRegistry {
pub fn build_schema(schema: Schema, prefix: &str) -> SchemaBuilder {
let mut schema = SchemaBuilder::new(schema);
let mut string;
let mut prefix = prefix.trim_end_matches("*");
if !prefix.contains("::") {
string = format!("{prefix}::");
prefix = &string;
}
if let Some((left, right)) = prefix.split_once("::") {
if left.contains("-") {
let left = left.replace('-', "_");
string = format!("{left}::{right}");
prefix = &string;
}
}
debug!("Registering entities with prefix `{prefix}`");
for entity in inventory::iter::<crate::EntityRegistry>() {
if entity.module_path.starts_with(prefix) {
schema.register_entity((entity.schema_info)(schema.helper()));
debug!("Registered {}", entity.module_path);
} else {
debug!("Skipped {}", entity.module_path);
}
}
schema
}
}