mcproto_types/debug/
update.rs1use std::io::{Read, Write};
4
5use mcproto_codec::error::{CodecError, CodecKind};
6
7use crate::{Boolean, TypeCodec};
8
9use super::{DebugSubscriptionData, DebugSubscriptionType};
10
11#[derive(Debug, Clone, PartialEq)]
44pub enum DebugSubscriptionUpdate {
45 Absent(DebugSubscriptionType),
47 Present(DebugSubscriptionData),
49}
50
51impl DebugSubscriptionUpdate {
52 #[must_use]
54 pub const fn absent(subscription_type: DebugSubscriptionType) -> Self {
55 Self::Absent(subscription_type)
56 }
57
58 #[must_use]
60 pub const fn present(data: DebugSubscriptionData) -> Self {
61 Self::Present(data)
62 }
63
64 #[must_use]
66 pub const fn subscription_type(&self) -> DebugSubscriptionType {
67 match self {
68 Self::Absent(subscription_type) => *subscription_type,
69 Self::Present(data) => data.subscription_type(),
70 }
71 }
72
73 #[must_use]
75 pub const fn data(&self) -> Option<&DebugSubscriptionData> {
76 match self {
77 Self::Absent(_) => None,
78 Self::Present(data) => Some(data),
79 }
80 }
81
82 #[must_use]
84 pub const fn is_present(&self) -> bool {
85 matches!(self, Self::Present(_))
86 }
87}
88
89impl TypeCodec for DebugSubscriptionUpdate {
90 fn encode(&self, writer: &mut impl Write) -> Result<(), CodecError> {
91 self.subscription_type()
92 .encode(writer)
93 .map_err(|error| error.with_context(CodecKind::DebugSubscriptionUpdate))?;
94
95 let data = self.data();
96 Boolean(data.is_some())
97 .encode(writer)
98 .map_err(|error| error.with_context(CodecKind::DebugSubscriptionUpdate))?;
99 if let Some(data) = data {
100 data.encode_payload(writer)
101 .map_err(|error| error.with_context(CodecKind::DebugSubscriptionUpdate))?;
102 }
103 Ok(())
104 }
105
106 fn decode(reader: &mut impl Read) -> Result<Self, CodecError> {
107 let subscription_type = DebugSubscriptionType::decode(reader)
108 .map_err(|error| error.with_context(CodecKind::DebugSubscriptionUpdate))?;
109 let present = Boolean::decode(reader)
110 .map_err(|error| error.with_context(CodecKind::DebugSubscriptionUpdate))?;
111 if present.0 {
112 DebugSubscriptionData::decode_payload(subscription_type, reader)
113 .map(Self::Present)
114 .map_err(|error| error.with_context(CodecKind::DebugSubscriptionUpdate))
115 } else {
116 Ok(Self::Absent(subscription_type))
117 }
118 }
119}