From 6f16e7fdc3ab4f64f70adca3ace578c8c78012b4 Mon Sep 17 00:00:00 2001 From: Alex van de Sandt Date: Thu, 7 May 2026 13:16:14 -0500 Subject: [PATCH] Catch error responses --- src/codec.rs | 28 ++++++++++++++++++++++ src/command/bc125at/battery_charge_time.rs | 2 +- src/command/bc125at/lcd_contrast.rs | 2 +- 3 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/codec.rs b/src/codec.rs index 558a51c..3660680 100644 --- a/src/codec.rs +++ b/src/codec.rs @@ -84,6 +84,12 @@ pub enum DecoderError { #[error("malformed response")] Malformed, + #[error("command must be executed in program mode")] + NotAcceptable, + + #[error("command format error or invalid value")] + ErrorResponse, + #[error(transparent)] DelimiterError(#[from] AnyDelimiterCodecError), @@ -110,6 +116,14 @@ impl Decoder for Codec { let raw_values = all_fields.collect::>(); + if raw_values.len() == 1 { + match raw_values[0].as_ref() { + b"NG" => return Err(DecoderError::NotAcceptable), + b"ERR" => return Err(DecoderError::ErrorResponse), + _ => {} + } + } + Ok(Some(RawResponse { cmd, raw_values })) } } @@ -219,6 +233,20 @@ mod tests { assert_matches!(err, DecoderError::Malformed); } + #[test] + fn not_acceptable_response() { + let err = decode(b"CMD,NG\r").unwrap_err(); + + assert_matches!(err, DecoderError::NotAcceptable); + } + + #[test] + fn error_response() { + let err = decode(b"CMD,ERR\r").unwrap_err(); + + assert_matches!(err, DecoderError::ErrorResponse); + } + #[test] fn delimiter_error() { let mut codec = Codec::new(); diff --git a/src/command/bc125at/battery_charge_time.rs b/src/command/bc125at/battery_charge_time.rs index 4eaffa8..2d87bed 100644 --- a/src/command/bc125at/battery_charge_time.rs +++ b/src/command/bc125at/battery_charge_time.rs @@ -1,6 +1,6 @@ use crate::command::{OkResponse, command, range_param, range_response}; -range_param!(BatteryChargeTime(0..=16): u8); +range_param!(BatteryChargeTime(1..=16): u8); range_response!(BatteryChargeTime => BatteryChargeTimeError : InvalidBatteryChargeTime("volume must be between [1..16], got `{0}`")); command!(b"BSV": GetBatteryInfo => BatteryChargeTime); diff --git a/src/command/bc125at/lcd_contrast.rs b/src/command/bc125at/lcd_contrast.rs index 80ff740..f40a74e 100644 --- a/src/command/bc125at/lcd_contrast.rs +++ b/src/command/bc125at/lcd_contrast.rs @@ -1,6 +1,6 @@ use crate::command::{OkResponse, command, range_param, range_response}; -range_param!(LcdContrast(1..=15): u8); +range_param!(LcdContrast(0..=15): u8); range_response!(LcdContrast => LcdContrastError : InvalidLcdContrast("LCD contrast must be between [1..15], got `{0}`")); command!(b"CNT": GetLcdContrast => LcdContrast); -- 2.51.2