use super::{Shape, SourceLocation, TypeDesc, TypeRegistry};
pub trait View: Shape + serde::Serialize {
const NAME: &'static str;
fn into_view(self) -> ViewModel<Self>
where
Self: Sized,
{
view(self)
}
}
impl<T: View + ?Sized> View for &T {
const NAME: &'static str = T::NAME;
}
#[derive(Clone, Debug)]
pub struct ViewModel<T: View> {
pub name: &'static str,
pub model: T,
}
pub fn view<T: View>(model: T) -> ViewModel<T> {
ViewModel {
name: T::NAME,
model,
}
}
pub trait Shared: Shape + serde::Serialize {}
#[derive(Clone, Copy)]
pub struct ViewRegistration {
pub name: &'static str,
pub source: SourceLocation,
pub describe: fn(&mut TypeRegistry) -> TypeDesc,
}
#[derive(Clone, Copy)]
pub struct SharedRegistration {
pub type_name: fn() -> &'static str,
pub source: SourceLocation,
pub describe: fn(&mut TypeRegistry) -> TypeDesc,
}
inventory::collect!(ViewRegistration);
inventory::collect!(SharedRegistration);
pub fn view_models() -> impl Iterator<Item = &'static ViewRegistration> {
inventory::iter::<ViewRegistration>.into_iter()
}
pub fn shared_models() -> impl Iterator<Item = &'static SharedRegistration> {
inventory::iter::<SharedRegistration>.into_iter()
}