use super::{SessionError, SessionErrorKind::ProtocolError};
pub const MAX_SESSION_ID: SessionId = SessionId(16);
#[derive(Copy, Clone, Debug, Eq, Hash, PartialEq, PartialOrd, Ord)]
pub struct SessionId(u8);
impl SessionId {
pub fn from_u8(id: u8) -> Result<Self, SessionError> {
if id > MAX_SESSION_ID.0 {
fail!(
ProtocolError,
"session ID exceeds the maximum allowed: {} (max {})",
id,
MAX_SESSION_ID.0
);
}
Ok(SessionId(id))
}
pub fn succ(self) -> Result<Self, SessionError> {
Self::from_u8(self.0 + 1)
}
pub fn to_u8(self) -> u8 {
self.0
}
}