use std::io::{Read, Write};
use mcproto_codec::error::{CodecError, CodecKind};
use crate::TypeCodec;
use super::{DebugSubscriptionData, DebugSubscriptionType};
#[derive(Debug, Clone, PartialEq)]
pub struct DebugSubscriptionEvent {
pub data: DebugSubscriptionData,
}
impl DebugSubscriptionEvent {
#[must_use]
pub const fn new(data: DebugSubscriptionData) -> Self {
Self { data }
}
#[must_use]
pub const fn subscription_type(&self) -> DebugSubscriptionType {
self.data.subscription_type()
}
}
impl From<DebugSubscriptionData> for DebugSubscriptionEvent {
fn from(data: DebugSubscriptionData) -> Self {
Self::new(data)
}
}
impl TypeCodec for DebugSubscriptionEvent {
fn encode(&self, writer: &mut impl Write) -> Result<(), CodecError> {
self.subscription_type()
.encode(writer)
.map_err(|error| error.with_context(CodecKind::DebugSubscriptionEvent))?;
self.data
.encode_payload(writer)
.map_err(|error| error.with_context(CodecKind::DebugSubscriptionEvent))
}
fn decode(reader: &mut impl Read) -> Result<Self, CodecError> {
let subscription_type = DebugSubscriptionType::decode(reader)
.map_err(|error| error.with_context(CodecKind::DebugSubscriptionEvent))?;
DebugSubscriptionData::decode_payload(subscription_type, reader)
.map(Self::new)
.map_err(|error| error.with_context(CodecKind::DebugSubscriptionEvent))
}
}