-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathupsert_repository.rs
More file actions
55 lines (43 loc) · 1.58 KB
/
upsert_repository.rs
File metadata and controls
55 lines (43 loc) · 1.58 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use async_trait::async_trait;
#[async_trait]
/// Represents a repository for policies
pub trait UpsertRepository<Key, Entity>: ReadOnlyRepository<Key, Entity> + Send + Sync {
type Error;
/// Updates or inserts a policy by id
async fn upsert(&self, key: Key, entity: Entity) -> Result<Entity, Self::Error>;
/// Checks if an object exists
async fn exists(&self, key: Key) -> Result<bool, Self::Error>;
}
#[async_trait]
/// Represents a repository for policies
pub trait ReadOnlyRepository<Key, Entity>: Send + Sync {
type ReadError;
/// Retrieves a policy by id
async fn get(&self, key: Key) -> Result<Entity, Self::ReadError>;
}
#[async_trait]
pub trait ValueFactory<Key, Entity>: Send + Sync {
type CreateError;
async fn create(&self, key: &Key) -> Result<Entity, Self::CreateError>;
}
#[async_trait]
/// Represents a repository for policies
pub trait ReadOnlyRepositoryWithFactory<Key, Entity>: Send + Sync {
type ReadError;
/// Retrieves a policy by id
async fn get(
&self,
key: Key,
create_new: &dyn ValueFactory<Key, Entity, CreateError = Self::ReadError>,
) -> Result<Entity, Self::ReadError>;
}
#[async_trait]
/// Represents a repository for policies
pub trait CanDelete<Key, Entity>: Send + Sync {
type DeleteError;
/// Retrieves a policy by id
async fn delete(&self, key: Key) -> Result<(), Self::DeleteError>;
}
pub trait UpsertRepositoryWithDelete<Key, Entity>: UpsertRepository<Key, Entity> + CanDelete<Key, Entity> {
// This trait is a marker trait that combines UpsertRepository and CanDelete
}