use crate::{len, std::fmt, CommandOps, MessageOps, MessageType};
mod index {
pub const PAYOUT_OPTION: usize = 4;
}
mod bitmask {
pub const OPTION: u8 = 0b11;
}
bitfield! {
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct EnablePayoutOption(u8);
u8;
pub field_0, set_field_0: 0;
pub field_1, set_field_1: 1;
}
impl From<u8> for EnablePayoutOption {
fn from(val: u8) -> Self {
Self(val & bitmask::OPTION)
}
}
impl From<&EnablePayoutOption> for u8 {
fn from(val: &EnablePayoutOption) -> Self {
val.0
}
}
impl From<EnablePayoutOption> for u8 {
fn from(val: EnablePayoutOption) -> Self {
(&val).into()
}
}
impl fmt::Display for EnablePayoutOption {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let f0 = self.field_0();
let f1 = self.field_1();
write!(f, r#"{{"field_0": {f0}, "field_1": {f1}}}"#)
}
}
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct EnablePayoutCommand {
buf: [u8; len::ENABLE_PAYOUT_COMMAND],
}
impl EnablePayoutCommand {
pub fn new() -> Self {
let mut msg = Self {
buf: [0u8; len::ENABLE_PAYOUT_COMMAND],
};
msg.init();
msg.set_command(MessageType::EnablePayout);
msg
}
pub fn option(&self) -> EnablePayoutOption {
self.buf[index::PAYOUT_OPTION].into()
}
pub fn set_option(&mut self, option: EnablePayoutOption) {
self.buf[index::PAYOUT_OPTION] = option.into();
}
pub fn with_option(mut self, option: EnablePayoutOption) -> Self {
self.set_option(option);
self
}
}
impl_default!(EnablePayoutCommand);
impl_message_from_buf!(EnablePayoutCommand);
impl_message_ops!(EnablePayoutCommand);
impl_command_ops!(EnablePayoutCommand);
impl fmt::Display for EnablePayoutCommand {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{{")?;
write!(f, r#""stx": "0x{:02x}", "#, self.stx())?;
write!(f, r#""sequence_id": {}, "#, self.sequence_id())?;
write!(f, r#""len": "0x{:02x}", "#, self.data_len())?;
write!(f, r#""command": {}, "#, self.command())?;
write!(f, r#""option": {}, "#, self.option())?;
write!(f, r#""checksum": "0x{:04x}""#, self.checksum())?;
write!(f, "}}")
}
}