systemprompt_traits/
repository.rs

1use async_trait::async_trait;
2
3#[async_trait]
4pub trait Repository: Send + Sync {
5    type Pool;
6    type Error: std::error::Error + Send + Sync + 'static;
7
8    fn pool(&self) -> &Self::Pool;
9}
10
11#[async_trait]
12pub trait CrudRepository<T>: Repository {
13    type Id;
14
15    async fn create(&self, entity: T) -> Result<T, Self::Error>;
16    async fn get(&self, id: Self::Id) -> Result<Option<T>, Self::Error>;
17    async fn update(&self, entity: T) -> Result<T, Self::Error>;
18    async fn delete(&self, id: Self::Id) -> Result<(), Self::Error>;
19    async fn list(&self) -> Result<Vec<T>, Self::Error>;
20}
21
22#[derive(Debug, thiserror::Error)]
23#[non_exhaustive]
24pub enum RepositoryError {
25    #[error("database error: {0}")]
26    Database(Box<dyn std::error::Error + Send + Sync>),
27
28    #[error("entity not found: {0}")]
29    NotFound(String),
30
31    #[error("serialization error: {0}")]
32    Serialization(#[from] serde_json::Error),
33
34    #[error("invalid data: {0}")]
35    InvalidData(String),
36
37    #[error("constraint violation: {0}")]
38    ConstraintViolation(String),
39
40    #[error("{0}")]
41    Other(#[from] anyhow::Error),
42}
43
44impl RepositoryError {
45    pub fn database(err: impl std::error::Error + Send + Sync + 'static) -> Self {
46        Self::Database(Box::new(err))
47    }
48}