What's the best practice to parse structure from AsyncRead?

I generally use chumsky to parse structure from a &[u8] or String.

But now I have to parse some structure (which can be defined by BNF) from AsyncRead, what's the best practice to do that?

1 Like

If you're using Tokio as your async runtime, look at tokio_util::codec - this is a set of traits and structs you can use to define how to go from AsyncRead to Stream<MyItem> (and from Sink<MyItem> to AsyncWrite).

You'd implement a Decoder that looks at bytes in front of you and determines if you have a complete structure, and if so, what does it decode to, and then use FramedRead to wrap your AsyncRead and get a Stream<_> from it.

In turn, the Decoder implementation probably uses a parser like chumsky, pest or nom to decode the bytes into objects - if you see a complete object, you can return it, while if there's not enough data, you can ask for more.

1 Like

To parse from an AsyncRead, you read into a Vec<u8>, then parse the data in the vector.

The codec stuff is a utility that helps with reading it into a byte array.

3 Likes

Alice chan daiski~ ^0^ thank you tokio

1 Like

This topic was automatically closed 90 days after the last reply. We invite you to open a new topic if you have further questions or comments.