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#[derive(Debug, Clone, Copy, PartialEq, Eq)]
17pub enum BodyCaptureError {
18 /// A truncated capture claimed a total no larger than captured bytes.
19 InvalidTotalLength {
20 /// Number of bytes present in the capture.
21 captured: usize,
22 /// Rejected claimed total source length.
23 total: usize,
24 },
25}
26
27impl Display for BodyCaptureError {
28 /// Writes a concise description of the invalid capture metadata.
29 ///
30 /// # Parameters
31 ///
32 /// * `formatter` - Destination formatting context.
33 ///
34 /// # Returns
35 ///
36 /// The formatter result from writing the error description.
37 ///
38 /// # Errors
39 ///
40 /// Returns [`fmt::Error`] when the destination rejects a write.
41 #[inline]
42 fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
43 match self {
44 Self::InvalidTotalLength { captured, total } => write!(
45 formatter,
46 "truncated body total length {total} must exceed {captured} captured bytes",
47 ),
48 }
49 }
50}
51
52impl Error for BodyCaptureError {}