diff --git a/src/types/utils/rights.rs b/src/types/utils/rights.rs index 63bbeb2..e4fff31 100644 --- a/src/types/utils/rights.rs +++ b/src/types/utils/rights.rs @@ -4,6 +4,10 @@ use bitflags::bitflags; use serde::{Deserialize, Serialize}; +use tokio::io::AsyncWriteExt; + +#[cfg(feature = "sqlx")] +use sqlx::{{Decode, Encode, MySql}, database::{HasArguments, HasValueRef}, encode::IsNull, error::BoxDynError, mysql::MySqlValueRef}; bitflags! { /// Rights are instance-wide, per-user permissions for everything you may perform on the instance, @@ -129,6 +133,33 @@ bitflags! { } } +#[cfg(feature = "sqlx")] +impl sqlx::Type for Rights { + fn type_info() -> ::TypeInfo { + u64::type_info() + } + + fn compatible(ty: &::TypeInfo) -> bool { + u64::compatible(ty) + } +} + +#[cfg(feature = "sqlx")] +impl<'q> Encode<'q, MySql> for Rights { + fn encode_by_ref(&self, buf: &mut >::ArgumentBuffer) -> IsNull { + >::encode_by_ref(&self.0.0, buf) + } +} + +#[cfg(feature = "sqlx")] +impl<'r> Decode<'r, MySql> for Rights { + fn decode(value: >::ValueRef) -> Result { + let raw = >::decode(value)?; + Ok(Rights::from_bits(raw).unwrap()) + } +} + + impl Rights { pub fn any(&self, permission: Rights, check_operator: bool) -> bool { (check_operator && self.contains(Rights::OPERATOR)) || self.contains(permission)