diff --git a/src/codec.rs b/src/codec.rs index 514ab4a..558a51c 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -128,18 +128,8 @@ mod tests { command::{OkResponse, command, range_param, range_response}, }; - #[derive(Debug, thiserror::Error)] - pub enum ParamError { - #[error("invalid UTF-8 bytes")] - Utf8Error(#[from] std::str::Utf8Error), - #[error(transparent)] - Parse(#[from] std::num::ParseIntError), - #[error("invalid value, got `{0}`")] - Invalid(u8), - } - range_param!(RangeParam(0..=15): u8); - range_response!(RangeParam => ParamError : Invalid); + range_response!(RangeParam => ParamError : Invalid("invalid")); command!(b"CMD": SimpleCommand); command!(b"CMD": ResponseCommand => RangeParam); diff --git a/src/command/bc125at/squelch.rs b/src/command/bc125at/squelch.rs index b11d0a0..5b02d80 100644 --- a/src/command/bc125at/squelch.rs +++ b/src/command/bc125at/squelch.rs @@ -1,19 +1,7 @@ -use std::{num::ParseIntError, str::Utf8Error}; - use crate::command::{OkResponse, command, range_param, range_response}; -#[derive(Debug, thiserror::Error)] -pub enum SquelchLevelError { - #[error("invalid UTF-8 bytes")] - Utf8Error(#[from] Utf8Error), - #[error(transparent)] - Parse(#[from] ParseIntError), - #[error("squelch must be between [0..15], got `{0}`")] - InvalidSquelch(u8), -} - range_param!(SquelchLevel(0..=15): u8); -range_response!(SquelchLevel => SquelchLevelError : InvalidSquelch); +range_response!(SquelchLevel => SquelchLevelError : InvalidSquelch("squelch must be between [0..15], got `{0}`")); command!(b"SQL": GetSquelchLevel => SquelchLevel); command!(b"SQL": SetSquelchLevel(SquelchLevel) => OkResponse); diff --git a/src/command/bc125at/volume.rs b/src/command/bc125at/volume.rs index bd47e7d..b9be9e9 100644 --- a/src/command/bc125at/volume.rs +++ b/src/command/bc125at/volume.rs @@ -1,19 +1,7 @@ -use std::{num::ParseIntError, str::Utf8Error}; - use crate::command::{OkResponse, command, range_param, range_response}; -#[derive(Debug, thiserror::Error)] -pub enum VolumeLevelError { - #[error("invalid UTF-8 bytes")] - Utf8Error(#[from] Utf8Error), - #[error(transparent)] - Parse(#[from] ParseIntError), - #[error("volume must be between [0..15], got `{0}`")] - InvalidVolume(u8), -} - range_param!(VolumeLevel(0..=15): u8); -range_response!(VolumeLevel => VolumeLevelError : InvalidVolume); +range_response!(VolumeLevel => VolumeLevelError : InvalidVolume("volume must be between [0..15], got `{0}`")); command!(b"VOL": GetVolumeLevel => VolumeLevel); command!(b"VOL": SetVolumeLevel(VolumeLevel) => OkResponse); diff --git a/src/command/macros/command.rs b/src/command/macros/command.rs index bf1e2e9..06163cd 100644 --- a/src/command/macros/command.rs +++ b/src/command/macros/command.rs @@ -51,18 +51,8 @@ mod tests { command::{NoParams, range_param, range_response}, }; - #[derive(Debug, thiserror::Error)] - pub enum ParamError { - #[error("invalid UTF-8 bytes")] - Utf8Error(#[from] std::str::Utf8Error), - #[error(transparent)] - Parse(#[from] std::num::ParseIntError), - #[error("invalid value, got `{0}`")] - Invalid(u8), - } - range_param!(RangeParam(0..=15): u8); - range_response!(RangeParam => ParamError : Invalid); + range_response!(RangeParam => ParamError : Invalid("invalid")); #[test] fn no_params_ok_response() { diff --git a/src/command/macros/range_response.rs b/src/command/macros/range_response.rs index 9960a61..5016410 100644 --- a/src/command/macros/range_response.rs +++ b/src/command/macros/range_response.rs @@ -1,5 +1,15 @@ macro_rules! range_response { - ($name:ty => $error:ty : $variant:ident) => { + ($name:ty => $error:ident : $invalid_variant:ident($msg:literal)) => { + #[derive(::core::fmt::Debug, ::thiserror::Error)] + pub enum $error { + #[error(transparent)] + Utf8Error(#[from] ::core::str::Utf8Error), + #[error(transparent)] + Parse(#[from] ::core::num::ParseIntError), + #[error($msg)] + $invalid_variant(u8), + } + impl crate::command::Response for $name { type Error = $error; @@ -7,7 +17,7 @@ macro_rules! range_response { raw_values: &[::tokio_util::bytes::Bytes], ) -> ::core::result::Result { let level = ::core::str::from_utf8(&raw_values[0])?.parse()?; - Self::new(level).ok_or(Self::Error::$variant(level)) + Self::new(level).ok_or(Self::Error::$invalid_variant(level)) } fn expected_field_count() -> usize { 1 @@ -24,18 +34,8 @@ mod tests { use crate::command::{Response, range_param, test::deserialize}; - #[derive(Debug, thiserror::Error)] - pub enum ParamError { - #[error("invalid UTF-8 bytes")] - Utf8Error(#[from] std::str::Utf8Error), - #[error(transparent)] - Parse(#[from] std::num::ParseIntError), - #[error("invalid value, got `{0}`")] - Invalid(u8), - } - range_param!(U8RangeParam(0..=10): u8); - range_response!(U8RangeParam => ParamError : Invalid); + range_response!(U8RangeParam => ParamError : Invalid("invalid")); #[test] fn expected_field_count() {