Skip to main content

qubit_redact/text/
log_output_limit_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//! Error returned for an undersized bounded log-output limit.
9
10use std::{
11    error::Error,
12    fmt::{
13        self,
14        Display,
15        Formatter,
16    },
17};
18
19use crate::LogOutputLimit;
20
21/// Indicates that a byte budget cannot contain the truncation marker.
22#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq)]
23pub struct LogOutputLimitError {
24    /// Invalid requested byte count.
25    requested: usize,
26}
27
28impl LogOutputLimitError {
29    /// Creates an error for an invalid requested byte count.
30    ///
31    /// # Parameters
32    ///
33    /// * `requested` - Invalid maximum output byte count.
34    ///
35    /// # Returns
36    ///
37    /// An error retaining the invalid request.
38    #[inline(always)]
39    pub(crate) const fn new(requested: usize) -> Self {
40        Self { requested }
41    }
42
43    /// Returns the invalid requested byte count.
44    ///
45    /// # Returns
46    ///
47    /// The caller-provided byte count.
48    #[inline(always)]
49    pub const fn requested(self) -> usize {
50        self.requested
51    }
52
53    /// Returns the smallest valid byte count.
54    ///
55    /// # Returns
56    ///
57    /// The byte length required for the complete truncation marker.
58    #[inline(always)]
59    pub const fn minimum(self) -> usize {
60        LogOutputLimit::MINIMUM
61    }
62}
63
64impl Display for LogOutputLimitError {
65    /// Describes the invalid and minimum byte counts.
66    ///
67    /// # Parameters
68    ///
69    /// * `formatter` - Destination formatting context.
70    ///
71    /// # Returns
72    ///
73    /// The formatter result.
74    ///
75    /// # Errors
76    ///
77    /// Returns [`fmt::Error`] when the destination formatter rejects output.
78    fn fmt(&self, formatter: &mut Formatter<'_>) -> fmt::Result {
79        write!(
80            formatter,
81            "log output limit {} bytes is smaller than the minimum {} bytes",
82            self.requested,
83            LogOutputLimit::MINIMUM,
84        )
85    }
86}
87
88impl Error for LogOutputLimitError {}