svd_rs/
access.rs

1/// Defines access rights for fields on the device, though it may be specified at a
2/// higher level than individual fields.
3#[cfg_attr(feature = "serde", derive(serde::Deserialize, serde::Serialize))]
4#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5pub enum Access {
6    /// Read access is permitted. Write operations have an undefined effect.
7    #[cfg_attr(feature = "serde", serde(rename = "read-only"))]
8    ReadOnly,
9
10    /// Read and write accesses are permitted.
11    #[cfg_attr(feature = "serde", serde(rename = "read-write"))]
12    ReadWrite,
13
14    /// Read access is always permitted.
15    /// Only the first write after a reset will affect the content.
16    /// Following writes have an undefined effect.
17    #[cfg_attr(feature = "serde", serde(rename = "read-writeOnce"))]
18    ReadWriteOnce,
19
20    /// Read operations have undefined results.
21    /// Only the first write after a reset will affect the content.
22    #[cfg_attr(feature = "serde", serde(rename = "writeOnce"))]
23    WriteOnce,
24
25    /// Read operations have an undefined result. Write access is permitted.
26    #[cfg_attr(feature = "serde", serde(rename = "write-only"))]
27    WriteOnly,
28}
29
30impl Access {
31    /// Whether the register/field is readable at least once.
32    pub fn can_read(self) -> bool {
33        matches!(self, Self::ReadOnly | Self::ReadWrite | Self::ReadWriteOnce)
34    }
35
36    /// Whether the register/field is writable at least once.
37    pub fn can_write(self) -> bool {
38        !matches!(self, Self::ReadOnly)
39    }
40}
41
42impl Default for Access {
43    fn default() -> Self {
44        Self::ReadWrite
45    }
46}
47
48impl Access {
49    /// Parse a string into an [`Access`] value, returning [`Option::None`] if the string is not valid.
50    pub fn parse_str(s: &str) -> Option<Self> {
51        match s {
52            "read-only" => Some(Self::ReadOnly),
53            "read-write" => Some(Self::ReadWrite),
54            "read-writeOnce" => Some(Self::ReadWriteOnce),
55            "write-only" => Some(Self::WriteOnly),
56            "writeOnce" => Some(Self::WriteOnce),
57            _ => None,
58        }
59    }
60
61    /// Convert this [`Access`] into a static string.
62    pub const fn as_str(self) -> &'static str {
63        match self {
64            Self::ReadOnly => "read-only",
65            Self::ReadWrite => "read-write",
66            Self::ReadWriteOnce => "read-writeOnce",
67            Self::WriteOnly => "write-only",
68            Self::WriteOnce => "writeOnce",
69        }
70    }
71}