use std::
{
result,
str::FromStr,
fmt::
{
Display,
Formatter,
Result,
},
};
use wincode::{ SchemaWrite, SchemaRead };
macro_rules! roles
{
($($variant:ident => $name:literal,)+) =>
{
#[derive(SchemaWrite, SchemaRead, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Debug, Default)]
pub enum Role
{
#[default] $($variant),+
}
impl Role
{
pub const ALL: &'static [Role] = &[$(Role::$variant),+];
pub fn name(&self) -> &'static str {
match self
{
$(Role::$variant => $name),+
}
}
}
};
}
roles!
{
User => "user", Moderator => "moderator", Owner => "owner", }
impl Display for Role
{
fn fmt(&self, f: &mut Formatter<'_>) -> Result { write!(f, "{}", self.name()) }
}
impl FromStr for Role
{
type Err = ();
fn from_str(text: &str) -> result::Result<Self, Self::Err>
{
Self::ALL.iter().find(|role| role.name().eq_ignore_ascii_case(text.trim())).copied().ok_or(())
}
}