[][src]Derive Macro kf_protocol_derive::Decode

#[derive(Decode)]
{
    // Attributes available to this derive:
    #[varint]
    #[fluvio_kf]
}

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

Examples

use kf_protocol::Decoder;
use kf_protocol::derive::Decode;

#[derive(Decode)]
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);

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

So this works

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

Also, enum without integer literal works as well

#[derive(Decode)]
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.

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

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