Comments (5)
Some examples in the documentation on how to parse common strings would be really helpful!
For example for fixed size strings with \0 padding, cstrings, etc.
I'm currently stuck at parsing this struct where each field has a fixed size and is filled with \0 until the end of its range:
#[derive(Debug, DekuRead, DekuWrite)]
struct RawRecord {
#[deku(count = "40")]
firstname: Vec<u8>,
#[deku(count = "40")]
lastname: Vec<u8>,
#[deku(count = "240")]
address: Vec<u8>,
#[deku(endian = "little")]
age: u32,
}
Help is very much appreciated! :) Thanks in advance!
from deku.
I've held off on implementing anything for String in favor of using a custom reader/writer or map, as this is more specific to the implementation of the protocol being serialized/deserialized. I.e. len (u8?, u16?, u64?) + value, null terminated, etc.
from deku.
This is a pretty common use of CString and isn't documented in deku
, probably should add that.
Anyway, this should work for you:
#[derive(Debug, DekuRead, DekuWrite)]
struct RawRecord {
#[deku(pad_bytes_after = "4 - firstname.to_bytes_with_nul().len()")]
firstname: CString,
#[deku(pad_bytes_after = "5 - lastname.to_bytes_with_nul().len()")]
lastname: CString,
}
from deku.
Thanks a lot! Very much appreciated!
from deku.
If I use this solution and Provide a string which is longer than or equal to the given pad bytes an "attempt to subtract with overflow" panikc occurs on trying to convert the Struct with to_bytes
.
In Release Mode this doesn't happen, but the bytes are just truncated.
Shouldn't this give a DekuError?
I guess there needs to be a check that pad_bytes_after cannot be zero.
from deku.
Related Issues (20)
- Cant "count" from Some(field) directly - need as_ref().unwrap() HOT 1
- Passing a struct in ctx creates opaque error HOT 7
- Doc request: update for structs
- How to access enum-struct members? HOT 4
- Assert for tuple enum HOT 1
- Working with elements of Vec<StructUsingDeku> to get sum of their byte-sizes HOT 4
- Weird parsing with `bool` and non-aligned data. HOT 4
- Is there a way to pad by a variable number of bytes? HOT 3
- Roll a release? HOT 12
- Performance of `read_all` and `count` HOT 2
- Ability to ignore excess bytes HOT 1
- endian attribute breaks enum HOT 2
- Comply to `cond` attribute while encoding HOT 6
- Support unit structure `struct S;` in derive-macro `DekuWrite` HOT 1
- Reading from a tokio stream
- Support for fallible use cases for mutating and serializing fields HOT 2
- Add `to_slice` for buffer reuse `DekuContainerWrite` trait HOT 3
- With no rest and input anymore: how to peek into the Reader? Or get total of whole input? HOT 1
- Is there a faster way to read large Vec<u8>? HOT 5
- Add support for error_in_core in a no_std environment HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deku.