svd_rs/
usage.rs

1/// Allows specifying two different enumerated values
2/// depending whether it is to be used for a read or a write access.
3#[cfg_attr(
4    feature = "serde",
5    derive(serde::Deserialize, serde::Serialize),
6    serde(rename_all = "kebab-case")
7)]
8#[derive(Clone, Copy, Debug, Eq, PartialEq)]
9pub enum Usage {
10    /// Read
11    Read,
12    /// Write
13    Write,
14    /// Read & Write
15    ReadWrite,
16}
17
18impl Default for Usage {
19    fn default() -> Self {
20        Self::ReadWrite
21    }
22}
23
24impl Usage {
25    /// Parse a string into an [`Usage`] value, returning [`Option::None`] if the string is not valid.
26    pub fn parse_str(s: &str) -> Option<Self> {
27        match s {
28            "read" => Some(Self::Read),
29            "write" => Some(Self::Write),
30            "read-write" => Some(Self::ReadWrite),
31            _ => None,
32        }
33    }
34
35    /// Convert this [`Usage`] into a static string.
36    pub const fn as_str(self) -> &'static str {
37        match self {
38            Self::Read => "read",
39            Self::Write => "write",
40            Self::ReadWrite => "read-write",
41        }
42    }
43}