moq_lite/lite/
subscribe.rs

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