moq_lite/message/
announce.rs1use num_enum::{IntoPrimitive, TryFromPrimitive};
2
3use crate::{coding::*, Path};
4
5#[derive(Clone, Debug, PartialEq, Eq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum Announce {
10 Active { suffix: Path },
11 Ended { suffix: Path },
12}
13
14impl Announce {
15 pub fn suffix(&self) -> &Path {
16 match self {
17 Announce::Active { suffix } => suffix,
18 Announce::Ended { suffix } => suffix,
19 }
20 }
21}
22
23impl Message for Announce {
24 fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
25 Ok(match AnnounceStatus::decode(r)? {
26 AnnounceStatus::Active => Self::Active {
27 suffix: Path::decode(r)?,
28 },
29 AnnounceStatus::Ended => Self::Ended {
30 suffix: Path::decode(r)?,
31 },
32 })
33 }
34
35 fn encode<W: bytes::BufMut>(&self, w: &mut W) {
36 match self {
37 Self::Active { suffix } => {
38 AnnounceStatus::Active.encode(w);
39 suffix.encode(w);
40 }
41 Self::Ended { suffix } => {
42 AnnounceStatus::Ended.encode(w);
43 suffix.encode(w);
44 }
45 }
46 }
47}
48
49#[derive(Clone, Debug)]
51pub struct AnnouncePlease {
52 pub prefix: Path,
54}
55
56impl Message for AnnouncePlease {
57 fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
58 let prefix = Path::decode(r)?;
59 Ok(Self { prefix })
60 }
61
62 fn encode<W: bytes::BufMut>(&self, w: &mut W) {
63 self.prefix.encode(w)
64 }
65}
66
67#[derive(Clone, Copy, Debug, IntoPrimitive, TryFromPrimitive)]
69#[repr(u8)]
70enum AnnounceStatus {
71 Ended = 0,
72 Active = 1,
73}
74
75impl Decode for AnnounceStatus {
76 fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
77 let status = u8::decode(r)?;
78 match status {
79 0 => Ok(Self::Ended),
80 1 => Ok(Self::Active),
81 _ => Err(DecodeError::InvalidValue),
82 }
83 }
84}
85
86impl Encode for AnnounceStatus {
87 fn encode<W: bytes::BufMut>(&self, w: &mut W) {
88 (*self as u8).encode(w)
89 }
90}
91
92#[derive(Clone, Debug, PartialEq, Eq)]
94#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
95pub struct AnnounceInit {
96 pub suffixes: Vec<Path>,
98}
99
100impl Message for AnnounceInit {
101 fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
102 let count = u64::decode(r)?;
103
104 let mut paths = Vec::with_capacity(count.min(1024) as usize);
106
107 for _ in 0..count {
108 paths.push(Path::decode(r)?);
109 }
110
111 Ok(Self { suffixes: paths })
112 }
113
114 fn encode<W: bytes::BufMut>(&self, w: &mut W) {
115 (self.suffixes.len() as u64).encode(w);
116 for path in &self.suffixes {
117 path.encode(w);
118 }
119 }
120}