ankurah_core/property/
traits.rs

1use ankurah_proto::{Clock, ClockOrdering};
2use anyhow::Result;
3
4use crate::{error::RetrievalError, model::Entity, property::PropertyName};
5
6use thiserror::Error;
7
8use super::PropertyValue;
9
10pub trait InitializeWith<T> {
11    fn initialize_with(entity: &Entity, property_name: PropertyName, value: &T) -> Self;
12}
13
14#[derive(Error, Debug)]
15pub enum PropertyError {
16    #[error("property is missing")]
17    Missing,
18    #[error("deserialization error: {0}")]
19    DeserializeError(Box<dyn std::error::Error + Send + Sync>),
20    #[error("retrieval error: {0}")]
21    RetrievalError(crate::error::RetrievalError),
22    #[error("invalid variant `{given}` for `{ty}`")]
23    InvalidVariant { given: PropertyValue, ty: String },
24    #[error("invalid value `{value}` for `{ty}`")]
25    InvalidValue { value: String, ty: String },
26}
27
28impl PartialEq for PropertyError {
29    fn eq(&self, other: &Self) -> bool { self.to_string() == other.to_string() }
30}
31
32#[cfg(feature = "wasm")]
33impl Into<wasm_bindgen::JsValue> for PropertyError {
34    fn into(self) -> wasm_bindgen::JsValue { wasm_bindgen::JsValue::from_str(&self.to_string()) }
35}
36
37impl From<RetrievalError> for PropertyError {
38    fn from(retrieval: RetrievalError) -> Self { PropertyError::RetrievalError(retrieval) }
39}
40
41pub trait FromEntity {
42    fn from_entity(property_name: PropertyName, entity: &Entity) -> Self;
43}
44
45pub trait FromActiveType<A> {
46    fn from_active(active: A) -> Result<Self, PropertyError>
47    where Self: Sized;
48}
49
50pub fn compare_clocks(clock: &Clock, other: &Clock /*, context: &Box<dyn TContext>*/) -> ClockOrdering {
51    let ulid1 = clock.as_slice().iter().max();
52    let ulid2 = other.as_slice().iter().max();
53
54    if ulid1 > ulid2 {
55        ClockOrdering::Child
56    } else if ulid1 < ulid2 {
57        ClockOrdering::Parent
58    } else {
59        ClockOrdering::Sibling
60    }
61}
62
63/*
64impl<A, T> FromActiveType<A> for Option<T>
65where T: FromActiveType<A> {
66    fn from_active(active: Result<A, PropertyError>) -> Result<Option<T>, PropertyError> {
67        match T::from_active(active) {
68            Ok(projected) => {
69                Ok(Some(projected))
70            }
71            Err(PropertyError::Missing) => Ok(None),
72            Err(err) => Err(err),
73        }
74    }
75}
76*/