2 releases
new 0.1.1 | Feb 16, 2025 |
---|---|
0.1.0 | Feb 15, 2025 |
#1048 in Parser implementations
182 downloads per month
15KB
299 lines
axumr-cbor
axum-cbor
is a Rust library designed for sending and receiving CBOR (Concise
Binary Object Representation) data via the
Axum web framework. This crate
provides a seamless way to handle CBOR data in your web applications, allowing
you to easily serialize and deserialize data in a compact binary format.
lib.rs
:
Library for sending and receiving cbox data via axum. Shamelessly copied from JSON part Cbor Extractor / Response.
When used as an extractor, it can deserialize request bodies into some type that
implements serde::Deserialize
. The request will be rejected (and a CborRejection
will
be returned) if:
- The request doesn't have a
content-type: application/cbor
(or similar) header. - The body doesn't contain syntactically valid Cbor.
- The body contains syntactically valid Cbor but it couldn't be deserialized into the target type.
- Buffering the request body fails.
⚠️ Since parsing Cbor requires consuming the request body, the Cbor
extractor must be
last if there are multiple extractors in a handler.
Extractor example
use axum::{
extract,
routing::post,
Router,
};
use serde::Deserialize;
use axum_cbor::Cbor;
#[derive(Deserialize)]
struct CreateUser {
email: String,
password: String,
}
async fn create_user(Cbor(payload): Cbor<CreateUser>) {
// payload is a `CreateUser`
}
let app = Router::<()>::new().route("/users", post(create_user));
When used as a response, it can serialize any type that implements serde::Serialize
to
Cbor
, and will automatically set content-type: application/cbor
.
Response example
use axum::{
extract::Path,
routing::get,
Router,
};
use serde::Serialize;
use axum_cbor::Cbor;
#[derive(Serialize)]
struct User {
id: String,
username: String,
}
async fn get_user(Path(user_id) : Path<String>) -> Cbor<User> {
let user = find_user(user_id).await;
Cbor(user)
}
async fn find_user(user_id: String) -> User {
// ...
# unimplemented!()
}
let app = Router::<()>::new().route("/users/:id", get(get_user));
Dependencies
~5–15MB
~191K SLoC