Skip to main content

qubit_redact/http/
body_budget_error.rs

1// =============================================================================
2//    Copyright (c) 2025 - 2026 Haixing Hu.
3//
4//    SPDX-License-Identifier: Apache-2.0
5//
6//    Licensed under the Apache License, Version 2.0.
7// =============================================================================
8//! Validation errors for HTTP body budgets.
9
10use std::{
11    error::Error,
12    fmt::{
13        self,
14        Display,
15        Formatter,
16    },
17};
18
19/// Reports which hard body-budget invariant was violated.
20#[derive(Debug, Clone, Copy, PartialEq, Eq)]
21pub enum BodyBudgetError {
22    /// The input byte limit was zero.
23    ZeroInput,
24    /// The output limit cannot contain the complete truncation marker.
25    OutputTooSmall {
26        /// Smallest accepted output limit in bytes.
27        minimum: usize,
28        /// Rejected output limit in bytes.
29        actual: usize,
30    },
31}
32
33impl Display for BodyBudgetError {
34    /// Writes a concise description of the violated budget invariant.
35    ///
36    /// # Parameters
37    ///
38    /// * `formatter` - Destination formatting context.
39    ///
40    /// # Returns
41    ///
42    /// The formatter result from writing the error description.
43    ///
44    /// # Errors
45    ///
46    /// Returns [`fmt::Error`] when the destination rejects a write.
47    #[inline]
48    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
49        match self {
50            Self::ZeroInput => formatter
51                .write_str("body input budget must be greater than zero"),
52            Self::OutputTooSmall { minimum, actual } => write!(
53                formatter,
54                "body output budget must be at least {minimum} bytes, got {actual}",
55            ),
56        }
57    }
58}
59
60impl Error for BodyBudgetError {}