Skip to content

Commit bc3fae2

Browse files
Merge pull request #51 from triblespace/codex/identify-next-steps-for-project-advancement
Add Python winnow parsing example
2 parents 73f3ea0 + 5808595 commit bc3fae2

File tree

4 files changed

+23
-2
lines changed

4 files changed

+23
-2
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
# Changelog
22

33
## Unreleased
4+
- added example combining Python bindings with winnow parsing
45
- added `ByteSource` support for `VecDeque<T>` when `zerocopy` is enabled and kept the deque as owner
56
- added `ByteSource` support for `Cow<'static, T>` where `T: AsRef<[u8]>`
67
- added `ByteArea` for staged file writes with `Section::freeze()` to return `Bytes`

INVENTORY.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
- None at the moment.
55

66
## Desired Functionality
7-
- Example demonstrating Python + winnow parsing.
8-
7+
- Python example using winnow::view to parse structured data.
8+
99
## Discovered Issues
1010
- None at the moment.

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,7 @@ needs these libraries installed; otherwise disable the feature during testing.
147147
- [`examples/try_unwrap_owner.rs`](examples/try_unwrap_owner.rs) – reclaim the owner when uniquely referenced
148148
- [`examples/pybytes.rs`](examples/pybytes.rs) – demonstrates the `pyo3` feature using `PyBytes`
149149
- [`examples/from_python.rs`](examples/from_python.rs) – wrap a Python `bytes` object into `Bytes`
150+
- [`examples/python_winnow.rs`](examples/python_winnow.rs) – parse Python bytes with winnow
150151

151152
## Comparison
152153

examples/python_winnow.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#![cfg(all(feature = "pyo3", feature = "winnow"))]
2+
3+
use anybytes::Bytes;
4+
use pyo3::{prelude::*, types::PyBytes};
5+
use winnow::{error::ContextError, stream::AsBytes, token::take, Parser};
6+
7+
fn main() -> PyResult<()> {
8+
Python::with_gil(|py| {
9+
let obj = PyBytes::new(py, b"abcde");
10+
let mut bytes = Bytes::from_source(obj);
11+
12+
// Take the first three bytes using a winnow parser
13+
let mut parser = take::<_, _, ContextError>(3usize);
14+
let prefix: Bytes = parser.parse_next(&mut bytes).expect("take");
15+
assert_eq!(prefix.as_bytes(), b"abc".as_ref());
16+
assert_eq!(bytes.as_bytes(), b"de".as_ref());
17+
Ok(())
18+
})
19+
}

0 commit comments

Comments
 (0)