bnr_xfs/capabilities/
secured_comm_level.rs

1use std::fmt;
2
3use crate::impl_xfs_enum;
4
5const SEC_COM_LEVEL1: u32 = 0;
6const SEC_COM_LEVEL2: u32 = 1;
7const SEC_COM_ERROR: u32 = 2;
8
9/// Values for indication of the security level in communication between Host and BNR.
10#[repr(u32)]
11#[derive(Clone, Copy, Debug, Default, PartialEq, serde::Deserialize, serde::Serialize)]
12pub enum SecuredCommLevel {
13    /// Standard communication level.
14    #[default]
15    Level1 = SEC_COM_LEVEL1,
16    /// Secured communication level.
17    Level2 = SEC_COM_LEVEL2,
18    /// Error occurred.
19    Error = SEC_COM_ERROR,
20}
21
22impl SecuredCommLevel {
23    /// Creates a new [SecuredCommLevel].
24    pub const fn new() -> Self {
25        Self::Level1
26    }
27
28    /// Creates a new [SecuredCommLevel] from the provided parameter.
29    pub const fn create(val: u32) -> Self {
30        match val {
31            SEC_COM_LEVEL1 => Self::Level1,
32            SEC_COM_LEVEL2 => Self::Level2,
33            SEC_COM_ERROR => Self::Error,
34            _ => Self::Level1,
35        }
36    }
37}
38
39impl From<&SecuredCommLevel> for &'static str {
40    fn from(val: &SecuredCommLevel) -> Self {
41        match val {
42            SecuredCommLevel::Level1 => "level1",
43            SecuredCommLevel::Level2 => "level2",
44            SecuredCommLevel::Error => "error",
45        }
46    }
47}
48
49impl From<SecuredCommLevel> for &'static str {
50    fn from(val: SecuredCommLevel) -> Self {
51        (&val).into()
52    }
53}
54
55impl fmt::Display for SecuredCommLevel {
56    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57        write!(f, r#""{}""#, <&str>::from(self))
58    }
59}
60
61impl_xfs_enum!(SecuredCommLevel, "securedCommLevel");