Skip to content

Commit 53187a6

Browse files
authored
Merge pull request #38 from rursprung/prepare-v0.3.0
prepare v0.3.0: change API to Iterator, implement `core::error::Error` & add some `Copy`, `Clone`, `Hash` where applicable
2 parents 5856dca + 6240970 commit 53187a6

15 files changed

+214
-150
lines changed

.github/workflows/CI.yml

+20-11
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,8 @@ jobs:
1313
strategy:
1414
fail-fast: false
1515
matrix:
16-
rust: [1.62.0, stable]
17-
features: ['use_alloc', 'use_alloc,defmt', 'use_heapless', 'use_heapless,defmt']
18-
exclude:
19-
- rust: 1.62.0
20-
features: 'use_alloc,defmt'
21-
- rust: 1.62.0
22-
features: 'use_heapless,defmt'
16+
rust: [1.81.0, stable]
17+
features: ['']
2318
runs-on: ubuntu-latest
2419
steps:
2520
- uses: actions/checkout@v4
@@ -31,17 +26,17 @@ jobs:
3126
- name: Install required cargo components
3227
run: cargo +stable install clippy-sarif sarif-fmt
3328
- name: build
34-
run: cargo build --features ${{ matrix.features }}
29+
run: cargo build ${{ matrix.features }}
3530
- name: check
36-
run: cargo check --features ${{ matrix.features }}
31+
run: cargo check ${{ matrix.features }}
3732
- name: test
38-
run: cargo test --features ${{ matrix.features }}
33+
run: cargo test ${{ matrix.features }}
3934
- name: check formatting
4035
run: cargo fmt --all -- --check
4136
- name: audit
4237
run: cargo audit
4338
- name: clippy (lib)
44-
run: cargo clippy --features ${{ matrix.features }} --message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
39+
run: cargo clippy ${{ matrix.features }} --message-format=json | clippy-sarif | tee rust-clippy-results.sarif | sarif-fmt
4540
continue-on-error: true
4641
- name: Upload analysis results to GitHub
4742
uses: github/codeql-action/upload-sarif@v3
@@ -87,3 +82,17 @@ jobs:
8782
with:
8883
sarif_file: examples/stm32f4-event-printer/rust-clippy-results.sarif
8984
wait-for-processing: true
85+
86+
# simplify GH settings: have one single build to be required
87+
build-results:
88+
name: Final Results
89+
if: ${{ always() }}
90+
runs-on: ubuntu-latest
91+
needs: [lib, stm32f4-event-printer]
92+
steps:
93+
- name: check for failed builds of the library
94+
if: ${{ needs.lib.result != 'success' }}
95+
run: exit 1
96+
- name: check for failed builds of the example
97+
if: ${{ needs.stm32f4-event-printer.result != 'success' }}
98+
run: exit 1

CHANGELOG.md

+6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
66

77
<!-- next-header -->
88
## [Unreleased] - ReleaseDate
9+
### Added
10+
* `Copy`, `Clone` and `Hash` on error & event types (where possible)
11+
### Changed
12+
* The MSRV has been updated to 1.81.0 due to `core::error::Error` being implemented
13+
* **BREAKING**: The `parse` API has been replaced with `Parser::new` where `Parser` now implements `Iterator` and the `next` function returns each parsed command
14+
* Accordingly, the features `use_alloc` and `use_heapless` have been removed.
915

1016
## [0.2.0] - 2023-11-14
1117
### Added

Cargo.toml

+2-6
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"
@@ -12,18 +12,14 @@ license = "MIT OR Apache-2.0"
1212

1313
[dependencies]
1414
defmt = { version = "0.3", optional = true }
15-
heapless = { version = "0.8", optional = true }
1615

1716
rgb = { version = "0.8", optional = true }
1817
serde = { version = "1.0", features = ["derive"], optional = true }
1918

2019
[features]
2120
default = ["accelerometer_event", "button_event", "color_event", "gyro_event", "location_event", "magnetometer_event", "quaternion_event"]
2221

23-
use_heapless = ["dep:heapless"]
24-
use_alloc = []
25-
26-
defmt = ["dep:defmt", "heapless?/defmt-03"]
22+
defmt = ["dep:defmt"]
2723

2824
accelerometer_event = []
2925
button_event = []

README.md

+5-7
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,6 @@ which is e.g. used by the [Adafruit Bluefruit LE UART Friend](https://learn.adaf
99

1010
Note that this work is not affiliated with Adafruit.
1111

12-
## Mandatory Features
13-
This crate is `no_std` and you can choose whether you want to use
14-
[`heapless::Vec`](https://docs.rs/heapless/0.8.0/heapless/struct.Vec.html) by selecting the feature `use_heapless` or
15-
[`alloc::vec::Vec`](https://doc.rust-lang.org/alloc/vec/struct.Vec.html) by selecting the feature `use_alloc`.
16-
If you select neither or both you'll get a compile error.
17-
1812
## Optional Features
1913
* `defmt`: you can enable the [`defmt`](https://defmt.ferrous-systems.com/) feature to get a `defmt::Format` implementation for all structs & enums and a `defmt::debug!` call for each command being parsed.
2014
* `rgb`: if enabled, `From<ColorEvent> for RGB8` is implemented to support the [RGB crate](https://crates.io/crates/rgb).
@@ -23,12 +17,16 @@ If you select neither or both you'll get a compile error.
2317
but you can opt to only select the event(s) you are interested in which will result in a small binary size.
2418
If other events are received, a `ProtocolParseError::DisabledControllerDataPackageType` will be returned.
2519

20+
## Usage
21+
The entry point to use this crate is `Parser` which implements `Iterator` to access the events in the input.
22+
Note that this is a [sans I/O](https://sans-io.readthedocs.io/) crate, i.e. you have to talk to the Adafruit device, the parser just expects a byte sequence.
23+
2624
## Examples
2725
A simple example for the STM32F4 microcontrollers is [available](examples/stm32f4-event-printer/README.md).
2826

2927
## Changelog
3028
For the changelog please see the dedicated [CHANGELOG.md](CHANGELOG.md).
3129

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

examples/stm32f4-event-printer/Cargo.lock

+2-23
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/stm32f4-event-printer/Cargo.toml

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ defmt = "0.3.6"
1717
defmt-rtt = "0.4"
1818

1919
# use `adafruit-bluefruit-protocol = "0.1"` in reality; path used here to ensure that the example always compiles against the latest master
20-
adafruit-bluefruit-protocol = { path = "../..", features = ["defmt", "use_heapless"] }
20+
adafruit-bluefruit-protocol = { path = "../..", features = ["defmt"] }
2121

2222
[profile.release]
2323
codegen-units = 1

examples/stm32f4-event-printer/src/adafruit_bluefruit_le_uart_friend.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,10 @@ impl BluefruitLEUARTFriend {
8484
filled_buffer
8585
);
8686

87-
let event = adafruit_bluefruit_protocol::parse::<4>(filled_buffer);
88-
defmt::info!("received event(s) over bluetooth: {}", &event);
87+
let parser = adafruit_bluefruit_protocol::Parser::new(filled_buffer);
88+
for event in parser {
89+
defmt::info!("received event over bluetooth: {:?}", &event);
90+
}
8991

9092
// switch out the buffers
9193
filled_buffer.fill(0);

src/accelerometer_event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::{try_f32_from_le_bytes, ProtocolParseError};
44

55
/// Represents an accelerometer event from the protocol.
6-
#[derive(PartialEq, Debug)]
6+
#[derive(PartialEq, Debug, Copy, Clone)]
77
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
88
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
99
#[allow(missing_docs)] // the names are already obvious enough

src/button_event.rs

+18-4
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
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.
6-
#[derive(PartialEq, Eq, Debug)]
8+
#[derive(PartialEq, Eq, Debug, Hash, Clone, Copy)]
79
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
810
pub enum ButtonParseError {
911
/// The message contained an unknown button. For the known buttons see [`Button`].
@@ -12,8 +14,20 @@ 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.
16-
#[derive(PartialEq, Eq, Debug)]
30+
#[derive(PartialEq, Eq, Debug, Copy, Clone, Hash)]
1731
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1832
#[allow(missing_docs)] // the names are already obvious enough
1933
pub enum Button {
@@ -45,7 +59,7 @@ impl Button {
4559
}
4660

4761
/// The state of the button.
48-
#[derive(PartialEq, Eq, Debug)]
62+
#[derive(PartialEq, Eq, Debug, Copy, Clone, Hash)]
4963
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
5064
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
5165
#[allow(missing_docs)] // the names are already obvious enough
@@ -66,7 +80,7 @@ impl ButtonState {
6680
}
6781

6882
/// Represents a button event from the protocol.
69-
#[derive(PartialEq, Eq, Debug)]
83+
#[derive(PartialEq, Eq, Debug, Copy, Clone, Hash)]
7084
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
7185
#[allow(missing_docs)] // the names are already obvious enough
7286
pub struct ButtonEvent {

src/color_event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::ProtocolParseError;
55
use rgb::RGB8;
66

77
/// Represents a color event from the protocol.
8-
#[derive(PartialEq, Eq, Debug)]
8+
#[derive(PartialEq, Eq, Debug, Copy, Clone, Hash)]
99
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
1010
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
1111
#[allow(missing_docs)] // the names are already obvious enough

src/gyro_event.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
use super::{try_f32_from_le_bytes, ProtocolParseError};
44

55
/// Represents a gyro event from the protocol.
6-
#[derive(PartialEq, Debug)]
6+
#[derive(PartialEq, Debug, Copy, Clone)]
77
#[cfg_attr(feature = "defmt", derive(defmt::Format))]
88
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
99
#[allow(missing_docs)] // the names are already obvious enough

0 commit comments

Comments
 (0)