moq_proto/message/
subscribe.rs

1use crate::{
2	coding::{Decode, DecodeError, Encode},
3	message::GroupOrder,
4};
5
6/// Sent by the subscriber to request all future objects for the given track.
7///
8/// Objects will use the provided ID instead of the full track name, to save bytes.
9#[derive(Clone, Debug)]
10pub struct Subscribe {
11	pub id: u64,
12	pub path: String,
13	pub priority: i8,
14	pub order: GroupOrder,
15
16	// TODO remove
17	pub start: Option<u64>,
18	pub end: Option<u64>,
19}
20
21impl Decode for Subscribe {
22	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
23		let id = u64::decode(r)?;
24		let path = String::decode(r)?;
25		let priority = i8::decode(r)?;
26		let order = GroupOrder::decode(r)?;
27		let start = match u64::decode(r)? {
28			0 => None,
29			n => Some(n - 1),
30		};
31		let end = match u64::decode(r)? {
32			0 => None,
33			n => Some(n - 1),
34		};
35
36		Ok(Self {
37			id,
38			path,
39			priority,
40			order,
41			start,
42			end,
43		})
44	}
45}
46
47impl Encode for Subscribe {
48	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
49		self.id.encode(w);
50		self.path.encode(w);
51		self.priority.encode(w);
52		self.order.encode(w);
53		self.start.map(|v| v + 1).unwrap_or(0).encode(w);
54		self.end.map(|v| v + 1).unwrap_or(0).encode(w);
55	}
56}
57
58#[derive(Clone, Debug)]
59pub struct SubscribeUpdate {
60	pub priority: i8,
61	pub order: GroupOrder,
62
63	// TODO remove
64	pub start: Option<u64>,
65	pub end: Option<u64>,
66}
67
68impl Decode for SubscribeUpdate {
69	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
70		let priority = i8::decode(r)?;
71		let order = GroupOrder::decode(r)?;
72		let start = match u64::decode(r)? {
73			0 => None,
74			n => Some(n - 1),
75		};
76		let end = match u64::decode(r)? {
77			0 => None,
78			n => Some(n - 1),
79		};
80
81		Ok(Self {
82			priority,
83			order,
84			start,
85			end,
86		})
87	}
88}
89
90impl Encode for SubscribeUpdate {
91	fn encode<W: bytes::BufMut>(&self, w: &mut W) {
92		self.priority.encode(w);
93		self.order.encode(w);
94		self.start.map(|v| v + 1).unwrap_or(0).encode(w);
95		self.end.map(|v| v + 1).unwrap_or(0).encode(w);
96	}
97}