moq_proto/message/
announce.rs

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