arch_pkg_text/value/
skip_or_array.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
/// Parse result of `SkipOrHex*::u8_array` methods.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SkipOrArray<const LEN: usize> {
    /// The source string was `"SKIP"`.
    Skip,
    /// The source string was a valid hexadecimal string.
    Array([u8; LEN]),
}

impl<const LEN: usize> SkipOrArray<LEN> {
    /// Returns `true` if the value is [`Skip`](SkipOrArray::Skip).
    #[must_use]
    pub fn is_skip(&self) -> bool {
        matches!(self, Self::Skip)
    }

    /// Returns `true` if the value is [`Array`](SkipOrArray::Array).
    #[must_use]
    pub fn is_array(&self) -> bool {
        matches!(self, Self::Array(_))
    }

    /// Try extracting an array of `u8`.
    pub fn try_into_array(self) -> Option<[u8; LEN]> {
        match self {
            SkipOrArray::Array(array) => Some(array),
            SkipOrArray::Skip => None,
        }
    }

    /// Try getting a reference to the underlying array of `u8`.
    pub fn as_array(&self) -> Option<&[u8; LEN]> {
        match self {
            SkipOrArray::Array(array) => Some(array),
            SkipOrArray::Skip => None,
        }
    }

    /// Try getting a slice of `[u8]`.
    pub fn as_slice(&self) -> Option<&[u8]> {
        self.as_array().map(|x| x.as_slice())
    }
}

impl<const LEN: usize> TryFrom<SkipOrArray<LEN>> for [u8; LEN] {
    type Error = ();
    fn try_from(value: SkipOrArray<LEN>) -> Result<Self, Self::Error> {
        value.try_into_array().ok_or(())
    }
}