moq_lite/lite/
announce.rs

1use num_enum::{IntoPrimitive, TryFromPrimitive};
2
3use crate::{
4	coding::*,
5	lite::{Message, Version},
6	Path,
7};
8
9/// Sent by the publisher to announce the availability of a track.
10/// The payload contains the contents of the wildcard.
11#[derive(Clone, Debug, PartialEq, Eq)]
12#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
13pub enum Announce<'a> {
14	Active {
15		#[cfg_attr(feature = "serde", serde(borrow))]
16		suffix: Path<'a>,
17	},
18	Ended {
19		#[cfg_attr(feature = "serde", serde(borrow))]
20		suffix: Path<'a>,
21	},
22}
23
24impl<'a> Message for Announce<'a> {
25	fn decode_msg<R: bytes::Buf>(r: &mut R, version: Version) -> Result<Self, DecodeError> {
26		Ok(match AnnounceStatus::decode(r, version)? {
27			AnnounceStatus::Active => Self::Active {
28				suffix: Path::decode(r, version)?,
29			},
30			AnnounceStatus::Ended => Self::Ended {
31				suffix: Path::decode(r, version)?,
32			},
33		})
34	}
35
36	fn encode_msg<W: bytes::BufMut>(&self, w: &mut W, version: Version) {
37		match self {
38			Self::Active { suffix } => {
39				AnnounceStatus::Active.encode(w, version);
40				suffix.encode(w, version);
41			}
42			Self::Ended { suffix } => {
43				AnnounceStatus::Ended.encode(w, version);
44				suffix.encode(w, version);
45			}
46		}
47	}
48}
49
50/// Sent by the subscriber to request ANNOUNCE messages.
51#[derive(Clone, Debug)]
52pub struct AnnouncePlease<'a> {
53	// Request tracks with this prefix.
54	pub prefix: Path<'a>,
55}
56
57impl<'a> Message for AnnouncePlease<'a> {
58	fn decode_msg<R: bytes::Buf>(r: &mut R, version: Version) -> Result<Self, DecodeError> {
59		let prefix = Path::decode(r, version)?;
60		Ok(Self { prefix })
61	}
62
63	fn encode_msg<W: bytes::BufMut>(&self, w: &mut W, version: Version) {
64		self.prefix.encode(w, version)
65	}
66}
67
68/// Send by the publisher, used to determine the message that follows.
69#[derive(Clone, Copy, Debug, IntoPrimitive, TryFromPrimitive)]
70#[repr(u8)]
71enum AnnounceStatus {
72	Ended = 0,
73	Active = 1,
74}
75
76impl<V> Decode<V> for AnnounceStatus {
77	fn decode<R: bytes::Buf>(r: &mut R, version: V) -> Result<Self, DecodeError> {
78		let status = u8::decode(r, version)?;
79		status.try_into().map_err(|_| DecodeError::InvalidValue)
80	}
81}
82
83impl<V> Encode<V> for AnnounceStatus {
84	fn encode<W: bytes::BufMut>(&self, w: &mut W, version: V) {
85		(*self as u8).encode(w, version)
86	}
87}
88
89/// Sent after setup to communicate the initially announced paths.
90#[derive(Clone, Debug, PartialEq, Eq)]
91#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
92pub struct AnnounceInit<'a> {
93	/// List of currently active broadcasts, encoded as suffixes to be combined with the prefix.
94	#[cfg_attr(feature = "serde", serde(borrow))]
95	pub suffixes: Vec<Path<'a>>,
96}
97
98impl<'a> Message for AnnounceInit<'a> {
99	fn decode_msg<R: bytes::Buf>(r: &mut R, version: Version) -> Result<Self, DecodeError> {
100		let count = u64::decode(r, version)?;
101
102		// Don't allocate more than 1024 elements upfront
103		let mut paths = Vec::with_capacity(count.min(1024) as usize);
104
105		for _ in 0..count {
106			paths.push(Path::decode(r, version)?);
107		}
108
109		Ok(Self { suffixes: paths })
110	}
111
112	fn encode_msg<W: bytes::BufMut>(&self, w: &mut W, version: Version) {
113		(self.suffixes.len() as u64).encode(w, version);
114		for path in &self.suffixes {
115			path.encode(w, version);
116		}
117	}
118}