Skip to content

Commit ca26f48

Browse files
committed
rework API to use implement Iterator
this completely revamps the API. the `parse` function is gone, instead you now construct a new `Parser` using `Parser::new` and then use the iterator API on it which will yield a `next` until the whole input has been parsed. due to this there's now no longer a need to return a list of values from the API as instead we return the individual events (or errors) step by step. thus the dependency on `alloc` and `heapless` has been removed. to get the same result as before (everything collected into a vector) the `collect` API of `Iterator` can be used by consumers.
1 parent 339f7e7 commit ca26f48

File tree

8 files changed

+122
-154
lines changed

8 files changed

+122
-154
lines changed

.github/workflows/CI.yml

+5-10
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,7 @@ jobs:
1414
fail-fast: false
1515
matrix:
1616
rust: [1.81.0, stable]
17-
features: ['alloc', 'alloc,defmt', 'heapless', 'heapless,defmt']
18-
exclude:
19-
- rust: 1.81.0
20-
features: 'alloc,defmt'
21-
- rust: 1.81.0
22-
features: 'heapless,defmt'
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

CHANGELOG.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1010
* `Copy`, `Clone` and `Hash` on error & event types (where possible)
1111
### Changed
1212
* The MSRV has been updated to 1.81.0 due to `core::error::Error` being implemented
13-
* **BREAKING**: the features `use_alloc` and `use_heapless` have been renamed to `alloc` and `heapless` respectively.
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.
1415

1516
## [0.2.0] - 2023-11-14
1617
### Added

Cargo.toml

+1-5
Original file line numberDiff line numberDiff line change
@@ -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-
heapless = ["dep:heapless"]
24-
alloc = []
25-
26-
defmt = ["dep:defmt", "heapless?/defmt-03"]
22+
defmt = ["dep:defmt"]
2723

2824
accelerometer_event = []
2925
button_event = []

README.md

+2-8
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 `heapless` or
15-
[`alloc::vec::Vec`](https://doc.rust-lang.org/alloc/vec/struct.Vec.html) by selecting the feature `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).
@@ -24,8 +18,8 @@ If you select neither or both you'll get a compile error.
2418
If other events are received, a `ProtocolParseError::DisabledControllerDataPackageType` will be returned.
2519

2620
## Usage
27-
The entry point to use this crate is the `parse` function. Note that this is a [sans I/O](https://sans-io.readthedocs.io/)
28-
crate, i.e. you have to talk to the Adafruit device, the `parse` function just expects a byte sequence.
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.
2923

3024
## Examples
3125
A simple example for the STM32F4 microcontrollers is [available](examples/stm32f4-event-printer/README.md).

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", "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);

0 commit comments

Comments
 (0)