sdmmc_core/response/
end.rsuse crate::lib_bitfield;
use crate::result::{Error, Result};
use crate::Crc7;
lib_bitfield! {
pub End(u8): u8 {
raw_crc: 7, 1;
pub end_bit: 0;
}
}
impl End {
pub const DEFAULT: u8 = 1;
const CRC_SHIFT: usize = 1;
pub const fn new() -> Self {
Self(Self::DEFAULT)
}
pub const fn crc(&self) -> Crc7 {
Crc7::from_bits(self.raw_crc())
}
pub fn set_crc(&mut self, val: Crc7) {
self.set_raw_crc(val.bits());
}
pub const fn from_crc(crc: Crc7) -> Self {
Self((crc.bits() << Self::CRC_SHIFT) | Self::DEFAULT)
}
pub const fn from_bits(val: u8) -> Self {
Self(val | Self::DEFAULT)
}
pub const fn try_from_bits(val: u8) -> Result<Self> {
match Self(val) {
e if !e.end_bit() => Err(Error::invalid_field_variant("end", val as usize)),
e => Ok(e),
}
}
}
impl Default for End {
fn default() -> Self {
Self::new()
}
}
impl TryFrom<u8> for End {
type Error = Error;
fn try_from(val: u8) -> Result<Self> {
Self::try_from_bits(val)
}
}
impl From<End> for u8 {
fn from(val: End) -> Self {
val.bits()
}
}
impl From<Crc7> for End {
fn from(val: Crc7) -> Self {
Self::from_crc(val)
}
}
impl From<End> for Crc7 {
fn from(val: End) -> Self {
val.crc()
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_fields() {
let mut s = End::new();
assert_eq!(s.crc(), Crc7::new());
assert!(s.end_bit());
(0..=0xff)
.map(|crc| Crc7::from_bits(crc & 0x7f))
.for_each(|crc| {
let raw_end = (crc.bits() << End::CRC_SHIFT) | End::DEFAULT;
let exp_end = End(raw_end);
assert_eq!(End::try_from_bits(raw_end), Ok(exp_end));
s.set_crc(crc);
assert_eq!(s.crc(), crc);
assert!(s.end_bit());
assert_eq!(exp_end.bits(), raw_end);
});
}
}