1pub mod tsify;
2
3use ankurah_proto::{CollectionId, State, ID};
4
5use crate::entity::Entity;
6use crate::property::Backends;
7use crate::property::PropertyError;
8
9use anyhow::Result;
10
11pub trait Model {
14 type View: View;
15 type Mutable<'trx>: Mutable<'trx>;
16 fn collection() -> CollectionId;
17 fn create_entity(&self, id: ID) -> Entity;
18}
19
20pub trait View {
22 type Model: Model;
23 type Mutable<'trx>: Mutable<'trx>;
24 fn id(&self) -> ID { self.entity().id }
25 fn backends(&self) -> &Backends { self.entity().backends() }
26 fn collection() -> CollectionId { <Self::Model as Model>::collection() }
27 fn entity(&self) -> &Entity;
28 fn from_entity(inner: Entity) -> Self;
29 fn to_model(&self) -> Result<Self::Model, PropertyError>;
30}
31
32pub trait Mutable<'rec> {
35 type Model: Model;
36 type View: View;
37 fn id(&self) -> ID { self.entity().id }
38 fn collection() -> CollectionId { <Self::Model as Model>::collection() }
39 fn backends(&self) -> &Backends { &self.entity().backends }
40 fn entity(&self) -> &Entity;
41 fn new(inner: &'rec Entity) -> Self
42 where Self: Sized;
43
44 fn state(&self) -> anyhow::Result<State> { self.entity().to_state() }
45
46 fn read(&self) -> Self::View {
47 let inner = self.entity();
48
49 let new_inner = match &inner.upstream {
50 Some(upstream) => upstream.clone(),
52 None => inner.clone(),
54 };
55
56 Self::View::from_entity(new_inner)
57 }
58}