moq_transport/data/
object.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
use crate::coding::{Decode, DecodeError, Encode, EncodeError};

#[derive(Clone, Copy, Debug, PartialEq)]
pub enum ObjectStatus {
	Object = 0x0,
	ObjectDoesNotExist = 0x1,
	GroupDoesNotExist = 0x2,
	EndOfGroup = 0x3,
	EndOfTrack = 0x4,
}

impl Decode for ObjectStatus {
	fn decode<B: bytes::Buf>(r: &mut B) -> Result<Self, DecodeError> {
		match u64::decode(r)? {
			0x0 => Ok(Self::Object),
			0x1 => Ok(Self::ObjectDoesNotExist),
			0x2 => Ok(Self::GroupDoesNotExist),
			0x3 => Ok(Self::EndOfGroup),
			0x4 => Ok(Self::EndOfTrack),
			_ => Err(DecodeError::InvalidObjectStatus),
		}
	}
}

impl Encode for ObjectStatus {
	fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
		match self {
			Self::Object => (0x0_u64).encode(w),
			Self::ObjectDoesNotExist => (0x1_u64).encode(w),
			Self::GroupDoesNotExist => (0x2_u64).encode(w),
			Self::EndOfGroup => (0x3_u64).encode(w),
			Self::EndOfTrack => (0x4_u64).encode(w),
		}
	}
}

#[derive(Clone, Debug)]
pub struct ObjectHeader {
	// The subscribe ID.
	pub subscribe_id: u64,

	// The track alias.
	pub track_alias: u64,

	// The sequence number within the track.
	pub group_id: u64,

	// The sequence number within the group.
	pub object_id: u64,

	// Publisher priority, where **smaller** values are sent first.
	pub publisher_priority: u8,

	// The object status
	pub object_status: ObjectStatus,
}

impl Decode for ObjectHeader {
	fn decode<R: bytes::Buf>(r: &mut R) -> Result<Self, DecodeError> {
		Ok(Self {
			subscribe_id: u64::decode(r)?,
			track_alias: u64::decode(r)?,
			group_id: u64::decode(r)?,
			object_id: u64::decode(r)?,
			publisher_priority: u8::decode(r)?,
			object_status: ObjectStatus::decode(r)?,
		})
	}
}

impl Encode for ObjectHeader {
	fn encode<W: bytes::BufMut>(&self, w: &mut W) -> Result<(), EncodeError> {
		self.subscribe_id.encode(w)?;
		self.track_alias.encode(w)?;
		self.group_id.encode(w)?;
		self.object_id.encode(w)?;
		self.publisher_priority.encode(w)?;
		self.object_status.encode(w)?;

		Ok(())
	}
}