Skip to main content

encode_path

Function encode_path 

Source
pub fn encode_path(path: &str) -> String
Expand description

A route path with its non-ASCII characters percent-encoded, so Unicode routes match (Rails’ Unicode routes).

Browsers send /über-uns as /%C3%BCber-uns, and axum matches the path as sent. Write the route in Unicode and wrap it: .route(&ocre::encode_path("/über-uns"), get(about)). Bytes outside printable ASCII (UTF-8 multibyte characters, spaces) become %XX with uppercase hex, as browsers send them; {id} captures and / are kept, and axum decodes captured values (Path<String>) back to Unicode. Pure CPU, once when the router is built.

§Examples

use axum::{Router, routing::get};

assert_eq!(ocre::encode_path("/über-uns"), "/%C3%BCber-uns");
assert_eq!(ocre::encode_path("/café/{id}"), "/caf%C3%A9/{id}");

let app: Router = Router::new().route(&ocre::encode_path("/über-uns"), get(|| async { "Über uns" }));