#binary-encoding #deku #string #serialization #no-std

no-std deku_string

String encoding/decoding helpers for Deku, supporting UTF-8/UTF-16 in fixed, Pascal, and C-style formats

1 unstable release

new 0.1.0 Apr 11, 2025

#2425 in Encoding

MIT license

21KB
375 lines

Easy out-of-the-box read and write strings in various formats using Deku

Crates.io Docs.rs

deku_string is an utility crate for Deku that provides convenient support for serializing and deserializing strings in a variety of binary formats. It handles both UTF-8 and UTF-16 encoded strings and is suitable for parsing various string layouts.

✨ Features

  • UTF-8 and UTF-16 support.
  • Multiple string encoding and layout formats.
  • Little and Big Endian support.
  • Dynamic read and write without additional temporary structs and operations.
  • No need to specify custom reader and writer.
  • Compatible with Deku's derive macros and custom readers/writers.

Supported Layout Formats

  • Fixed-length strings
    • With or without zero-termination
  • Pascal-style strings
    • Length-prefixed strings having length size u8, u16, or u32
  • C-style strings
    • Zero-terminated strings

📦 Usage

See full example in tests/deku_derive.rs

#[derive(Default, Debug, Clone, PartialEq, PartialOrd, ::deku::DekuRead, ::deku::DekuWrite)]
#[deku(endian = "little")]
struct SampleModel {
    // fixed length buffer, null  character is required to be inside
    // "012345678\x00" is allowed
    // "0123456789" is NOT allowed
    #[deku(ctx = "Encoding::Utf8, Layout::fixed_length(10)")]
    fixed_value: StringDeku,

    // length (1 byte) then string, null character is NOT allowed inside
    // b"\0x501234"
    #[deku(ctx = "Encoding::Utf8, Layout::LengthPrefix(Size::U8)")]
    prefixed_u8: StringDeku,

    // String with null byte at the end
    // b"012345\x00"
    #[deku(ctx = "Encoding::Utf8, Layout::ZeroEnded")]
    zero_ended: StringDeku,
}

🤝 Contributing

Contributions are warmly welcome!

Whether you're fixing a bug, improving performance, adding support for new string formats, or just refining docs — your pull requests and suggestions are greatly appreciated.

Dependencies

~1–1.6MB
~30K SLoC