Skip to content

Commit abe2f20

Browse files
committed
implement core::error::Error
this API has been stabilised with Rust 1.81.0. accordingly the MSRV has been raised.
1 parent 09c49dd commit abe2f20

File tree

6 files changed

+82
-10
lines changed

6 files changed

+82
-10
lines changed

.github/workflows/CI.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
rust: [1.62.0, stable]
16+
rust: [1.81.0, stable]
1717
features: ['use_alloc', 'use_alloc,defmt', 'use_heapless', 'use_heapless,defmt']
1818
exclude:
19-
- rust: 1.62.0
19+
- rust: 1.81.0
2020
features: 'use_alloc,defmt'
21-
- rust: 1.62.0
21+
- rust: 1.81.0
2222
features: 'use_heapless,defmt'
2323
runs-on: ubuntu-latest
2424
steps:

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
<!-- next-header -->
88
## [Unreleased] - ReleaseDate
9+
### Changed
10+
* The MSRV has been updated to 1.81.0 due to `core::error::Error` being implemented
911

1012
## [0.2.0] - 2023-11-14
1113
### Added

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
name = "adafruit-bluefruit-protocol"
33
version = "0.2.0"
44
edition = "2021"
5-
rust-version = "1.62"
5+
rust-version = "1.81"
66

77
description = "A `no_std` parser for the Adafruit Bluefruit LE Connect controller protocol."
88
repository = "https://github.com/rust-embedded-community/adafruit-bluefruit-protocol-rs"

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,5 +30,5 @@ A simple example for the STM32F4 microcontrollers is [available](examples/stm32f
3030
For the changelog please see the dedicated [CHANGELOG.md](CHANGELOG.md).
3131

3232
## Minimum Supported Rust Version (MSRV)
33-
This crate is guaranteed to compile on stable Rust 1.62 and up. It *might*
33+
This crate is guaranteed to compile on stable Rust 1.81 and up. It *might*
3434
compile with older versions but that may change in any new patch release.

src/button_event.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
//! Implements the [`ButtonEvent`] and its parsing from the protocol.
22
33
use super::ProtocolParseError;
4+
use core::error::Error;
5+
use core::fmt::{Display, Formatter};
46

57
/// Errors which can be raised while parsing a button event.
68
#[derive(PartialEq, Eq, Debug)]
@@ -12,6 +14,18 @@ pub enum ButtonParseError {
1214
UnknownButtonState(u8),
1315
}
1416

17+
impl Display for ButtonParseError {
18+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
19+
use ButtonParseError::*;
20+
match self {
21+
UnknownButton(button) => write!(f, "Unknown button: {:#x}", button),
22+
UnknownButtonState(state) => write!(f, "Unknown button state: {:#x}", state),
23+
}
24+
}
25+
}
26+
27+
impl Error for ButtonParseError {}
28+
1529
/// Lists all possible buttons which can be sent in the event.
1630
#[derive(PartialEq, Eq, Debug)]
1731
#[cfg_attr(feature = "defmt", derive(defmt::Format))]

src/lib.rs

Lines changed: 61 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,8 @@ use heapless::Vec;
6666
extern crate alloc;
6767
#[cfg(feature = "use_alloc")]
6868
use alloc::vec::Vec;
69+
use core::error::Error;
70+
use core::fmt::{Display, Formatter};
6971
#[cfg(feature = "location_event")]
7072
use location_event::LocationEvent;
7173
#[cfg(feature = "magnetometer_event")]
@@ -101,8 +103,7 @@ pub enum ProtocolParseError {
101103
/// The message contained an event which is not known to the current implementation.
102104
/// This can mean that:
103105
/// * the message was malformed or
104-
/// * that a newer protocol version has been used or
105-
/// * that the event type has not been enabled as a feature.
106+
/// * that a newer protocol version has been used.
106107
UnknownEvent(Option<u8>),
107108
/// The message contained an event which is known to the library but has not been selected as a feature and can thus not be parsed. Select the feature when compiling the library to handle this message.
108109
DisabledControllerDataPackageType(ControllerDataPackageType),
@@ -117,6 +118,44 @@ pub enum ProtocolParseError {
117118
InvalidFloatSize(usize),
118119
}
119120

121+
impl Display for ProtocolParseError {
122+
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
123+
use ProtocolParseError::*;
124+
match self {
125+
UnknownEvent(event) => write!(f, "Unknown event type: {:?}", event),
126+
DisabledControllerDataPackageType(event) => {
127+
write!(f, "Disabled event type: {:?}", event)
128+
}
129+
ButtonParseError(_) => write!(f, "Error while parsing button event"),
130+
InvalidLength(expected, actual) => write!(
131+
f,
132+
"Invalid message length: expected {} but received {}",
133+
expected, actual
134+
),
135+
InvalidCrc(expected, actual) => write!(
136+
f,
137+
"Invalid CRC: expected {:#x} but calculated {:#x}",
138+
expected, actual
139+
),
140+
InvalidFloatSize(length) => write!(
141+
f,
142+
"Failed to parse float from a message with size {}",
143+
length
144+
),
145+
}
146+
}
147+
}
148+
149+
impl Error for ProtocolParseError {
150+
fn source(&self) -> Option<&(dyn Error + 'static)> {
151+
use ProtocolParseError::*;
152+
match self {
153+
ButtonParseError(e) => Some(e),
154+
_ => None,
155+
}
156+
}
157+
}
158+
120159
/// Lists all data packages which can be sent by the controller. Internal state used during parsing. Use [`ControllerEvent`] to return the actual event.
121160
#[derive(PartialEq, Eq, Debug)]
122161
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
@@ -340,7 +379,7 @@ fn try_f32_from_le_bytes(input: &[u8]) -> Result<f32, ProtocolParseError> {
340379

341380
#[cfg(test)]
342381
mod tests {
343-
use crate::button_event::{Button, ButtonState};
382+
use crate::button_event::{Button, ButtonParseError, ButtonState};
344383
use crate::{check_crc, parse, try_f32_from_le_bytes, ControllerEvent, ProtocolParseError};
345384

346385
fn assert_is_button_event(
@@ -359,16 +398,33 @@ mod tests {
359398

360399
#[test]
361400
fn test_parse() {
362-
let input = b"\x00!B11:!B10;\x00\x00!\x00\x00\x00\x00";
401+
let input = b"\x00!B11:!B10;\x00\x00!\x00\x00\x00\x00!B138";
363402
#[cfg(feature = "use_heapless")]
364403
let result = parse::<4>(input);
365404
#[cfg(feature = "use_alloc")]
366405
let result = parse(input);
367406

368-
assert_eq!(result.len(), 3);
407+
assert_eq!(result.len(), 4);
369408
assert_is_button_event(&result[0], Button::Button1, ButtonState::Pressed);
370409
assert_is_button_event(&result[1], Button::Button1, ButtonState::Released);
371410
assert_eq!(result[2], Err(ProtocolParseError::UnknownEvent(Some(0))));
411+
if let Err(e) = &result[3] {
412+
assert_eq!(
413+
e,
414+
&ProtocolParseError::ButtonParseError(ButtonParseError::UnknownButtonState(b'3'))
415+
);
416+
#[cfg(feature = "use_alloc")]
417+
{
418+
use alloc::string::ToString;
419+
use core::error::Error;
420+
assert_eq!(
421+
e.source().unwrap().to_string(),
422+
"Unknown button state: 0x33"
423+
);
424+
}
425+
} else {
426+
assert!(false, "expected an error");
427+
}
372428
}
373429

374430
#[test]

0 commit comments

Comments
 (0)