sdmmc_core/command/class/class2/cmd23/
arg.rs

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