Skip to content

Commit

Permalink
lib: add derive traits
Browse files Browse the repository at this point in the history
Adds common derive traits to command and reply types that were missing
them. Results in better ergonomics for crate users.

Removes implementing the `Default` trait using `impl_default` where
possible, and substitutes `const fn default` impls with the `Default`
trait. This allows containing types to also implement `Default` with the
derive macro.
  • Loading branch information
ebds-rs committed Aug 17, 2023
1 parent 22b2642 commit 581c6ee
Show file tree
Hide file tree
Showing 40 changed files with 171 additions and 197 deletions.
5 changes: 2 additions & 3 deletions src/advanced_bookmark_mode/command.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
bool_enum, impl_default, impl_extended_ops, impl_message_ops, impl_omnibus_extended_command,
bool_enum, impl_extended_ops, impl_message_ops, impl_omnibus_extended_command,
len::ADVANCED_BOOKMARK_MODE_COMMAND, ExtendedCommand, ExtendedCommandOps, MessageOps,
MessageType,
};
Expand Down Expand Up @@ -56,7 +56,7 @@ bool_enum!(
///
/// The Status byte tells the device to enable (0x01) or disable (0x00) the mode.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AdvancedBookmarkModeCommand {
buf: [u8; ADVANCED_BOOKMARK_MODE_COMMAND],
}
Expand Down Expand Up @@ -86,7 +86,6 @@ impl AdvancedBookmarkModeCommand {
}
}

impl_default!(AdvancedBookmarkModeCommand);
impl_message_ops!(AdvancedBookmarkModeCommand);
impl_extended_ops!(AdvancedBookmarkModeCommand);
impl_omnibus_extended_command!(AdvancedBookmarkModeCommand);
Expand Down
5 changes: 2 additions & 3 deletions src/advanced_bookmark_mode/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::std;
use std::fmt;

use crate::{
bool_enum, impl_default, impl_extended_ops, impl_message_ops, impl_omnibus_extended_reply,
bool_enum, impl_extended_ops, impl_message_ops, impl_omnibus_extended_reply,
len::ADVANCED_BOOKMARK_MODE_REPLY, ExtendedCommand, ExtendedCommandOps, MessageOps,
MessageType, OmnibusReplyOps,
};
Expand Down Expand Up @@ -37,7 +37,7 @@ May also indicate the device was busy (NAKs when stacking or powering up).
/// When the device stacks a document in this mode, the Standard Omnibus Reply (section 7.1.2) is
/// reported with the stacked bit set and no value reported in data byte 2.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct AdvancedBookmarkModeReply {
buf: [u8; ADVANCED_BOOKMARK_MODE_REPLY],
}
Expand Down Expand Up @@ -67,7 +67,6 @@ impl AdvancedBookmarkModeReply {
}
}

impl_default!(AdvancedBookmarkModeReply);
impl_message_ops!(AdvancedBookmarkModeReply);
impl_omnibus_extended_reply!(AdvancedBookmarkModeReply);
impl_extended_ops!(AdvancedBookmarkModeReply);
Expand Down
54 changes: 29 additions & 25 deletions src/banknote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ pub type ISOCode = Currency;

/// A three character ASCII coded decimal value
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct BaseValue(u16);

impl BaseValue {
Expand Down Expand Up @@ -75,8 +75,9 @@ impl<const N: usize> From<&[u8; N]> for BaseValue {
/// An ASCII coded sign value for the Exponent.
/// This field is either a “+” or a “-“
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Sign {
#[default]
Positive,
Negative,
}
Expand Down Expand Up @@ -117,11 +118,17 @@ pub struct Exponent(u8);
impl Exponent {
pub const LEN: usize = 2;

pub const fn default() -> Self {
pub const fn new() -> Self {
Self(1)
}
}

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

impl From<u8> for Exponent {
fn from(b: u8) -> Self {
Self(b)
Expand Down Expand Up @@ -190,8 +197,9 @@ impl<const N: usize> From<&[u8; N]> for Exponent {
/// Extended orientation bit is set in device
/// capabilities map.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum BanknoteOrientation {
#[default]
RightEdgeFaceUp = 0x00,
RightEdgeFaceDown = 0x01,
LeftEdgeFaceUp = 0x02,
Expand Down Expand Up @@ -251,13 +259,13 @@ impl fmt::Display for BanknoteOrientation {
macro_rules! ascii_tuple_struct {
($name:ident, $base:tt, $doc:tt) => {
#[doc = $doc]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct $name($base);

impl $name {
pub const LEN: usize = std::mem::size_of::<$base>();

pub const fn default() -> Self {
pub const fn new() -> Self {
Self(0)
}

Expand Down Expand Up @@ -332,15 +340,17 @@ ascii_tuple_struct!(
"
);

/// Represents how the BAU device classifies a [Banknote].
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum BanknoteClassification {
/// Sent for any the following:
///
/// - In response to a Host Query Extended Note Specification Command (i.e. host requests a note table element).
/// - In response to a note escrowed or stacked event while device is in extended note mode and classification is
/// - Supported by the device but disabled.
/// - NOT supported by the device.
#[default]
DisabledOrNotSupported = 0x00,
/// Class 1 (unidentified banknote)
Unidentified = 0x01,
Expand All @@ -353,7 +363,7 @@ pub enum BanknoteClassification {
}

impl BanknoteClassification {
pub const fn default() -> Self {
pub const fn new() -> Self {
Self::DisabledOrNotSupported
}
}
Expand Down Expand Up @@ -400,7 +410,7 @@ impl fmt::Display for BanknoteClassification {

/// The banknote value
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct Banknote {
/// The banknote value.
pub(crate) value: f32,
Expand All @@ -419,6 +429,7 @@ pub struct Banknote {
}

impl Banknote {
/// Creates a new [Banknote] with the provided parameters.
pub const fn new(
value: f32,
iso_code: ISOCode,
Expand All @@ -439,15 +450,16 @@ impl Banknote {
}
}

pub const fn default() -> Self {
/// Creates a new null [Banknote].
pub const fn null() -> Self {
Self {
value: 0f32,
value: 0.0,
iso_code: ISOCode::new(),
note_type: NoteType::default(),
note_series: NoteSeries::default(),
note_compatibility: NoteCompatibility::default(),
note_version: NoteVersion::default(),
banknote_classification: BanknoteClassification::default(),
note_type: NoteType::new(),
note_series: NoteSeries::new(),
note_compatibility: NoteCompatibility::new(),
note_version: NoteVersion::new(),
banknote_classification: BanknoteClassification::new(),
}
}

Expand Down Expand Up @@ -546,7 +558,7 @@ impl fmt::Display for Banknote {

/// Extended note table item
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct NoteTableItem {
pub(crate) note_index: usize,
pub(crate) banknote: Banknote,
Expand All @@ -561,14 +573,6 @@ impl NoteTableItem {
}
}

/// Creates a default (null) [NoteTableItem]
pub const fn default() -> Self {
Self {
note_index: 0,
banknote: Banknote::default(),
}
}

/// Get whether the [NoteTableItem] is null, indicating the end of the note table
pub fn is_null(&self) -> bool {
self == &Self::default()
Expand Down
10 changes: 5 additions & 5 deletions src/clear_audit_data/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::std;
use std::fmt;

use crate::{
bool_enum, impl_default, impl_extended_ops, impl_message_ops, impl_omnibus_extended_reply,
bool_enum, impl_extended_ops, impl_message_ops, impl_omnibus_extended_reply,
len::{CLEAR_AUDIT_DATA_REQUEST_ACK, CLEAR_AUDIT_DATA_REQUEST_RESULTS},
ExtendedCommand, ExtendedCommandOps, MessageOps, MessageType, OmnibusReplyOps,
};
Expand Down Expand Up @@ -47,7 +47,8 @@ pub mod index {
/// device will NAK all Clear Audit Data Request
/// * Device is in calibration mode.
/// * The device is currently servicing another type 7 message request.
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ClearAuditDataRequestAck {
buf: [u8; CLEAR_AUDIT_DATA_REQUEST_ACK],
}
Expand Down Expand Up @@ -79,7 +80,6 @@ impl ClearAuditDataRequestAck {
}
}

impl_default!(ClearAuditDataRequestAck);
impl_message_ops!(ClearAuditDataRequestAck);
impl_omnibus_extended_reply!(ClearAuditDataRequestAck);
impl_extended_ops!(ClearAuditDataRequestAck);
Expand Down Expand Up @@ -119,7 +119,8 @@ impl fmt::Display for ClearAuditDataRequestAck {
/// |:------|:----:|:----:|:----:|:-------:|:------:|:------:|:------:|:------:|:------:|:------:|:---------:|:----:|:---:|
/// | Byte | 0 | 1 | 2 | 3 | 4 | 5 | 6 | 7 | 8 | 9 | 10 | 11 | 12 |
/// | Value | 0x02 | 0x0D | 0x7n | 0x1D | nn | nn | nn | nn | nn | nn | 0x00/01 | 0x03 | zz |
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ClearAuditDataRequestResults {
buf: [u8; CLEAR_AUDIT_DATA_REQUEST_RESULTS],
}
Expand All @@ -143,7 +144,6 @@ impl ClearAuditDataRequestResults {
}
}

impl_default!(ClearAuditDataRequestResults);
impl_message_ops!(ClearAuditDataRequestResults);
impl_omnibus_extended_reply!(ClearAuditDataRequestResults);
impl_extended_ops!(ClearAuditDataRequestResults);
Expand Down
5 changes: 3 additions & 2 deletions src/clear_audit_data/request.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
impl_default, impl_extended_ops, impl_message_ops, impl_omnibus_extended_command,
impl_extended_ops, impl_message_ops, impl_omnibus_extended_command,
len::CLEAR_AUDIT_DATA_REQUEST, ExtendedCommand, ExtendedCommandOps, MessageOps, MessageType,
};

Expand All @@ -19,6 +19,8 @@ use crate::{
///
/// Since the command needs to clear large sections of memory, the command may take a few seconds to
/// complete.
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ClearAuditDataRequest {
buf: [u8; CLEAR_AUDIT_DATA_REQUEST],
}
Expand All @@ -37,7 +39,6 @@ impl ClearAuditDataRequest {
}
}

impl_default!(ClearAuditDataRequest);
impl_message_ops!(ClearAuditDataRequest);
impl_extended_ops!(ClearAuditDataRequest);
impl_omnibus_extended_command!(ClearAuditDataRequest);
Expand Down
11 changes: 7 additions & 4 deletions src/denomination.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ use std::fmt;

/// Cash denominations
#[repr(u32)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum Denomination {
#[default]
Zero = 0,
One = 1,
Two = 2,
Expand Down Expand Up @@ -96,7 +97,7 @@ impl From<Denomination> for u32 {

bitfield! {
/// Enable/disable note denominations while in base note mode
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct StandardDenomination(u8);
u8;
pub one, set_one: 0;
Expand Down Expand Up @@ -224,7 +225,7 @@ impl From<StandardDenominationFlag> for StandardDenomination {

/// Bit flags for [StandardDenomination]s.
#[repr(u8)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub enum StandardDenominationFlag {
Denom1 = 0b000_0001,
Denom2 = 0b000_0010,
Expand All @@ -233,11 +234,13 @@ pub enum StandardDenominationFlag {
Denom5 = 0b001_0000,
Denom6 = 0b010_0000,
Denom7 = 0b100_0000,
#[default]
Zero = 0b000_0000,
}

impl StandardDenominationFlag {
pub const fn default() -> Self {
/// Creates a new [StandardDenominationFlag].
pub const fn new() -> Self {
Self::Zero
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/extended_note_inhibits/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ pub mod index {

bitfield! {
/// Represents enabled notes in the extended note table.
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct EnableNote(u8);
u8;
pub note1, set_note1: 0;
Expand Down Expand Up @@ -179,6 +179,7 @@ impl From<EnableNote> for u8 {
/// | Enable 19 | - | - | - | - | - | Note 128 | Note 127 |
///
/// If the bit equals 1 then the note is enabled.
#[derive(Clone, Copy, Debug, PartialEq)]
pub struct SetExtendedNoteInhibits<const M: usize, const N: usize> {
buf: [u8; M],
}
Expand Down
5 changes: 2 additions & 3 deletions src/extended_note_inhibits/reply.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::std;
use std::fmt;

use crate::{
impl_default, impl_extended_reply_ops, impl_message_ops, impl_omnibus_extended_reply,
impl_extended_reply_ops, impl_message_ops, impl_omnibus_extended_reply,
len::EXTENDED_NOTE_INHIBITS_REPLY_ALT, ExtendedCommand, ExtendedReplyOps, MessageOps,
MessageType, OmnibusReply,
};
Expand Down Expand Up @@ -31,7 +31,7 @@ pub type ExtendedNoteInhibitsReply = OmnibusReply;
/// **WARNING** In order to avoid possible confusion processing the extended note data, this command should
/// only be sent when the device is in the idle state.
#[repr(C)]
#[derive(Clone, Copy, Debug, PartialEq)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct ExtendedNoteInhibitsReplyAlt {
buf: [u8; EXTENDED_NOTE_INHIBITS_REPLY_ALT],
}
Expand All @@ -51,7 +51,6 @@ impl ExtendedNoteInhibitsReplyAlt {
}
}

impl_default!(ExtendedNoteInhibitsReplyAlt);
impl_message_ops!(ExtendedNoteInhibitsReplyAlt);
impl_omnibus_extended_reply!(ExtendedNoteInhibitsReplyAlt);
impl_extended_reply_ops!(ExtendedNoteInhibitsReplyAlt);
Expand Down
6 changes: 3 additions & 3 deletions src/extended_note_specification/query.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
impl_default, impl_extended_ops, impl_message_ops, impl_omnibus_extended_command,
impl_extended_ops, impl_message_ops, impl_omnibus_extended_command,
len::QUERY_EXTENDED_NOTE_SPECIFICATION, ExtendedCommand, ExtendedCommandOps,
ExtendedNoteReporting, MessageOps, MessageType, OmnibusCommandOps,
};
Expand All @@ -23,7 +23,8 @@ pub mod index {
///
/// The first extended data byte is the Index. This index value starts at `1` and represents the index of the
/// extended note table data in the device.
#[derive(Clone, Copy, Debug, PartialEq)]
#[repr(C)]
#[derive(Clone, Copy, Debug, Default, PartialEq)]
pub struct QueryExtendedNoteSpecification {
buf: [u8; QUERY_EXTENDED_NOTE_SPECIFICATION],
}
Expand Down Expand Up @@ -54,7 +55,6 @@ impl QueryExtendedNoteSpecification {
}
}

impl_default!(QueryExtendedNoteSpecification);
impl_message_ops!(QueryExtendedNoteSpecification);
impl_extended_ops!(QueryExtendedNoteSpecification);
impl_omnibus_extended_command!(QueryExtendedNoteSpecification);
Expand Down
Loading

0 comments on commit 581c6ee

Please sign in to comment.