Skip to main content

qubit_redact/formats/http/
body_capture_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 truncated HTTP body captures.
9
10use std::error::Error;
11use std::fmt;
12use std::fmt::Display;
13use std::fmt::Formatter;
14
15/// Reports inconsistent source-length metadata for a body capture.
16///
17/// # Examples
18///
19/// ```
20/// use qubit_redact::formats::http::BodyCapture;
21/// use qubit_redact::formats::http::BodyCaptureError;
22///
23/// let error = BodyCapture::truncated(b"prefix", 3)
24///     .expect_err("the claimed total must exceed captured bytes");
25/// assert!(matches!(error, BodyCaptureError::InvalidTotalLength { .. }));
26/// ```
27#[derive(Debug, Clone, Copy, PartialEq, Eq)]
28pub enum BodyCaptureError {
29    /// A truncated capture claimed a total no larger than captured bytes.
30    InvalidTotalLength {
31        /// Number of bytes present in the capture.
32        captured: usize,
33        /// Rejected claimed total source length.
34        total: usize,
35    },
36}
37
38impl Display for BodyCaptureError {
39    /// Writes a concise description of the invalid capture metadata.
40    ///
41    /// # Parameters
42    ///
43    /// * `formatter` - Destination formatting context.
44    ///
45    /// # Returns
46    ///
47    /// The formatter result from writing the error description.
48    ///
49    /// # Errors
50    ///
51    /// Returns [`fmt::Error`] when the destination rejects a write.
52    #[inline]
53    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
54        match self {
55            Self::InvalidTotalLength { captured, total } => write!(
56                formatter,
57                "truncated body total length {total} must exceed {captured} captured bytes",
58            ),
59        }
60    }
61}
62
63impl Error for BodyCaptureError {}