Axum integration for Facet - extractors and responses using Facet's serialization.
This crate provides Axum extractors and response types that use Facet's serialization instead of serde. This allows you to use Facet-derived types directly in your Axum handlers without needing serde derives.
use axum::{routing::{get, post}, Router};
use facet::Facet;
use facet_axum::{Json, Query};
#[derive(Debug, Facet)]
struct CreateUser {
name: String,
email: String,
}
#[derive(Debug, Facet)]
struct User {
id: u64,
name: String,
email: String,
}
#[derive(Debug, Facet)]
struct SearchParams {
q: String,
page: u64,
}
async fn create_user(Json(payload): Json<CreateUser>) -> Json<User> {
Json(User {
id: 1,
name: payload.name,
email: payload.email,
})
}
async fn search(Query(params): Query<SearchParams>) -> String {
format!("Searching for '{}' on page {}", params.q, params.page)
}
let app = Router::new()
.route("/users", post(create_user))
.route("/search", get(search));json(default): EnablesJson<T>extractor/response usingfacet-jsonform(default): EnablesForm<T>andQuery<T>extractors usingfacet-urlencodedyaml: EnablesYaml<T>extractor/response usingfacet-yamltoml: EnablesToml<T>extractor/response usingfacet-tomlxml: EnablesXml<T>extractor/response usingfacet-xmlmsgpack: EnablesMsgPack<T>extractor/response usingfacet-msgpackpostcard: EnablesPostcard<T>extractor/response usingfacet-postcardall: Enables all format features
Thanks to all individual sponsors:
...along with corporate sponsors:
...without whom this work could not exist.
The facet logo was drawn by Misiasart.
Licensed under either of:
- Apache License, Version 2.0 (LICENSE-APACHE or http://www.apache.org/licenses/LICENSE-2.0)
- MIT license (LICENSE-MIT or http://opensource.org/licenses/MIT)
at your option.