use snarkvm::prelude::{FromBytes, ToBytes, error};
use serde::{Deserialize, Serialize};
use std::io;
#[derive(Copy, Clone, Debug, Eq, PartialEq, Serialize, Deserialize, Hash)]
#[repr(u8)]
pub enum NodeType {
Client = 0,
Prover,
Validator,
BootstrapClient,
}
impl NodeType {
pub const fn description(&self) -> &str {
match self {
Self::Client => "a client node",
Self::Prover => "a prover node",
Self::Validator => "a validator node",
Self::BootstrapClient => "a bootstrap client node",
}
}
pub const fn is_client(&self) -> bool {
matches!(self, Self::Client)
}
pub const fn is_prover(&self) -> bool {
matches!(self, Self::Prover)
}
pub const fn is_validator(&self) -> bool {
matches!(self, Self::Validator)
}
}
impl core::fmt::Display for NodeType {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
write!(f, "{}", match self {
Self::Client => "Client",
Self::Prover => "Prover",
Self::Validator => "Validator",
Self::BootstrapClient => "Bootstrap Client",
})
}
}
impl ToBytes for NodeType {
fn write_le<W: io::Write>(&self, writer: W) -> io::Result<()> {
(*self as u8).write_le(writer)
}
}
impl FromBytes for NodeType {
fn read_le<R: io::Read>(reader: R) -> io::Result<Self> {
match u8::read_le(reader)? {
0 => Ok(Self::Client),
1 => Ok(Self::Prover),
2 => Ok(Self::Validator),
3 => Ok(Self::BootstrapClient),
x => Err(error(format!("Invalid node type: expected 0..=3, got {x}."))),
}
}
}