use solana_program_error::ProgramError;
#[derive(Clone, Copy, Debug, Eq, PartialEq, num_enum::TryFromPrimitive)]
#[cfg_attr(feature = "cli", derive(clap::ValueEnum))]
#[repr(u8)]
#[num_enum(error_type(name = ProgramError, constructor = Role::unknown_role))]
pub enum Role {
Manager = 0,
ReclaimAuthority,
SelfOrderAuthority,
}
impl Role {
pub const ALL: [Self; 3] = [
Role::Manager,
Role::ReclaimAuthority,
Role::SelfOrderAuthority,
];
pub fn discriminator(self) -> u8 {
self as u8
}
fn unknown_role(_: u8) -> ProgramError {
ProgramError::InvalidInstructionData
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn role_try_from_partitions_all_bytes() {
for i in u8::MIN..=u8::MAX {
match Role::try_from(i) {
Ok(role) => assert_eq!(role as u8, i),
Err(err) => assert_eq!(err, ProgramError::InvalidInstructionData),
}
}
}
#[test]
fn role_try_from_matches_manager() {
assert_eq!(Role::try_from(0), Ok(Role::Manager));
}
#[test]
fn all_roles_lists_every_role_in_discriminator_order() {
let every_role: Vec<Role> = (u8::MIN..=u8::MAX)
.filter_map(|byte| Role::try_from(byte).ok())
.collect();
assert_eq!(Role::ALL.as_slice(), every_role.as_slice());
}
}