sheave_core/messages/headers/basic/
message_format.rs

1/// The first 2 bits to indicate a format of message header.
2///
3/// Variants correspond to respectively formats:
4///
5/// |Pattern|Format(length)|
6/// | :- | -: |
7/// |`New`|11 bytes|
8/// |`SameSource`|7 bytes|
9/// |`TimerChange`|3 bytes|
10/// |`Continue`|0 bytes|
11#[repr(u8)]
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum MessageFormat {
14    New,
15    SameSource,
16    TimerChange,
17    Continue
18}
19
20impl From<u8> for MessageFormat {
21    /// Converts message format bits into a variant.
22    ///
23    /// # Panics
24    ///
25    /// Because of the RTMP specification, this is implemented in such a way as to emit a panic when is passed any value above 3.
26    ///
27    /// # Examples
28    ///
29    /// ```rust
30    /// use std::panic::catch_unwind;
31    /// use sheave_core::messages::headers::{
32    ///     MessageFormat,
33    ///     MessageFormat::*
34    /// };
35    ///
36    /// assert_eq!(New, MessageFormat::from(0)); // => ok
37    /// assert_eq!(SameSource, MessageFormat::from(1)); // => ok
38    /// assert_eq!(TimerChange, MessageFormat::from(2)); // => ok
39    /// assert_eq!(Continue, MessageFormat::from(3)); // => ok
40    /// assert!(catch_unwind(|| MessageFormat::from(4)).is_err()); // => this will be backtrace.
41    /// ```
42    fn from(message_format: u8) -> Self {
43        use MessageFormat::*;
44
45        match message_format {
46            0 => New,
47            1 => SameSource,
48            2 => TimerChange,
49            3 => Continue,
50            _ => unreachable!("MessageFormat.")
51        }
52    }
53}
54
55impl From<MessageFormat> for u8 {
56    fn from(message_format: MessageFormat) -> Self {
57        message_format as u8
58    }
59}