Skip to main content

commit_bridge/domain/
mod.rs

1//! Domain models for the application.
2
3pub mod accept_header;
4pub mod api_version;
5pub mod branch_name;
6pub mod commit_hash;
7pub mod event_type;
8pub mod non_empty_string;
9pub mod repo_url;
10pub mod target_repo;
11
12pub use accept_header::AcceptHeader;
13pub use api_version::ApiVersion;
14pub use branch_name::BranchName;
15pub use commit_hash::CommitHash;
16pub use event_type::EventType;
17pub use non_empty_string::NonEmptyString;
18pub use repo_url::RepoUrl;
19pub use target_repo::TargetRepo;
20
21/// Derives `sqlx` trait implementations for a type that implements `TryFrom<String>`.
22///
23/// This macro implements `sqlx::Type`, `sqlx::Decode`, and `sqlx::Encode` for the
24/// specified type, assuming it can be converted to/from a `String` and uses
25/// the same underlying representation as `String` in the database.
26#[macro_export]
27macro_rules! derive_sqlx_traits {
28    ($t:ty) => {
29        impl sqlx::Type<sqlx::Sqlite> for $t {
30            fn type_info() -> sqlx::sqlite::SqliteTypeInfo {
31                <String as sqlx::Type<sqlx::Sqlite>>::type_info()
32            }
33        }
34        impl<'r> sqlx::Decode<'r, sqlx::Sqlite> for $t {
35            fn decode(
36                value: sqlx::sqlite::SqliteValueRef<'r>,
37            ) -> Result<Self, sqlx::error::BoxDynError> {
38                let s = <String as sqlx::Decode<sqlx::Sqlite>>::decode(value)?;
39                <$t>::try_from(s).map_err(|e| Box::new(e) as sqlx::error::BoxDynError)
40            }
41        }
42        impl sqlx::Encode<'_, sqlx::Sqlite> for $t {
43            fn encode_by_ref(
44                &self,
45                buf: &mut Vec<sqlx::sqlite::SqliteArgumentValue<'_>>,
46            ) -> Result<sqlx::encode::IsNull, Box<dyn std::error::Error + Send + Sync>> {
47                <String as sqlx::Encode<sqlx::Sqlite>>::encode_by_ref(&self.to_string(), buf)
48            }
49        }
50    };
51}