moq_lite/message/
announce.rs

1use num_enum::{IntoPrimitive, TryFromPrimitive};
2
3use crate::coding::*;
4
5/// Sent by the publisher to announce the availability of a track.
6/// The payload contains the contents of the wildcard.
7#[derive(Clone, Debug, PartialEq, Eq)]
8#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
9pub enum Announce {
10	Active { suffix: String },
11	Ended { suffix: String },
12}
13
14impl Announce {
15	pub fn suffix(&self) -> &str {
16		match self {
17			Announce::Active { suffix } => suffix,
18			Announce::Ended { suffix } => suffix,
19		}
20	}
21}
22
23impl Decode 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: String::decode(r)?,
28			},
29			AnnounceStatus::Ended => Self::Ended {
30				suffix: String::decode(r)?,
31			},
32		})
33	}
34}
35
36impl Encode for Announce {
37	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
38		match self {
39			Self::Active { suffix } => {
40				AnnounceStatus::Active.encode(w);
41				suffix.encode(w);
42			}
43			Self::Ended { suffix } => {
44				AnnounceStatus::Ended.encode(w);
45				suffix.encode(w);
46			}
47		}
48	}
49}
50
51/// Sent by the subscriber to request ANNOUNCE messages.
52#[derive(Clone, Debug)]
53pub struct AnnounceRequest {
54	// Request tracks with this prefix.
55	pub prefix: String,
56}
57
58impl Decode for AnnounceRequest {
59	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
60		let prefix = String::decode(r)?;
61		Ok(Self { prefix })
62	}
63}
64
65impl Encode for AnnounceRequest {
66	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
67		self.prefix.encode(w)
68	}
69}
70
71/// Send by the publisher, used to determine the message that follows.
72#[derive(Clone, Copy, Debug, IntoPrimitive, TryFromPrimitive)]
73#[repr(u8)]
74enum AnnounceStatus {
75	Ended = 0,
76	Active = 1,
77}
78
79impl Decode for AnnounceStatus {
80	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
81		let status = u8::decode(r)?;
82		match status {
83			0 => Ok(Self::Ended),
84			1 => Ok(Self::Active),
85			_ => Err(DecodeError::InvalidValue),
86		}
87	}
88}
89
90impl Encode for AnnounceStatus {
91	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
92		(*self as u8).encode(w)
93	}
94}