moq_transfork/message/
announce.rs

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