Derive Macro fluvio_protocol::Decoder[][src]

#[derive(Decoder)]
{
    // Attributes available to this derive:
    #[varint]
    #[fluvio]
}
Expand description

Custom derive for decoding structure or enum from bytes using fluvio protocol format. This assumes all fields implement fluvio decode traits.

Examples

use std::io::Cursor;
use fluvio_protocol::Decoder;

#[derive(Default, Decoder)]
pub struct SimpleRecord {
    val: u8
}

let data = [
    0x04
];

let record = SimpleRecord::decode_from(&mut Cursor::new(&data),0).expect("decode");
assert_eq!(record.val, 4);

Decoder applies to either Struct of Enum. For enum, it implements TryFrom trait.
Currently it only supports integer variants.

So this works

#[derive(Decoder)]
pub enum ThreeChoice {
    First = 1,
    Second = 2,
    Third = 3
}

Also, enum without integer literal works as well

#[derive(Decoder)]
pub enum ThreeChoice {
    First,
    Second,
    Third
}

In this case, 1 is decoded as First, 2 as Second, 3 as Third.

Currently, mixing enum variants are not supported.

Decoder support container and field level attributes. Container level applies to struct. For field attributes

  • #[varint] force decode using varint format.
  • #fluvio(min_version = <version>)] decodes only if version is equal or greater than min_version
  • #fluvio(max_version = <version>)]decodes only if version is less or equal than max_version