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
mod collection;
mod names;
mod schematic;
pub mod view;
use std::fmt::Debug;
pub use self::{
collection::Collection,
names::{Authority, CollectionName, InvalidNameError, Name, SchemaName, ViewName},
schematic::Schematic,
view::{
map::{Key, Map, MappedDocument, MappedValue},
MapResult, View,
},
};
use crate::Error;
pub trait Schema: Send + Sync + Debug + 'static {
fn schema_name() -> Result<SchemaName, InvalidNameError>;
fn define_collections(schema: &mut Schematic) -> Result<(), Error>;
fn schematic() -> Result<Schematic, Error> {
let mut schematic = Schematic::default();
Self::define_collections(&mut schematic)?;
Ok(schematic)
}
}
impl Schema for () {
fn schema_name() -> Result<SchemaName, InvalidNameError> {
SchemaName::new("", "")
}
fn define_collections(_schema: &mut Schematic) -> Result<(), Error> {
Ok(())
}
}
impl<T> Schema for T
where
T: Collection + 'static,
{
fn schema_name() -> Result<SchemaName, InvalidNameError> {
let CollectionName { authority, name } = Self::collection_name()?;
Ok(SchemaName { authority, name })
}
fn define_collections(schema: &mut Schematic) -> Result<(), Error> {
schema.define_collection::<Self>()
}
}