Skip to main content

rorm_db/
choice.rs

1//! Wrapper around string which is decodable as an enum
2
3/// Wrapper around string which is decodable as an enum
4pub struct Choice(pub String);
5
6#[allow(non_local_definitions)]
7const _: () = {
8    use sqlx::database::Database;
9    use sqlx::error::BoxDynError;
10    use sqlx::{Decode, Type};
11
12    #[cfg(feature = "postgres")]
13    const _: () = {
14        use sqlx::Postgres;
15        impl Type<Postgres> for Choice {
16            fn type_info() -> <Postgres as Database>::TypeInfo {
17                <str as Type<Postgres>>::type_info()
18            }
19            fn compatible(_ty: &<Postgres as Database>::TypeInfo) -> bool {
20                true // ugly but the only possible solution at the moment
21            }
22        }
23        impl<'r> Decode<'r, Postgres> for Choice {
24            fn decode(value: <Postgres as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
25                <String as Decode<'r, Postgres>>::decode(value).map(Self)
26            }
27        }
28    };
29
30    #[cfg(feature = "sqlite")]
31    const _: () = {
32        use sqlx::Sqlite;
33        impl Type<Sqlite> for Choice {
34            fn type_info() -> <Sqlite as Database>::TypeInfo {
35                <str as Type<Sqlite>>::type_info()
36            }
37            fn compatible(ty: &<Sqlite as Database>::TypeInfo) -> bool {
38                <str as Type<Sqlite>>::compatible(ty)
39            }
40        }
41        impl<'r> Decode<'r, Sqlite> for Choice {
42            fn decode(value: <Sqlite as Database>::ValueRef<'r>) -> Result<Self, BoxDynError> {
43                <String as Decode<'r, Sqlite>>::decode(value).map(Self)
44            }
45        }
46    };
47};