From a49c0f431a717e706f125a2022cceea1d19e704d Mon Sep 17 00:00:00 2001 From: Alex van de Sandt Date: Mon, 27 Apr 2026 14:07:54 -0500 Subject: [PATCH] Move macros to submodule --- src/codec/command.rs | 127 ++++++------------------------------ src/codec/command/macros.rs | 91 ++++++++++++++++++++++++++ 2 files changed, 112 insertions(+), 106 deletions(-) create mode 100644 src/codec/command/macros.rs diff --git a/src/codec/command.rs b/src/codec/command.rs index 4f81bdf..c5ba47c 100644 --- a/src/codec/command.rs +++ b/src/codec/command.rs @@ -1,3 +1,6 @@ +#[macro_use] +mod macros; + mod private { pub trait Sealed {} } @@ -24,102 +27,14 @@ impl Param for u8 { } } -macro_rules! command { - ($cmd:literal, $name:ident) => { - #[derive(Clone, Debug)] - pub struct $name; - - command!(@traits $cmd, $name); - - impl Command for $name { - fn as_bytes(&self) -> &'static [u8] { $cmd } - } - }; - ($cmd:literal, $name: ident ( $param_ty:ty )) => { - #[derive(Clone, Debug)] - pub struct $name(pub $param_ty); - - command!(@traits $cmd, $name); - - impl Command for $name { - fn as_bytes(&self) -> &'static [u8] { $cmd } - fn params(&self) -> impl IntoIterator { - [&self.0 as &dyn Param] - } - } - }; - ($cmd:literal, $name: ident { $($param:ident: $param_ty:ty),+ }) => { - #[derive(Clone, Debug)] - pub struct $name { $(pub $param: $param_ty),+ } - - command!(@traits $cmd, $name); - - impl Command for $name { - fn as_bytes(&self) -> &'static [u8] { $cmd } - fn params(&self) -> impl IntoIterator { - [$(&self.$param as &dyn Param),+] - } - } - }; - (@traits $cmd:literal, $name:ident) => { - impl private::Sealed for $name {} - impl std::fmt::Display for $name { - fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { - String::from_utf8_lossy($cmd).fmt(f) - } - } - }; -} - -macro_rules! param { - (pub enum $name:ident { $($variant:ident => $val:literal),+ $(,)? }) => { - #[derive(Clone, Debug)] - pub enum $name { - $($variant),+ - } - - param!(@traits $name); - - impl Param for $name { - fn write_bytes(&self, dst: &mut tokio_util::bytes::BytesMut) { - let s = match self { - $(Self::$variant => $val.as_slice()),+ - }; - dst.extend_from_slice(s); - } - } - }; - (pub range $name:ident ($range:expr)) => { - #[derive(Clone, Debug)] - pub struct $name(u8); - - param!(@traits $name); - - impl $name { - pub fn new(value: u8) -> Self { - assert!($range.contains(&value)); - Self(value) - } - } - impl Param for $name { - fn write_bytes(&self, dst: &mut tokio_util::bytes::BytesMut) { - self.0.write_bytes(dst); - } - } - }; - (@traits $name:ident) => { - impl private::Sealed for $name {} - }; -} - -command!(b"PRG", EnterProgramMode); -command!(b"EPG", ExitProgramMode); -command!(b"MDL", GetModelInfo); -command!(b"VER", GetFirmwareVersion); +gen_command!(b"PRG", EnterProgramMode); +gen_command!(b"EPG", ExitProgramMode); +gen_command!(b"MDL", GetModelInfo); +gen_command!(b"VER", GetFirmwareVersion); -command!(b"BLT", GetBacklight); -command!(b"BLT", SetBacklight(Backlight)); -param!(pub enum Backlight { +gen_command!(b"BLT", GetBacklight); +gen_command!(b"BLT", SetBacklight(Backlight)); +gen_param!(pub enum Backlight { AlwaysOn => b"AO", AlwaysOff => b"AF", Keypress => b"KY", @@ -127,32 +42,32 @@ param!(pub enum Backlight { KeySquelch => b"KS", }); -command!(b"BSV", GetBatteryInfo); -command!(b"BSV", SetBatteryInfo(BatteryChargeTime)); -param!(pub range BatteryChargeTime(1..=16)); +gen_command!(b"BSV", GetBatteryInfo); +gen_command!(b"BSV", SetBatteryInfo(BatteryChargeTime)); +gen_param!(pub range BatteryChargeTime(1..=16)); -command!(b"CLR", ClearAllMemory); +gen_command!(b"CLR", ClearAllMemory); -command!(b"BPL", GetBandPlan); -command!(b"BPL", SetBandPlan(BandPlan)); -param!(pub enum BandPlan { +gen_command!(b"BPL", GetBandPlan); +gen_command!(b"BPL", SetBandPlan(BandPlan)); +gen_param!(pub enum BandPlan { Usa => b"0", Canada => b"1" }); -command!(b"KBP", GetKeyBeep); -command!( +gen_command!(b"KBP", GetKeyBeep); +gen_command!( b"KBP", SetKeyBeep { beep_level: BeepLevel, key_lock_status: KeyLockStatus } ); -param!(pub enum BeepLevel { +gen_param!(pub enum BeepLevel { Auto => b"0", Off => b"99", }); -param!(pub enum KeyLockStatus { +gen_param!(pub enum KeyLockStatus { Off => b"0", On => b"1", }); diff --git a/src/codec/command/macros.rs b/src/codec/command/macros.rs new file mode 100644 index 0000000..8a937aa --- /dev/null +++ b/src/codec/command/macros.rs @@ -0,0 +1,91 @@ +// #[macro_export] +// #[doc(hidden)] +macro_rules! gen_command { + ($cmd:literal, $name:ident) => { + #[derive(Clone, Debug)] + pub struct $name; + + gen_command!(@traits $cmd, $name); + + impl Command for $name { + fn as_bytes(&self) -> &'static [u8] { $cmd } + } + }; + ($cmd:literal, $name: ident ( $param_ty:ty )) => { + #[derive(Clone, Debug)] + pub struct $name(pub $param_ty); + + gen_command!(@traits $cmd, $name); + + impl Command for $name { + fn as_bytes(&self) -> &'static [u8] { $cmd } + fn params(&self) -> impl IntoIterator { + [&self.0 as &dyn Param] + } + } + }; + ($cmd:literal, $name: ident { $($param:ident: $param_ty:ty),+ }) => { + #[derive(Clone, Debug)] + pub struct $name { $(pub $param: $param_ty),+ } + + gen_command!(@traits $cmd, $name); + + impl Command for $name { + fn as_bytes(&self) -> &'static [u8] { $cmd } + fn params(&self) -> impl IntoIterator { + [$(&self.$param as &dyn Param),+] + } + } + }; + (@traits $cmd:literal, $name:ident) => { + impl private::Sealed for $name {} + impl std::fmt::Display for $name { + fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> { + String::from_utf8_lossy($cmd).fmt(f) + } + } + }; +} + +// #[macro_export] +// #[doc(hidden)] +macro_rules! gen_param { + (pub enum $name:ident { $($variant:ident => $val:literal),+ $(,)? }) => { + #[derive(Clone, Debug)] + pub enum $name { + $($variant),+ + } + + gen_param!(@traits $name); + + impl Param for $name { + fn write_bytes(&self, dst: &mut tokio_util::bytes::BytesMut) { + let s = match self { + $(Self::$variant => $val.as_slice()),+ + }; + dst.extend_from_slice(s); + } + } + }; + (pub range $name:ident ($range:expr)) => { + #[derive(Clone, Debug)] + pub struct $name(u8); + + gen_param!(@traits $name); + + impl $name { + pub fn new(value: u8) -> Self { + assert!($range.contains(&value)); + Self(value) + } + } + impl Param for $name { + fn write_bytes(&self, dst: &mut tokio_util::bytes::BytesMut) { + self.0.write_bytes(dst); + } + } + }; + (@traits $name:ident) => { + impl private::Sealed for $name {} + }; +} -- 2.51.2