#messagepack

axum-msgpack

serialize/derserialize msgpack for axum

5 releases (3 breaking)

0.4.0 Dec 31, 2023
0.3.0 Nov 28, 2022
0.2.2 Apr 16, 2022
0.2.1 Jan 21, 2022
0.1.0 Aug 28, 2021

#12 in #message-pack

Download history 27/week @ 2024-06-29 21/week @ 2024-07-06 11/week @ 2024-07-13 21/week @ 2024-07-20 51/week @ 2024-07-27 68/week @ 2024-08-03 9/week @ 2024-08-10 19/week @ 2024-08-17 17/week @ 2024-08-24 16/week @ 2024-08-31 8/week @ 2024-09-07 13/week @ 2024-09-14 49/week @ 2024-09-21 48/week @ 2024-09-28 27/week @ 2024-10-05 14/week @ 2024-10-12

141 downloads per month
Used in 4 crates

MIT license

22KB
430 lines

axum-msgpack

axum-msgpack adds MessagePack features to axum.

Documentation Crates.io

More information about this crate can be found in the crate documentation.

serde msgpack

Features

  • Serialize, Deserialize MessagePack from request/response

Usage example

use axum_msgpack::MsgPack;
use serde::{Deserialize, Serialize};

#[derive(Debug, Serialize, Deserialize)]
struct User {
    pub name: String,
    #[serde(with = "serde_bytes")]
    pub data: Vec<u8>
}

// axum handler | MsgPack
async fn get_handler() -> MsgPack<User> {
    let user = User {
        name: "steve".to_string(),
        data: vec![0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    };
    MsgPack(user)
}

// axum handler | MsgPack
async fn post_handler(MsgPack(user): MsgPack<User>) -> Html<String> {
    let string = format!("<h1>{:?}</h1>", user.name);
    Html(string)
}

// axum handler | MsgPackRaw
async fn get_handler_raw() -> MsgPackRaw<User> {
    let user = User {
        name: "steve".to_string(),
        data: vec![0,0,0,0,0,0,0,0,0,0,0,0,0,0,0]
    };
    MsgPackRaw(user)
}

// axum handler | MsgPackRaw
async fn post_handler_raw(MsgPackRaw(user): MsgPackRaw<User>) -> Html<String> {
    let string = format!("<h1>{:?}</h1>", user.name);
    Html(string)
}

Dependencies for serializing/deserializing MsgPack

serde = { version = "1.0", features = ["derive"] }
serde_bytes = "0.11"

In order to pack arrays correct remember to add #[serde(with = "serde_bytes")] to the struct member.

Safety

This crate uses #![forbid(unsafe_code)] to ensure everything is implemented in 100% safe Rust.

License

This project is licensed under the MIT license.

Dependencies

~6–13MB
~146K SLoC