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