use axum::http::Request;

/// Pass to axum::middleware::map_request() to transform the entire URI path
/// (but not search query) to lowercase.
pub async fn lowercase_uri_path<B>(mut request: Request<B>) -> Request<B> {
    let path = request.uri().path().to_lowercase();
    let path_and_query = match request.uri().query() {
        Some(query) => format!("{}?{}", path, query),
        None => path,
    };
    let builder =
        axum::http::uri::Builder::from(request.uri().clone()).path_and_query(path_and_query);
    *request.uri_mut() = builder
        .build()
        .expect("lowercasing URI path should not break it");
    request
}
