es_entity/
traits.rs

1use async_trait::async_trait;
2use serde::{de::DeserializeOwned, Serialize};
3
4use super::{error::EsEntityError, events::EntityEvents, nested::*};
5
6pub trait EsEvent: DeserializeOwned + Serialize + Send + Sync {
7    type EntityId: Clone
8        + PartialEq
9        + sqlx::Type<sqlx::Postgres>
10        + Eq
11        + std::hash::Hash
12        + Send
13        + Sync;
14}
15
16pub trait IntoEvents<E: EsEvent> {
17    fn into_events(self) -> EntityEvents<E>;
18}
19
20pub trait TryFromEvents<E: EsEvent> {
21    fn try_from_events(events: EntityEvents<E>) -> Result<Self, EsEntityError>
22    where
23        Self: Sized;
24}
25
26pub trait EsEntity: TryFromEvents<Self::Event> {
27    type Event: EsEvent;
28    type New: IntoEvents<Self::Event>;
29
30    fn events(&self) -> &EntityEvents<Self::Event>;
31    fn events_mut(&mut self) -> &mut EntityEvents<Self::Event>;
32}
33
34pub trait Parent<T: EsEntity> {
35    fn nested(&self) -> &Nested<T>;
36    fn nested_mut(&mut self) -> &mut Nested<T>;
37}
38
39pub trait EsRepo {
40    type Entity: EsEntity;
41    type Err: From<EsEntityError>;
42}
43
44#[async_trait]
45pub trait PopulateNested<C>: EsRepo {
46    async fn populate(
47        &self,
48        lookup: std::collections::HashMap<C, &mut Nested<<Self as EsRepo>::Entity>>,
49    ) -> Result<(), <Self as EsRepo>::Err>;
50}
51
52pub trait RetryableInto<T>: Into<T> + Copy + std::fmt::Debug {}
53impl<T, O> RetryableInto<O> for T where T: Into<O> + Copy + std::fmt::Debug {}