axum_cbor/
lib.rs

1#![warn(clippy::all, clippy::pedantic)]
2//! Library for sending and receiving cbox data via axum.
3//! Shamelessly copied from JSON part
4//! Cbor Extractor / Response.
5//!
6//! When used as an extractor, it can deserialize request bodies into some type that
7//! implements [`serde::Deserialize`]. The request will be rejected (and a [`CborRejection`] will
8//! be returned) if:
9//!
10//! - The request doesn't have a `content-type: application/cbor` (or similar) header.
11//! - The body doesn't contain syntactically valid Cbor.
12//! - The body contains syntactically valid Cbor but it couldn't be deserialized into the target
13//!   type.
14//! - Buffering the request body fails.
15//!
16//! ⚠️ Since parsing Cbor requires consuming the request body, the `Cbor` extractor must be
17//! *last* if there are multiple extractors in a handler.
18//!
19//! # Extractor example
20//!
21//! ```rust,no_run
22//! use axum::{
23//!     extract,
24//!     routing::post,
25//!     Router,
26//! };
27//! use serde::Deserialize;
28//! use axum_cbor::Cbor;
29//!
30//! #[derive(Deserialize)]
31//! struct CreateUser {
32//!     email: String,
33//!     password: String,
34//! }
35//!
36//! async fn create_user(Cbor(payload): Cbor<CreateUser>) {
37//!     // payload is a `CreateUser`
38//! }
39//!
40//! let app =  Router::<()>::new().route("/users", post(create_user));
41//! ```
42//!
43//! When used as a response, it can serialize any type that implements [`serde::Serialize`] to
44//! `Cbor`, and will automatically set `content-type: application/cbor`.
45//!
46//! # Response example
47//!
48//! ```rust,no_run
49//! use axum::{
50//!     extract::Path,
51//!     routing::get,
52//!     Router,
53//! };
54//! use serde::Serialize;
55//! use axum_cbor::Cbor;
56//!
57//! #[derive(Serialize)]
58//! struct User {
59//!     id: String,
60//!     username: String,
61//! }
62//!
63//! async fn get_user(Path(user_id) : Path<String>) -> Cbor<User> {
64//!     let user = find_user(user_id).await;
65//!     Cbor(user)
66//! }
67//!
68//! async fn find_user(user_id: String) -> User {
69//!     // ...
70//!     # unimplemented!()
71//! }
72//!
73//! let app = Router::<()>::new().route("/users/:id", get(get_user));
74//! ```
75
76mod cbor;
77mod cbor_rejecton;
78mod errors;
79#[cfg(any(feature = "axum_test", test))]
80mod request_builder;
81#[cfg(test)]
82mod test;
83
84pub use crate::cbor::Cbor;
85pub use cbor_rejecton::CborRejection;
86pub use errors::*;
87#[cfg(any(feature = "axum_test", test))]
88pub use request_builder::{CborReq, CborResp};