use crate::lib_bitfield;
use crate::result::{Error, Result};
mod xpc;
pub use xpc::*;
lib_bitfield! {
pub Argument(u32): u32 {
pub hcs: 30;
raw_xpc: 28;
pub s18r: 24;
pub vdd_3536: 23;
pub vdd_3435: 22;
pub vdd_3334: 21;
pub vdd_3233: 20;
pub vdd_3132: 19;
pub vdd_3031: 18;
pub vdd_2930: 17;
pub vdd_2829: 16;
pub vdd_2728: 15;
}
}
impl Argument {
pub const LEN: usize = 4;
pub const DEFAULT: u32 = 0;
pub const fn new() -> Self {
Self(Self::DEFAULT)
}
pub const fn xpc(&self) -> Xpc {
Xpc::from_bool(self.raw_xpc())
}
pub fn set_xpc(&mut self, val: Xpc) {
self.set_raw_xpc(val.into_bool());
}
pub const fn from_bits(val: u32) -> Self {
Self(val)
}
pub const fn try_from_bits(val: u32) -> Result<Self> {
Ok(Self::from_bits(val))
}
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)),
_ => Ok(Self(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::*;
use crate::test_field;
#[test]
fn test_fields() {
let mut arg = Argument::new();
let new_xpc = Xpc::new();
test_field!(arg, hcs: 30);
test_field!(arg, s18r: 24);
test_field!(arg, vdd_3536: 23);
test_field!(arg, vdd_3435: 22);
test_field!(arg, vdd_3334: 21);
test_field!(arg, vdd_3233: 20);
test_field!(arg, vdd_3132: 19);
test_field!(arg, vdd_3031: 18);
test_field!(arg, vdd_2930: 17);
test_field!(arg, vdd_2829: 16);
test_field!(arg, vdd_2728: 15);
(1..=u32::BITS)
.map(|r| ((1u64 << r) - 1) as u32)
.for_each(|raw_arg| {
let raw = raw_arg.to_be_bytes();
let mut exp_arg = Argument(raw_arg);
let exp_hcs = (raw_arg & (1 << 30)) != 0;
let exp_xpc = Xpc::from_bool((raw_arg & (1 << 28)) != 0);
let exp_s18r = (raw_arg & (1 << 24)) != 0;
let exp_vdd_3536 = (raw_arg & (1 << 23)) != 0;
let exp_vdd_3435 = (raw_arg & (1 << 22)) != 0;
let exp_vdd_3334 = (raw_arg & (1 << 21)) != 0;
let exp_vdd_3233 = (raw_arg & (1 << 20)) != 0;
let exp_vdd_3132 = (raw_arg & (1 << 19)) != 0;
let exp_vdd_3031 = (raw_arg & (1 << 18)) != 0;
let exp_vdd_2930 = (raw_arg & (1 << 17)) != 0;
let exp_vdd_2829 = (raw_arg & (1 << 16)) != 0;
let exp_vdd_2728 = (raw_arg & (1 << 15)) != 0;
assert_eq!(Argument::try_from_bits(raw_arg), Ok(exp_arg));
assert_eq!(Argument::try_from_bytes(&raw), Ok(exp_arg));
assert_eq!(Argument::from(raw_arg), exp_arg);
assert_eq!(Argument::try_from(raw), Ok(exp_arg));
assert_eq!(Argument::try_from(&raw), Ok(exp_arg));
assert_eq!(exp_arg.hcs(), exp_hcs);
assert_eq!(exp_arg.xpc(), exp_xpc);
assert_eq!(exp_arg.s18r(), exp_s18r);
assert_eq!(exp_arg.vdd_3536(), exp_vdd_3536);
assert_eq!(exp_arg.vdd_3435(), exp_vdd_3435);
assert_eq!(exp_arg.vdd_3334(), exp_vdd_3334);
assert_eq!(exp_arg.vdd_3233(), exp_vdd_3233);
assert_eq!(exp_arg.vdd_3132(), exp_vdd_3132);
assert_eq!(exp_arg.vdd_3031(), exp_vdd_3031);
assert_eq!(exp_arg.vdd_2930(), exp_vdd_2930);
assert_eq!(exp_arg.vdd_2829(), exp_vdd_2829);
assert_eq!(exp_arg.vdd_2728(), exp_vdd_2728);
test_field!(exp_arg, hcs: 30);
test_field!(exp_arg, s18r: 24);
test_field!(exp_arg, vdd_3536: 23);
test_field!(exp_arg, vdd_3435: 22);
test_field!(exp_arg, vdd_3334: 21);
test_field!(exp_arg, vdd_3233: 20);
test_field!(exp_arg, vdd_3132: 19);
test_field!(exp_arg, vdd_3031: 18);
test_field!(exp_arg, vdd_2930: 17);
test_field!(exp_arg, vdd_2829: 16);
test_field!(exp_arg, vdd_2728: 15);
exp_arg.set_xpc(new_xpc);
assert_eq!(exp_arg.xpc(), new_xpc);
exp_arg.set_xpc(exp_xpc);
assert_eq!(exp_arg.xpc(), exp_xpc);
});
}
}