ankurah_core/
model.rs

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
11/// A model is a struct that represents the present values for a given entity
12/// Schema is defined primarily by the Model object, and the View is derived from that via macro.
13pub 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
20/// A read only view of an Entity which offers typed accessors
21pub 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
32/// A mutable Model instance for an Entity with typed accessors.
33/// It is associated with a transaction, and may not outlive said transaction.
34pub 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            // If there is an upstream, use it
51            Some(upstream) => upstream.clone(),
52            // Else we're a new Entity, and we have to rely on the commit to add this to the node
53            None => inner.clone(),
54        };
55
56        Self::View::from_entity(new_inner)
57    }
58}