Skip to main content

gbp_core/
stream.rs

1//! GBP stream classes.
2
3use core::fmt;
4
5/// Stream class. Each [`StreamType`] is a separate sub-protocol with its own
6/// delivery and reliability policy (see the GBP interop profile).
7#[repr(u8)]
8#[derive(Copy, Clone, Debug, PartialEq, Eq, Hash)]
9pub enum StreamType {
10    /// Stream 0 — the GBP control plane.
11    Control = 0,
12    /// Group Audio Protocol (Opus media frames).
13    Audio = 1,
14    /// Group Text Protocol (textual messages).
15    Text = 2,
16    /// Group Signaling Protocol (membership and policy signals).
17    Signal = 3,
18}
19
20impl StreamType {
21    /// Returns the on-wire byte representation.
22    pub const fn as_u8(self) -> u8 {
23        self as u8
24    }
25}
26
27impl TryFrom<u32> for StreamType {
28    type Error = u32;
29    fn try_from(v: u32) -> Result<Self, u32> {
30        match v {
31            0 => Ok(Self::Control),
32            1 => Ok(Self::Audio),
33            2 => Ok(Self::Text),
34            3 => Ok(Self::Signal),
35            other => Err(other),
36        }
37    }
38}
39
40impl TryFrom<u8> for StreamType {
41    type Error = u8;
42    fn try_from(v: u8) -> Result<Self, u8> {
43        StreamType::try_from(v as u32).map_err(|_| v)
44    }
45}
46
47impl fmt::Display for StreamType {
48    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
49        f.write_str(match self {
50            Self::Control => "control",
51            Self::Audio => "audio",
52            Self::Text => "text",
53            Self::Signal => "signal",
54        })
55    }
56}