#msgpack #schema #messagepack

msgpack-schema

A specification language for MessagePack data format

12 unstable releases (3 breaking)

0.4.1 Jul 29, 2021
0.4.0 Jul 19, 2021
0.3.1 Jun 21, 2021
0.2.1 Jun 14, 2021
0.1.6 Jun 12, 2021

#145 in Encoding

Download history 22/week @ 2021-06-03 132/week @ 2021-06-10 80/week @ 2021-06-17 37/week @ 2021-06-24 38/week @ 2021-07-01 69/week @ 2021-07-08 116/week @ 2021-07-15 69/week @ 2021-07-22 258/week @ 2021-07-29 62/week @ 2021-08-05 140/week @ 2021-08-12 63/week @ 2021-08-19 53/week @ 2021-08-26 161/week @ 2021-09-02 90/week @ 2021-09-09 50/week @ 2021-09-16

188 downloads per month

MIT license

105KB
2.5K SLoC

msgpack-schema Crates.io docs.rs CircleCI

msgpack-schema is a schema language for describing data formats encoded in MessagePack. It provides two derive macros Serialize and Deserialize that allow you to transcode MessagePack binary data to/from Rust data structures in a type-directed way.

use msgpack_schema::{Deserialize, Serialize};

#[derive(Deserialize, Serialize)]
struct Human {
    #[tag = 0]
    name: String,
    #[tag = 2]
    #[optional]
    age: Option<u32>,
}

Behaviours of serializers and deserializers

Some general rules

  • The deserializer ignores irrelevant key-value pairs in MsgPack map objects.
  • MsgPack map objects must not have duplicate keys.

Structs with named fields

Structs with named fields will be serialized into a MsgPack map object whose keys are fixints specified by #[tag] attributes.

schema Rust MessagePack
struct S {
    #[tag = 0]
    foo: u32,
    #[tag = 1]
    bar: String,
}
S { foo: 42, bar: "hello".to_owned() }
{ 0: 42, 1: "hello" }

Fields in named structs may be tagged with #[optional].

  • The tagged field must be of type Option<T>.
  • On serialization, the key-value pair will not be included in the result map object when the field data contains None.
  • On deserialization, the field of the result struct will be filled with None when the given MsgPack map object contains no corresponding key-value pair.

The #[flatten] attribute is used to factor out a single definition of named struct into multiple ones.

# use msgpack_schema::*;
#[derive(Serialize)]
struct S1 {
    #[tag = 1]
    x: u32,
}

#[derive(Serialize)]
struct S2 {
    #[flatten]
    s1: S1,
    #[tag = 2]
    y: u32,
}

#[derive(Serialize)]
struct S3 {
    #[tag = 1]
    x: u32,
    #[tag = 2]
    y: u32,
}

assert_eq!(serialize(S2 { s1: S1 { x: 42 }, y: 43, }), serialize(S3 { x: 42, y: 43 }));

Untagged structs with named fields

Structs with named fields may be attached #[untagged]. Untagged structs are serialized into an array and will not contain tags.

schema Rust MessagePack
#[untagged]
struct S {
    foo: u32,
    bar: String,
}
S { foo: 42, bar: "hello".to_owned() }
[ 42, "hello" ]

Newtype structs

Tuple structs with only one element are treated transparently.

schema Rust MessagePack
struct S(u32)
S(42)
42

Unit structs and empty tuple structs

Serialization and deserialization of unit structs and empty tuple structs are currently unsupported.

schema Rust MessagePack
struct S
S

UNSUPPORTED

struct S()
S()

UNSUPPORTED

Tuple structs

Tuple structs with more than one element are encoded as an array.

schema Rust MessagePack
struct S(u32, bool)
S(42, true)
[ 42, true ]

Unit variants and empty tuple variants

Unit variants and empty tuple variants are serialized into a single fixint whose value is determined by the tag.

schema Rust MessagePack
enum E {
    #[tag = 3]
    Foo
}
E::Foo
3
enum E {
    #[tag = 3]
    Foo()
}
E::Foo()
3

Newtype variants

Newtype variants (one-element tuple variants) are serialized into an array of the tag and the inner value.

schema Rust MessagePack
enum E {
    #[tag = 3]
    Foo(u32)
}
E::Foo(42)
[ 3, 42 ]

Untagged variants

Enums may be attached #[untagged] when all variants are newtype variants. Serializing untagged variants results in the same data layout as the inner type. The deserializer deserializes into an untagged enum type by trying deserization one by one from the first variant to the last.

schema Rust MessagePack
#[derive(Serialize, Deserialize)]
#[untagged]
enum Animal {
    Foo(String),
    Bar(u32),
}
E::Bar(42)
42

License

Licensed under MIT license.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in msgpack-schema by you shall be licensed as above, without any additional terms or conditions.

Dependencies

~0.6–1.1MB
~25K SLoC