Skip to main content

moqtap_codec/draft18/
types.rs

1//! Draft-18 object status values (unchanged from draft-17).
2//!
3//! - 0x0 = Normal
4//! - 0x3 = End of Group
5//! - 0x4 = End of Track
6
7/// Object status values, from MoQ Transport draft-18 Section 11.2.1.1
8/// "Object Status".
9///
10/// The draft assigns 0x0, 0x3 and 0x4. Of every other value the section says:
11/// "Any other value SHOULD be treated as a protocol error and the session
12/// SHOULD be closed with a PROTOCOL_VIOLATION". [`ObjectStatus::from_u64`]
13/// answers `None` for everything the draft leaves unassigned, 0x1 and 0x2
14/// included.
15#[derive(Debug, Clone, Copy, PartialEq, Eq)]
16#[repr(u8)]
17pub enum ObjectStatus {
18    /// Normal object. Implicit for any non-zero length object; zero-length
19    /// objects encode it explicitly.
20    Normal = 0x0,
21    /// End of Group. No object with the given Group ID and an Object ID greater
22    /// than or equal to the one specified exists in that group.
23    EndOfGroup = 0x3,
24    /// End of Track. No object at a location equal to or greater than the one
25    /// specified exists.
26    EndOfTrack = 0x4,
27}
28
29impl ObjectStatus {
30    /// Every status draft-18 assigns, in ascending wire order.
31    ///
32    /// This is exactly the set [`ObjectStatus::from_u64`] accepts. Any other
33    /// value is one the draft does not assign.
34    pub const ALL: &[ObjectStatus] =
35        &[ObjectStatus::Normal, ObjectStatus::EndOfGroup, ObjectStatus::EndOfTrack];
36
37    /// Convert a raw u64 to an `ObjectStatus`, or `None` if draft-18 does not
38    /// assign that value.
39    pub fn from_u64(v: u64) -> Option<Self> {
40        match v {
41            0x0 => Some(ObjectStatus::Normal),
42            0x3 => Some(ObjectStatus::EndOfGroup),
43            0x4 => Some(ObjectStatus::EndOfTrack),
44            _ => None,
45        }
46    }
47
48    /// Return the wire value.
49    pub fn as_u64(self) -> u64 {
50        self as u64
51    }
52
53    /// Return the wire value as a single byte.
54    ///
55    /// A draft-18 status datagram carries its status as one bare byte rather
56    /// than a varint, so the datagram encoder needs the code in that width;
57    /// every assigned code is well under 0xff, so this is the same number
58    /// [`ObjectStatus::as_u64`] returns.
59    pub fn as_u8(self) -> u8 {
60        self as u8
61    }
62}