use crate::error::ErrorResponse;
use axum::{
    body::HttpBody,
    extract::{Request, State},
    middleware::Next,
    response::{IntoResponse, Response},
};

pub async fn enforce_body_limit(State(limit): State<usize>, req: Request, next: Next) -> Response {
    if req
        .body()
        .size_hint()
        .upper()
        .is_some_and(|size| size > limit as u64)
    {
        return ErrorResponse::payload_too_large("request body too large").into_response();
    }
    next.run(req).await
}
