#![allow(deprecated)]
use crate::PacketSize;
use serde::{Deserialize, Serialize};
use std::fmt;
use thiserror::Error;
#[derive(Error, Debug)]
#[error("{received} is not a valid packet type tag")]
pub struct InvalidPacketType {
received: u8,
}
#[repr(u8)]
#[allow(deprecated)]
#[derive(Clone, Copy, Debug, PartialEq, Eq, Default, Serialize, Deserialize)]
pub enum PacketType {
#[default]
#[serde(rename = "mix")]
#[serde(alias = "sphinx")]
Mix = 0,
#[deprecated]
#[serde(rename = "outfox")]
Outfox = 2,
}
impl fmt::Display for PacketType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
PacketType::Mix => write!(f, "Mix"),
PacketType::Outfox => write!(f, "Outfox"),
}
}
}
impl PacketType {
pub fn is_mix(self) -> bool {
self == PacketType::Mix
}
pub fn is_outfox(self) -> bool {
self == PacketType::Outfox
}
}
impl TryFrom<u8> for PacketType {
type Error = InvalidPacketType;
fn try_from(value: u8) -> Result<Self, Self::Error> {
match value {
_ if value == (PacketType::Mix as u8) => Ok(Self::Mix),
_ if value == (PacketType::Outfox as u8) => Ok(Self::Outfox),
v => Err(InvalidPacketType { received: v }),
}
}
}
impl From<PacketSize> for PacketType {
fn from(s: PacketSize) -> Self {
match s {
PacketSize::RegularPacket => PacketType::Mix,
PacketSize::AckPacket => PacketType::Mix,
PacketSize::ExtendedPacket32 => PacketType::Mix,
PacketSize::ExtendedPacket8 => PacketType::Mix,
PacketSize::ExtendedPacket16 => PacketType::Mix,
PacketSize::OutfoxRegularPacket => PacketType::Outfox,
PacketSize::OutfoxAckPacket => PacketType::Outfox,
}
}
}