1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
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>();
}
}