use super::{Error, ErrorKind::ProtocolError};
use std::fmt::{self, Display};
pub const MAX_SESSION_ID: Id = Id(16);
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct Id(u8);
impl Id {
pub fn from_u8(id: u8) -> Result<Self, Error> {
if id > MAX_SESSION_ID.0 {
fail!(
ProtocolError,
"session ID exceeds the maximum allowed: {} (max {})",
id,
MAX_SESSION_ID.0
);
}
Ok(Id(id))
}
pub fn succ(self) -> Result<Self, Error> {
Self::from_u8(self.0 + 1)
}
pub fn to_u8(self) -> u8 {
self.0
}
}
impl Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}