53 lines
1.5 KiB
Rust
53 lines
1.5 KiB
Rust
use sqlx::error::BoxDynError;
|
|
use sqlx::{Database, Decode, Encode, Postgres, Type};
|
|
use sqlx::encode::IsNull;
|
|
|
|
#[repr(i32)]
|
|
#[derive(Clone, Copy)]
|
|
pub enum DefaultMessageNotificationSetting {
|
|
All = 0,
|
|
MentionsOnly = 1,
|
|
None = 2,
|
|
}
|
|
|
|
|
|
impl Into<i32> for &DefaultMessageNotificationSetting {
|
|
fn into(self) -> i32 {
|
|
*self as i32
|
|
}
|
|
}
|
|
|
|
impl TryFrom<i32> for DefaultMessageNotificationSetting {
|
|
type Error = BoxDynError;
|
|
|
|
fn try_from(value: i32) -> Result<Self, Self::Error> {
|
|
match value {
|
|
0 => Ok(Self::All),
|
|
1 => Ok(Self::MentionsOnly),
|
|
2 => Ok(Self::None),
|
|
_ => Err(format!("Unknown value {}", value).into()),
|
|
}
|
|
}
|
|
}
|
|
|
|
impl Type<Postgres> for DefaultMessageNotificationSetting {
|
|
fn type_info() -> <Postgres as Database>::TypeInfo {
|
|
<i32 as Type<Postgres>>::type_info()
|
|
}
|
|
|
|
fn compatible(ty: &<Postgres as Database>::TypeInfo) -> bool {
|
|
<i32 as Type<Postgres>>::compatible(ty)
|
|
}
|
|
}
|
|
|
|
impl Encode<'_, Postgres> for DefaultMessageNotificationSetting {
|
|
fn encode_by_ref(&self, buf: &mut <Postgres as Database>::ArgumentBuffer<'_>) -> Result<IsNull, BoxDynError> {
|
|
<i32 as sqlx::Encode<Postgres>>::encode(self.into(), buf)
|
|
}
|
|
}
|
|
|
|
impl Decode<'_, Postgres> for DefaultMessageNotificationSetting {
|
|
fn decode(value: <Postgres as Database>::ValueRef<'_>) -> Result<Self, BoxDynError> {
|
|
<i32 as Decode<Postgres>>::decode(value).map(Self::try_from)?
|
|
}
|
|
} |