Skip to main content

systemprompt_database/repository/
macros.rs

1#[macro_export]
2macro_rules! impl_repository_new {
3    ($repo:ty) => {
4        impl $repo {
5            #[must_use]
6            pub fn new(db: &$crate::DbPool) -> Self {
7                let pool = db.get_postgres_pool().expect("Database must be PostgreSQL");
8                Self { pool }
9            }
10
11            pub fn try_new(db: &$crate::DbPool) -> Result<Self, $crate::RepositoryError> {
12                let pool = db.get_postgres_pool().ok_or_else(|| {
13                    $crate::RepositoryError::internal("Database must be PostgreSQL")
14                })?;
15                Ok(Self { pool })
16            }
17
18            #[must_use]
19            pub const fn from_pool(pool: $crate::PgDbPool) -> Self {
20                Self { pool }
21            }
22        }
23    };
24}
25
26#[macro_export]
27macro_rules! define_repository {
28    ($repo:ident) => {
29        #[derive(Debug, Clone)]
30        pub struct $repo {
31            pool: $crate::PgDbPool,
32        }
33
34        $crate::impl_repository_new!($repo);
35    };
36
37    ($repo:ident, $visibility:vis) => {
38        #[derive(Debug, Clone)]
39        $visibility struct $repo {
40            pool: $crate::PgDbPool,
41        }
42
43        $crate::impl_repository_new!($repo);
44    };
45}
46
47#[macro_export]
48macro_rules! impl_repository_pool {
49    ($repo:ty) => {
50        impl $repo {
51            #[must_use]
52            pub fn pool(&self) -> &$crate::PgDbPool {
53                &self.pool
54            }
55
56            #[must_use]
57            pub fn pg_pool(&self) -> &::sqlx::PgPool {
58                &self.pool
59            }
60        }
61    };
62}