use core::fmt;
use ts_keys::NodePublicKey;
use zerocopy::{FromBytes, Immutable, IntoBytes, KnownLayout, Unaligned};
use crate::frame;
#[derive(
Debug, Copy, Clone, PartialEq, KnownLayout, Immutable, IntoBytes, FromBytes, Unaligned,
)]
#[repr(C, packed)]
pub struct PeerGone {
pub key: NodePublicKey,
pub raw_reason: u8,
}
impl PeerGone {
pub fn reason(&self) -> Result<PeerGoneReason, frame::Error> {
self.raw_reason.try_into()
}
}
impl frame::Body for PeerGone {
const FRAME_TYPE: frame::FrameType = frame::FrameType::PeerGone;
}
#[derive(Debug, Copy, Clone, KnownLayout, Immutable, IntoBytes)]
#[repr(u8)]
pub enum PeerGoneReason {
Disconnected = 0x0,
NotHere = 0x1,
}
impl fmt::Display for PeerGoneReason {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{self:?}")
}
}
impl TryFrom<u8> for PeerGoneReason {
type Error = frame::Error;
fn try_from(v: u8) -> Result<Self, Self::Error> {
Ok(match v {
0x0 => PeerGoneReason::Disconnected,
0x1 => PeerGoneReason::NotHere,
_ => return Err(frame::Error::InvalidPeerGoneReason(v)),
})
}
}
impl From<PeerGoneReason> for u8 {
fn from(v: PeerGoneReason) -> Self {
v as u8
}
}