Git Product home page Git Product logo

Comments (5)

knolljo avatar knolljo commented on August 20, 2024 1

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.

sharksforarms avatar sharksforarms commented on August 20, 2024

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.

wcampbell0x2a avatar wcampbell0x2a commented on August 20, 2024

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.

knolljo avatar knolljo commented on August 20, 2024

Thanks a lot! Very much appreciated!

from deku.

knolljo avatar knolljo commented on August 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.