pub mod collection;
mod schematic;
pub mod view;
use std::{
borrow::Cow,
fmt::{Debug, Display},
};
use serde::{Deserialize, Serialize};
pub use self::{collection::*, schematic::*, view::*};
#[derive(Hash, PartialEq, Eq, Deserialize, Serialize, Debug, Clone)]
#[serde(transparent)]
pub struct Id(Cow<'static, str>);
impl AsRef<str> for Id {
fn as_ref(&self) -> &str {
self.0.as_ref()
}
}
impl Display for Id {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
Display::fmt(&self.0, f)
}
}
impl Id {
pub fn new<S: Into<String>>(id: S) -> Self {
Self(Cow::Owned(id.into()))
}
}
impl From<&'static str> for Id {
fn from(id: &'static str) -> Self {
Self(Cow::Borrowed(id))
}
}
pub trait Schema: Send + Sync + Debug + 'static {
fn schema_id() -> Id;
fn define_collections(schema: &mut Schematic);
#[must_use]
fn schematic() -> Schematic {
let mut schematic = Schematic::default();
Self::define_collections(&mut schematic);
schematic
}
}
impl Schema for () {
fn schema_id() -> Id {
Id::from("")
}
fn define_collections(_schema: &mut Schematic) {}
}
impl<T> Schema for T
where
T: Collection + 'static,
{
fn schema_id() -> Id {
Id(Self::collection_id().0)
}
fn define_collections(schema: &mut Schematic) {
schema.define_collection::<Self>();
}
}