#![allow(clippy::bad_bit_mask)]
use codec::{self, Encode, EncodeLike, Input, Output};
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub enum ObservedRole {
Full,
Light,
Authority,
}
impl ObservedRole {
pub fn is_light(&self) -> bool {
matches!(self, Self::Light)
}
}
impl From<Roles> for ObservedRole {
fn from(roles: Roles) -> Self {
if roles.is_authority() {
ObservedRole::Authority
} else if roles.is_full() {
ObservedRole::Full
} else {
ObservedRole::Light
}
}
}
#[derive(Debug, Clone, Copy)]
pub enum Role {
Full,
Authority,
}
impl Role {
pub fn is_authority(&self) -> bool {
matches!(self, Self::Authority)
}
}
impl std::fmt::Display for Role {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Self::Full => write!(f, "FULL"),
Self::Authority => write!(f, "AUTHORITY"),
}
}
}
bitflags::bitflags! {
pub struct Roles: u8 {
const NONE = 0b00000000;
const FULL = 0b00000001;
const LIGHT = 0b00000010;
const AUTHORITY = 0b00000100;
}
}
impl Roles {
pub fn is_full(&self) -> bool {
self.intersects(Self::FULL | Self::AUTHORITY)
}
pub fn is_authority(&self) -> bool {
*self == Self::AUTHORITY
}
pub fn is_light(&self) -> bool {
!self.is_full()
}
}
impl<'a> From<&'a Role> for Roles {
fn from(roles: &'a Role) -> Self {
match roles {
Role::Full => Self::FULL,
Role::Authority => Self::AUTHORITY,
}
}
}
impl Encode for Roles {
fn encode_to<T: Output + ?Sized>(&self, dest: &mut T) {
dest.push_byte(self.bits())
}
}
impl EncodeLike for Roles {}
impl codec::Decode for Roles {
fn decode<I: Input>(input: &mut I) -> Result<Self, codec::Error> {
Self::from_bits(input.read_byte()?).ok_or_else(|| codec::Error::from("Invalid bytes"))
}
}