laxum

Crates.iolaxum
lib.rslaxum
version0.2.3
created_at2025-10-22 06:36:00.238528+00
updated_at2025-12-23 15:27:47.631139+00
descriptionMake working with Axum a little smoother.
homepagehttps://codeberg.org/d2718/laxum
repositoryhttps://codeberg.org/d2718/laxum/src/tag/0.2.0
max_upload_size
id1895100
size45,453
Dan (d2718)

documentation

README

Make Axum a little smoother

(c) d2718; MIT license

Motivation

I use Axum a great deal, both in my personal projects and at work. I have, as one does when one uses anything with frequency, identified a miscellaney of snags and irritations; this crate is an attempt to smooth and soothe those.

This is in no way meant to be a complaint about axum or a criticism of any of the Tokio team's grave contributions to the Rust ecosystem; this is simply an adapter for my habitual use cases which I have chosen to publish in case some part of it might also be of use to others.

Features

Ergonomic Early-Return Error Handling

The [LaxErr] provides a flexible error type which implements IntoResponse; a companion [Laxative<T>] trait enables returning error responses ergonomically from Result<T, LaxErr> with more control than a simple '?' provides.

use eyre::{OptionExt, WrapErr};
use http::status::StatusCode;
use laxum::{GetStrHeaders, Laxative};

let auth_key = request.get_str("authorizaiton")
    .ok_or_eyre("request requires Authorization: header")
    .with_status(StatusCode::BAD_REQUEST)?;

Ergonomic Header Extraction

use axum::response::{IntoResponse, Response};
use laxum::{GetHeaders LaxErr, Laxative};

async fn handler(req: Request) -> Result<Response, LaxErr> {
    let action = request.require_str("x-action")?;

    // do something with `action` here...
}

If the request lacks a comprehensible x-action header, they'll get a 400 response with the message "Request requires 'x-action' header."

A Layer to add Multiple Headers

tower-http has SetResponseHeaderLayer, but that will only add one header per layer; the laxum::AddHeadersLayer can add an arbitrary number of them.

use axum::routing::Router;
use http::header::{self, HeaderValue};
use laxum::AddHeadersLayer;

let add_headers = AddHeadersLayer::build()
    .with(header::SERVER, HeaderValue::from_static("something-cool v0.2.14"))
    .with("x-clacks-overhead", HeaderValue::from_static("GNU Terry Pratchett, Jordan Dudash"))
    .to_layer();

let router = Router::new()
    // A bunch of routes go here...
    .layer(add_headers);
Commit count: 0

cargo fmt