Crate deku_string

Source
Expand description

Simple wrapper around String to implement DekuReader and DekuWriter

This implementation requires following context:

  • Endian — little or big
  • Encoding — UTF-8 or UTF-16
  • StringLayout — how string is represented in binary

Pick your favorite combination

Example usage od StringDeku with UTF-8 strings:

use ::deku_string::{StringDeku, Encoding, StringLayout, Size};

#[derive(Debug, Clone, PartialEq, ::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, StringLayout::fixed_length(10)")]
    fixed_value1: StringDeku,

    // fixed length buffer, null byte is allowed to be inside,
    // both "012345678\x00" and "0123456789" are allowed
    #[deku(ctx = "Encoding::Utf8, StringLayout::FixedLength{size: 10, allow_no_null: true}")]
    fixed_value2: StringDeku,

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

    // length (2 byte) then string, null character is NOT allowed inside
    // b"\0x5\x0001234"
    #[deku(ctx = "Encoding::Utf8, StringLayout::LengthPrefix(Size::U16)")]
    prefixed_u16: StringDeku,

    // length (4 byte) then string, null character is NOT allowed inside
    // b"\0x5\x00\x00\x0001234"
    #[deku(ctx = "Encoding::Utf8, StringLayout::LengthPrefix(Size::U32)")]
    prefixed_u32: StringDeku,

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

Structs§

StringDeku
Simple wrapper around String to read and write various layouts. This looks as a simple wrapper over String, StringDeku structure is built to be able of reading and writing of various common binary layouts.

Enums§

Encoding
Encoding to use for read and write
Size
Length prefix size
StringLayout
String variant to read and write