Skip to main content

moqtap_codec/draft07/
types.rs

1/// SETUP ROLE parameter values (draft-07, key 0x00).
2#[derive(Debug, Clone, Copy, PartialEq, Eq)]
3#[repr(u8)]
4pub enum Role {
5    /// Publisher only.
6    Publisher = 1,
7    /// Subscriber only.
8    Subscriber = 2,
9    /// Both publisher and subscriber.
10    PubSub = 3,
11}
12
13impl Role {
14    /// Convert a raw byte to a `Role`, if valid.
15    pub fn from_u8(v: u8) -> Option<Self> {
16        match v {
17            1 => Some(Role::Publisher),
18            2 => Some(Role::Subscriber),
19            3 => Some(Role::PubSub),
20            _ => None,
21        }
22    }
23}
24
25/// Object status values, from MoQ Transport draft-07 Section 7.1.1.1
26/// "Object Status".
27///
28/// The draft assigns 0x0, 0x1, 0x3, 0x4 and 0x5, and says of everything else
29/// that it "SHOULD be treated as a protocol error and terminate the session
30/// with a Protocol Violation". 0x2 is not assigned; [`ObjectStatus::from_u64`]
31/// answers `None` for it, and for every other unassigned value.
32#[derive(Debug, Clone, Copy, PartialEq, Eq)]
33#[repr(u8)]
34pub enum ObjectStatus {
35    /// Normal object. The payload is an array of bytes and can be empty.
36    Normal = 0,
37    /// The object does not exist at any publisher and will not be published in
38    /// the future.
39    ObjectDoesNotExist = 1,
40    /// End of Group. Object ID is one greater than the largest object produced
41    /// in the group identified by the Group ID; 0 means the group is empty.
42    EndOfGroup = 3,
43    /// End of Track and Group, which is the name draft-07 gives 0x4: Group ID
44    /// is one greater than the largest group produced in the track and Object
45    /// ID is one greater than the largest object produced in that group.
46    ///
47    /// Named as the draft names it, and as drafts 08-10 name the same code
48    /// point. Draft-08 keeps 0x4 here and adds a separate "end of Track" at
49    /// 0x5, so a variant called `EndOfTrack` at 0x4 would read as that later
50    /// status one code point off.
51    EndOfTrackAndGroup = 4,
52    /// End of Subgroup. Object ID is one greater than the largest normal
53    /// object ID in the subgroup.
54    EndOfSubgroup = 5,
55}
56
57impl ObjectStatus {
58    /// Every status draft-07 assigns, in ascending wire order.
59    ///
60    /// This is exactly the set [`ObjectStatus::from_u64`] accepts. Any other
61    /// value is one the draft does not assign.
62    pub const ALL: &[ObjectStatus] = &[
63        ObjectStatus::Normal,
64        ObjectStatus::ObjectDoesNotExist,
65        ObjectStatus::EndOfGroup,
66        ObjectStatus::EndOfTrackAndGroup,
67        ObjectStatus::EndOfSubgroup,
68    ];
69
70    /// Convert a raw u64 to an `ObjectStatus`, or `None` if draft-07 does not
71    /// assign that value.
72    pub fn from_u64(v: u64) -> Option<Self> {
73        match v {
74            0 => Some(ObjectStatus::Normal),
75            1 => Some(ObjectStatus::ObjectDoesNotExist),
76            3 => Some(ObjectStatus::EndOfGroup),
77            4 => Some(ObjectStatus::EndOfTrackAndGroup),
78            5 => Some(ObjectStatus::EndOfSubgroup),
79            _ => None,
80        }
81    }
82
83    /// Return the wire value.
84    pub fn as_u64(self) -> u64 {
85        self as u64
86    }
87}