ankurah_core/property/
traits.rs

1use anyhow::Result;
2
3use crate::{entity::Entity, error::RetrievalError, property::PropertyName, value::CastError};
4
5use thiserror::Error;
6
7use super::Value;
8
9pub trait InitializeWith<T> {
10    fn initialize_with(entity: &Entity, property_name: PropertyName, value: &T) -> Self;
11}
12
13#[derive(Error, Debug)]
14#[cfg_attr(feature = "uniffi", derive(uniffi::Error))]
15#[cfg_attr(feature = "uniffi", uniffi(flat_error))]
16pub enum PropertyError {
17    #[error("property is missing")]
18    Missing,
19
20    // #[error("property is missing: {name} in collection: {collection}")]
21    // NotFoundInBackend { backend: &'static str, name: PropertyName },
22    #[error("serialization error: {0}")]
23    SerializeError(Box<dyn std::error::Error + Send + Sync>),
24    #[error("deserialization error: {0}")]
25    DeserializeError(Box<dyn std::error::Error + Send + Sync + 'static>),
26    #[error("retrieval error: {0}")]
27    RetrievalError(crate::error::RetrievalError),
28    #[error("invalid variant `{given}` for `{ty}`")]
29    InvalidVariant { given: Value, ty: String },
30    #[error("invalid value `{value}` for `{ty}`")]
31    InvalidValue { value: String, ty: String },
32    #[error("transaction is no longer alive")]
33    TransactionClosed,
34
35    #[error("cast error: {0}")]
36    CastError(CastError),
37}
38
39impl PartialEq for PropertyError {
40    fn eq(&self, other: &Self) -> bool { self.to_string() == other.to_string() }
41}
42
43impl From<PropertyError> for std::fmt::Error {
44    fn from(_: PropertyError) -> std::fmt::Error { std::fmt::Error }
45}
46
47#[cfg(feature = "wasm")]
48impl From<PropertyError> for wasm_bindgen::JsValue {
49    fn from(val: PropertyError) -> Self { wasm_bindgen::JsValue::from_str(&val.to_string()) }
50}
51
52impl From<RetrievalError> for PropertyError {
53    fn from(retrieval: RetrievalError) -> Self { PropertyError::RetrievalError(retrieval) }
54}
55
56impl From<serde_json::Error> for PropertyError {
57    fn from(e: serde_json::Error) -> Self { PropertyError::SerializeError(Box::new(e)) }
58}
59
60pub trait FromEntity {
61    fn from_entity(property_name: PropertyName, entity: &Entity) -> Self;
62}
63
64pub trait FromActiveType<A> {
65    fn from_active(active: A) -> Result<Self, PropertyError>
66    where Self: Sized;
67}
68
69/*
70impl<A, T> FromActiveType<A> for Option<T>
71where T: FromActiveType<A> {
72    fn from_active(active: Result<A, PropertyError>) -> Result<Option<T>, PropertyError> {
73        match T::from_active(active) {
74            Ok(projected) => {
75                Ok(Some(projected))
76            }
77            Err(PropertyError::Missing) => Ok(None),
78            Err(err) => Err(err),
79        }
80    }
81}
82*/