moq_lite/message/
subscribe.rs

1use std::borrow::Cow;
2
3use crate::{
4	coding::{Decode, DecodeError, Encode, Message},
5	Path,
6};
7
8/// Sent by the subscriber to request all future objects for the given track.
9///
10/// Objects will use the provided ID instead of the full track name, to save bytes.
11#[derive(Clone, Debug)]
12pub struct Subscribe<'a> {
13	pub id: u64,
14	pub broadcast: Path<'a>,
15	pub track: Cow<'a, str>,
16	pub priority: u8,
17}
18
19impl<'a> Message for Subscribe<'a> {
20	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
21		let id = u64::decode(r)?;
22		let broadcast = Path::decode(r)?;
23		let track = Cow::<str>::decode(r)?;
24		let priority = u8::decode(r)?;
25
26		Ok(Self {
27			id,
28			broadcast,
29			track,
30			priority,
31		})
32	}
33
34	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
35		self.id.encode(w);
36		self.broadcast.encode(w);
37		self.track.encode(w);
38		self.priority.encode(w);
39	}
40}
41
42#[derive(Clone, Debug)]
43pub struct SubscribeOk {
44	pub priority: u8,
45}
46
47impl Message for SubscribeOk {
48	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
49		self.priority.encode(w);
50	}
51
52	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
53		let priority = u8::decode(r)?;
54		Ok(Self { priority })
55	}
56}