Skip to main content

Codec

Derive Macro Codec 

Source
#[derive(Codec)]
{
    // Attributes available to this derive:
    #[codec]
}
Expand description

Automatically implement the Codec trait for arbitrary structs or enums.

§Structs

Structs are typically represented as maps.

#[derive(Codec, Debug, PartialEq)]
struct Thing {
    meow: String,
    woof: i32,
}
assert_eq!(
    Thing::codec_read(&json!({"meow": "!!!", "woof": 100})).unwrap(),
    Thing { meow: "!!!".into(), woof: 100 }
);

You can also use structs with a single item to pass-through to another type, like so:

#[derive(Codec)]
struct Thing(String);
#[derive(Codec)]
#[codec(transparent)] // this attribute is required since the field is named
struct SameThing {
    thing: Thing,
}

TODO: it’s 4am i’m tired i can finish writing this later

§Enums

When Codec is derived on an enum, reading will attempt each variant sequentially until one matches.

#[derive(Codec)]
enum Thing {
    A(i32),
    B(String),
}

You can easily represent enums that are serialized as strings with #[codec(key = “…”)].

#[derive(Codec)]
enum Thing {
    #[codec(key = "first")]
    A,
    #[codec(key = "second")]
    B,
}

On occasion, you may want keys in variants that are maps.

#[derive(Codec, Debug, PartialEq)]
#[codec(tag = "type")]
enum Thing {
    #[codec(key = "first")]
    A { value: i32 },
    #[codec(key = "second")]
    B { value: String },
}
assert_eq!(
    Thing::codec_read(&json!({"type": "first", "value": 1})).unwrap(),
    Thing::A { value:1 }
);

You may not want to manually write keys for every variant if their key should be obvious from the variant name. For this, there is #[codec(default_snake_case_key)]

#[derive(Codec, Debug, PartialEq)]
#[codec(tag = "type")]
#[codec(default_snake_case_key)]
enum Thing {
    A { value: i32 },
    B { value: String },
}

If the key should be read as a different static Codec, this is supported too. Your keys are validated against that const_from_str at build-time, by the way.

#![feature(const_trait_impl, const_cmp)]
use azalea_codec::types::ConstFromStr;
#[derive(Codec, Debug, PartialEq)]
#[codec(tag = "type", tag_as = Key)]
#[codec(default_snake_case_key)]
enum Thing {
    A { value: i32 },
    B { value: String },
}
#[derive(Codec, PartialEq)]
#[codec(default_snake_case_key)]
enum Key {
    First,
    Second,
}
const impl ConstFromStr for Key {
    fn const_from_str(s: &str) -> Option<Self> {
        if s == "a" { Some(Self::First) }
        else if s == "b" { Some(Self::Second) }
        else { None }
    }
}
assert_eq!(
    Thing::codec_read(&json!({"type": "first", "value": 1})).unwrap(),
    Thing::A { value: 1 }
);
assert_eq!(
    Thing::codec_read(&json!({"type": "second", "value": ""})).unwrap(),
    Thing::B { value: "".to_string() }
);

Sadly, implementing ConstFromStr requires enabling a Nightly-only feature: const_trait_impl.

Named variants are treated exactly the same as structs, and support all of their #[codec(...)] attributes.

#[derive(Codec)]
#[codec(tag = "id", default_snake_case_key)]
enum Thing {
    #[codec(allow_unknown_keys)]
    A { value: i32 },
    B {
        #[codec(lenient)]
        value: Option<String>,
    },
}