sdmmc_core/command/class/class11/cmd39/
arg.rs

1use crate::lib_bitfield;
2use crate::result::{Error, Result};
3
4lib_bitfield! {
5    /// Argument for CMD39.
6    pub Argument(u32): u8 {
7        raw_partition_id: 31, 24;
8    }
9}
10
11impl Argument {
12    /// Represents the byte length of the [Argument].
13    pub const LEN: usize = 4;
14    /// Represents the default value of the [Argument].
15    pub const DEFAULT: u32 = 0;
16
17    /// Creates a new [Argument].
18    pub const fn new() -> Self {
19        Self(Self::DEFAULT)
20    }
21
22    /// Gets the `partition ID` field of the [Argument].
23    pub const fn partition_id(&self) -> u8 {
24        self.raw_partition_id()
25    }
26
27    /// Sets the `partition ID` field of the [Argument].
28    pub fn set_partition_id(&mut self, val: u8) {
29        self.set_raw_partition_id(val as u32);
30    }
31
32    /// Converts a [`u32`] into an [`Argument`].
33    pub const fn from_bits(val: u32) -> Self {
34        Self(val)
35    }
36
37    /// Attempts to convert a [`u32`] into an [`Argument`].
38    pub const fn try_from_bits(val: u32) -> Result<Self> {
39        Ok(Self::from_bits(val))
40    }
41
42    /// Converts the [Argument] into a byte array.
43    pub const fn bytes(&self) -> [u8; Self::LEN] {
44        self.0.to_be_bytes()
45    }
46
47    /// Attempts to convert a byte slice into an [`Argument`].
48    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
49        match val.len() {
50            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
51            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
52        }
53    }
54}
55
56impl Default for Argument {
57    fn default() -> Self {
58        Self::new()
59    }
60}
61
62impl From<Argument> for u32 {
63    fn from(val: Argument) -> Self {
64        val.bits()
65    }
66}
67
68impl From<Argument> for [u8; Argument::LEN] {
69    fn from(val: Argument) -> Self {
70        val.bytes()
71    }
72}
73
74impl TryFrom<u32> for Argument {
75    type Error = Error;
76
77    fn try_from(val: u32) -> Result<Self> {
78        Self::try_from_bits(val)
79    }
80}
81
82impl TryFrom<&[u8]> for Argument {
83    type Error = Error;
84
85    fn try_from(val: &[u8]) -> Result<Self> {
86        Self::try_from_bytes(val)
87    }
88}
89
90impl<const N: usize> TryFrom<&[u8; N]> for Argument {
91    type Error = Error;
92
93    fn try_from(val: &[u8; N]) -> Result<Self> {
94        Self::try_from_bytes(val.as_ref())
95    }
96}
97
98impl<const N: usize> TryFrom<[u8; N]> for Argument {
99    type Error = Error;
100
101    fn try_from(val: [u8; N]) -> Result<Self> {
102        Self::try_from_bytes(val.as_ref())
103    }
104}
105
106#[cfg(test)]
107mod tests {
108    use super::*;
109
110    #[test]
111    fn test_fields() {
112        (1..=u32::BITS)
113            .map(|r| ((1u64 << r) - 1) as u32)
114            .for_each(|raw_arg| {
115                let raw = raw_arg.to_be_bytes();
116                let mut exp_arg = Argument(raw_arg);
117
118                let exp_partition_id = (raw_arg >> 24) as u8;
119
120                assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
121                assert_eq!(Argument::try_from_bytes(raw.as_ref()), Ok(exp_arg));
122                assert_eq!(Argument::try_from(raw_arg), Ok(exp_arg));
123                assert_eq!(Argument::try_from(raw), Ok(exp_arg));
124                assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
125
126                assert_eq!(exp_arg.bits(), raw_arg);
127                assert_eq!(exp_arg.bytes(), raw);
128
129                assert_eq!(exp_arg.partition_id(), exp_partition_id);
130
131                exp_arg.set_partition_id(0);
132                assert_eq!(exp_arg.partition_id(), 0);
133
134                exp_arg.set_partition_id(exp_partition_id);
135                assert_eq!(exp_arg.partition_id(), exp_partition_id);
136            });
137    }
138}