AccessLevel

Derive Macro AccessLevel 

Source
#[derive(AccessLevel)]
Expand description

Derives the AccessLevel trait for enums.

This macro automatically implements from_str and as_str methods for your access level enum, using the variant names as string representations.

§Example

use nut_shell_macros::AccessLevel;

#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, AccessLevel)]
pub enum MyAccessLevel {
    Guest = 0,
    User = 1,
    Admin = 2,
}

This generates:

impl AccessLevel for MyAccessLevel {
    fn from_str(s: &str) -> Option<Self> {
        match s {
            "Guest" => Some(Self::Guest),
            "User" => Some(Self::User),
            "Admin" => Some(Self::Admin),
            _ => None,
        }
    }

    fn as_str(&self) -> &'static str {
        match self {
            Self::Guest => "Guest",
            Self::User => "User",
            Self::Admin => "Admin",
        }
    }
}

§Requirements

  • The type must be an enum
  • All variants must be unit variants (no fields)
  • Variant names will be used as the string representation