scufflecloud_core/models/
mod.rs

1mod mfa;
2mod organizations;
3mod sessions;
4mod users;
5
6pub(crate) use mfa::*;
7pub(crate) use organizations::*;
8pub(crate) use sessions::*;
9pub(crate) use users::*;
10
11/// A macro helper to implement the `ToSql` and `FromSql` traits for an enum.
12/// Unfortunately diesel doesn't automatically generate these for enums, so we
13/// have to do it manually. This means we need to make sure that this enum
14/// matches the definition in the database.
15macro_rules! impl_enum {
16    ($enum:ident, $sql_type:ty, {
17        $(
18            $variant:ident => $value:literal
19        ),*$(,)?
20    }) => {
21        #[derive(Debug, Clone, Copy, PartialEq, Eq, ::diesel::deserialize::FromSqlRow, ::diesel::expression::AsExpression, serde_derive::Serialize)]
22        #[diesel(sql_type = $sql_type)]
23        pub enum $enum {
24            $(
25                $variant,
26            )*
27        }
28
29        const _: () = {
30            impl ::std::fmt::Display for $enum {
31                fn fmt(&self, f: &mut ::std::fmt::Formatter<'_>) -> ::std::fmt::Result {
32                    match self {
33                        $(
34                            $enum::$variant => ::std::write!(f, "{}", stringify!($variant)),
35                        )*
36                    }
37                }
38            }
39
40            impl ::diesel::serialize::ToSql<$sql_type, ::diesel::pg::Pg> for $enum {
41                fn to_sql<'b>(&'b self, out: &mut ::diesel::serialize::Output<'b, '_, ::diesel::pg::Pg>) -> ::diesel::serialize::Result {
42                    match self {
43                        $(
44                            $enum::$variant => ::std::io::Write::write_all(out, $value)?,
45                        )*
46                    };
47
48                    Ok(::diesel::serialize::IsNull::No)
49                }
50            }
51
52            impl ::diesel::deserialize::FromSql<$sql_type, ::diesel::pg::Pg> for $enum {
53                fn from_sql(bytes: <::diesel::pg::Pg as ::diesel::backend::Backend>::RawValue<'_>) -> ::diesel::deserialize::Result<Self> {
54                    match bytes.as_bytes() {
55                        $(
56                            $value => Ok($enum::$variant),
57                        )*
58                        bytes => Err(format!("invalid {}: {:?}", stringify!($enum), bytes).into()),
59                    }
60                }
61            }
62        };
63    };
64}
65
66pub(crate) use impl_enum;