sdmmc-core 0.5.0

SD/MMC core data structures and algorithms
Documentation
use crate::lib_bitfield;
use crate::result::{Error, Result};

mod rw;

pub use rw::*;

lib_bitfield! {
    /// Argumentument for CMD56.
    pub Argument(u32): u16 {
        raw_rw: 0;
    }
}

impl Argument {
    /// Represents the byte length of the [Argument].
    pub const LEN: usize = 4;
    /// Represents the raw default value of the [Argument].
    pub const DEFAULT: u32 = 0;

    /// Creates a new [Argument].
    pub const fn new() -> Self {
        Self(Self::DEFAULT)
    }

    /// Gets the `rd/wr` field for the [Argument].
    pub const fn rw(&self) -> ReadWrite {
        ReadWrite::from_bool(self.raw_rw())
    }

    /// Sets the `rd/wr` field for the [Argument].
    pub fn set_rw(&mut self, val: ReadWrite) {
        self.set_raw_rw(val.into_bool())
    }

    /// Converts a [`u32`] into an [Argument].
    pub const fn from_bits(val: u32) -> Self {
        Self(val)
    }

    /// Attempts to convert a [`u32`] into an [Argument].
    pub const fn try_from_bits(val: u32) -> Result<Self> {
        Ok(Self(val))
    }

    /// Converts the [Argument] into a byte array.
    pub const fn bytes(&self) -> [u8; Self::LEN] {
        self.0.to_be_bytes()
    }

    /// Attempts to convert a byte slice into an [Argument].
    pub const fn try_from_bytes(val: &[u8]) -> Result<Self> {
        match val.len() {
            len if len < Self::LEN => Err(Error::invalid_length(len, Self::LEN)),
            _ => Self::try_from_bits(u32::from_be_bytes([val[0], val[1], val[2], val[3]])),
        }
    }
}

impl Default for Argument {
    fn default() -> Self {
        Self::new()
    }
}

impl From<u32> for Argument {
    fn from(val: u32) -> Self {
        Self::from_bits(val)
    }
}

impl From<Argument> for u32 {
    fn from(val: Argument) -> Self {
        val.bits()
    }
}

impl From<Argument> for [u8; Argument::LEN] {
    fn from(val: Argument) -> Self {
        val.bytes()
    }
}

impl TryFrom<&[u8]> for Argument {
    type Error = Error;

    fn try_from(val: &[u8]) -> Result<Self> {
        Self::try_from_bytes(val)
    }
}

impl<const N: usize> TryFrom<&[u8; N]> for Argument {
    type Error = Error;

    fn try_from(val: &[u8; N]) -> Result<Self> {
        Self::try_from_bytes(val.as_ref())
    }
}

impl<const N: usize> TryFrom<[u8; N]> for Argument {
    type Error = Error;

    fn try_from(val: [u8; N]) -> Result<Self> {
        Self::try_from_bytes(val.as_ref())
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_fields() {
        let new_rw = ReadWrite::new();

        (1..=u32::BITS)
            .map(|r| ((1u64 << r) - 1) as u32)
            .for_each(|raw| {
                let exp_rw = ReadWrite::from_bool((raw & 0x1) != 0);
                let raw_bytes = raw.to_be_bytes();

                let mut exp_arg = Argument(raw);

                assert_eq!(Argument::try_from_bits(raw), Ok(exp_arg));
                assert_eq!(Argument::from_bits(raw), exp_arg);
                assert_eq!(Argument::from(raw), exp_arg);

                assert_eq!(Argument::try_from_bytes(&raw_bytes), Ok(exp_arg));
                assert_eq!(Argument::try_from(raw_bytes), Ok(exp_arg));
                assert_eq!(Argument::try_from(&raw_bytes), Ok(exp_arg));

                assert_eq!(exp_arg.bytes(), raw_bytes);
                assert_eq!(<[u8; Argument::LEN]>::from(exp_arg), raw_bytes);

                assert_eq!(exp_arg.rw(), exp_rw);

                exp_arg.set_rw(new_rw);
                assert_eq!(exp_arg.rw(), new_rw);

                exp_arg.set_rw(exp_rw);
                assert_eq!(exp_arg.rw(), exp_rw);
            });
    }
}