arch_pkg_text/value/
skip_or_array.rs

1/// Parse result of `SkipOrHex*::u8_array` methods.
2#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
3pub enum SkipOrArray<const LEN: usize> {
4    /// The source string was `"SKIP"`.
5    Skip,
6    /// The source string was a valid hexadecimal string.
7    Array([u8; LEN]),
8}
9
10impl<const LEN: usize> SkipOrArray<LEN> {
11    /// Returns `true` if the value is [`Skip`](SkipOrArray::Skip).
12    #[must_use]
13    pub fn is_skip(&self) -> bool {
14        matches!(self, Self::Skip)
15    }
16
17    /// Returns `true` if the value is [`Array`](SkipOrArray::Array).
18    #[must_use]
19    pub fn is_array(&self) -> bool {
20        matches!(self, Self::Array(_))
21    }
22
23    /// Try extracting an array of `u8`.
24    pub fn try_into_array(self) -> Option<[u8; LEN]> {
25        match self {
26            SkipOrArray::Array(array) => Some(array),
27            SkipOrArray::Skip => None,
28        }
29    }
30
31    /// Try getting a reference to the underlying array of `u8`.
32    pub fn as_array(&self) -> Option<&[u8; LEN]> {
33        match self {
34            SkipOrArray::Array(array) => Some(array),
35            SkipOrArray::Skip => None,
36        }
37    }
38
39    /// Try getting a slice of `[u8]`.
40    pub fn as_slice(&self) -> Option<&[u8]> {
41        self.as_array().map(|x| x.as_slice())
42    }
43}
44
45impl<const LEN: usize> TryFrom<SkipOrArray<LEN>> for [u8; LEN] {
46    type Error = ();
47    fn try_from(value: SkipOrArray<LEN>) -> Result<Self, Self::Error> {
48        value.try_into_array().ok_or(())
49    }
50}