use crate::Error;
use serde::{Deserialize, Serialize};
use std::fmt;
pub const PROTOCOL_NAME: &str = "doldskrift";
pub const PROTOCOL_VERSION: u8 = 1;
pub const PROTOCOL_VERSION_PROTECTED: u8 = 3;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct ProtocolVersion(pub u8);
impl ProtocolVersion {
pub const V1: Self = Self(1);
pub const V3: Self = Self(3);
pub const fn is_supported(self) -> bool {
self.0 == PROTOCOL_VERSION || self.0 == PROTOCOL_VERSION_PROTECTED
}
pub fn check_supported(self) -> Result<(), Error> {
if self.is_supported() {
Ok(())
} else {
Err(Error::UnsupportedVersion(self.0))
}
}
}
impl Default for ProtocolVersion {
fn default() -> Self {
Self::V1
}
}
impl fmt::Display for ProtocolVersion {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "DSK/{}", self.0)
}
}