use crate::response::spi::r1::R1;
use crate::result::{Error, Result};
use crate::{lib_bitfield, response};
lib_bitfield! {
pub R2(u16): u8 {
raw_r1: 15, 8;
pub out_of_range: 7;
pub erase_param: 6;
pub wp_violation: 5;
pub card_ecc_failed: 4;
pub cc_error: 3;
pub error: 2;
pub lock_failed: 1;
pub card_locked: 0;
}
}
response! {
R2 {
response_mode: Spi,
}
}
impl R2 {
pub const LEN: usize = 2;
pub const DEFAULT: u16 = 0;
pub const fn new() -> Self {
Self(Self::DEFAULT)
}
pub const fn r1(&self) -> Result<R1> {
R1::try_from_bits(self.raw_r1())
}
pub fn set_r1(&mut self, r1: R1) {
self.set_raw_r1(r1.bits() as u16);
}
pub const fn with_r1(mut self, r1: R1) -> Self {
Self(((r1.bits() as u16) << 8) | (self.bits() & 0xff))
}
pub const fn try_from_bits(val: u16) -> Result<Self> {
match Self(val) {
r if r.r1().is_ok() => Ok(r),
_ => Err(Error::invalid_field_variant("r2", val as usize)),
}
}
pub const fn bytes(&self) -> [u8; Self::LEN] {
self.0.to_be_bytes()
}
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)),
_ => R2::try_from_bits(u16::from_be_bytes([val[0], val[1]])),
}
}
}
impl Default for R2 {
fn default() -> Self {
Self::new()
}
}
impl TryFrom<u16> for R2 {
type Error = Error;
fn try_from(val: u16) -> Result<Self> {
Self::try_from_bits(val)
}
}
impl TryFrom<&[u8]> for R2 {
type Error = Error;
fn try_from(val: &[u8]) -> Result<Self> {
Self::try_from_bytes(val)
}
}
impl<const N: usize> TryFrom<[u8; N]> for R2 {
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 R2 {
type Error = Error;
fn try_from(val: &[u8; N]) -> Result<Self> {
Self::try_from_bytes(val.as_ref())
}
}
impl From<R2> for [u8; R2::LEN] {
fn from(val: R2) -> Self {
val.bytes()
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::test_field;
#[test]
fn test_fields() {
let mut r2 = R2::new();
test_field!(r2, card_locked: 0);
test_field!(r2, lock_failed: 1);
test_field!(r2, error: 2);
test_field!(r2, cc_error: 3);
test_field!(r2, card_ecc_failed: 4);
test_field!(r2, wp_violation: 5);
test_field!(r2, erase_param: 6);
test_field!(r2, out_of_range: 7);
assert_eq!(r2.r1(), Ok(R1::new()));
(0..=u16::MAX).for_each(|v| {
let [r1, _] = v.to_be_bytes();
match v {
v if R1::try_from_bits(r1).is_err() => {
assert_eq!(
R2::try_from(v),
Err(Error::invalid_field_variant("r2", v as usize))
)
}
v => {
let exp_r1 = R1::try_from_bits(r1).unwrap();
assert_eq!(R2::try_from(v), Ok(R2(v)));
assert_eq!(R2(v).r1(), Ok(exp_r1));
assert_eq!(R2::new().with_r1(exp_r1).r1(), Ok(exp_r1));
}
}
});
}
}