Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/adinack/pattern
A compact pattern recognition and extraction system.
https://github.com/adinack/pattern
deserialization embedded-rust no-std
Last synced: about 20 hours ago
JSON representation
A compact pattern recognition and extraction system.
- Host: GitHub
- URL: https://github.com/adinack/pattern
- Owner: AdinAck
- Created: 2023-11-13T00:46:32.000Z (about 1 year ago)
- Default Branch: main
- Last Pushed: 2023-12-13T20:35:34.000Z (about 1 year ago)
- Last Synced: 2024-11-08T21:52:42.269Z (about 2 months ago)
- Topics: deserialization, embedded-rust, no-std
- Language: Rust
- Homepage:
- Size: 9.77 KB
- Stars: 0
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# pattern
A compact pattern recognition and extraction system.
## no_std
This crate is intended for use in `no_std` environments.
# Usage
Given an iterator of some serialized data, values of various types can be extracted in sequence.
```rust
fn get_from_data(data: &mut &[u8]) -> Result<(u8, u16, u16, u32), PatternError> {
let mut pattern = Pattern::new(data.iter());let [start] = pattern
.values([0u8, 1u8]) // look for these exact values in order
.deferred() // eat bytes until these values are found
.extract()?; // attempt extractionlet [a] = pattern
.get() // deserialize 1 expected type from u8's (.get() only supports u8's)
.extract()?;let [b, c] = pattern
.get() // deserialize 2 of the expected type (u16)
.extract_and(|bytes| { /* do something with the bytes */ })?; // attempt extraction then call closure on bytes extractedlet [d] = pattern
.get() // deserialize 1 expected type (u32)
.extract()?;Ok((a, b, c, d))
}
```# Dependencies
This crate uses the [tiny-serde](https://github.com/AdinAck/tiny-serde) crate for deserialization.
[defmt](https://github.com/knurling-rs/defmt) is available behind a feature gate for formatting `PatternError`.
# Design Considerations
## Safety
This crate *does* use `unsafe` blocks but has been proven to never exhibit undefined behavior.