Skip to main content

wafrift_encoding/
error.rs

1//! Error types for wafrift-encoding.
2
3/// Errors that can occur during encoding.
4#[derive(Debug, Clone, PartialEq, Eq)]
5pub enum EncodeError {
6    /// The input payload exceeds the maximum allowed size.
7    PayloadTooLarge { max: usize, actual: usize },
8    /// The accumulated layered output exceeds the maximum allowed size.
9    LayeredOutputTooLarge { max: usize, actual: usize },
10    /// The payload contains invalid UTF-8 where valid UTF-8 is required.
11    InvalidUtf8,
12    /// The requested strategy is not applicable in the given context.
13    InvalidContext {
14        strategy: &'static str,
15        context: String,
16    },
17    /// An internal configuration or I/O error occurred.
18    InvalidConfig(String),
19}
20
21impl std::fmt::Display for EncodeError {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        match self {
24            Self::PayloadTooLarge { max, actual } => {
25                write!(f, "payload too large: {actual} bytes (max {max} bytes)")
26            }
27            Self::LayeredOutputTooLarge { max, actual } => {
28                write!(
29                    f,
30                    "layered output too large: {actual} bytes (max {max} bytes)"
31                )
32            }
33            Self::InvalidUtf8 => f.write_str("payload contains invalid UTF-8"),
34            Self::InvalidContext { strategy, context } => {
35                write!(f, "strategy {strategy} is not valid in context {context}")
36            }
37            Self::InvalidConfig(msg) => write!(f, "invalid config: {msg}"),
38        }
39    }
40}
41
42impl std::error::Error for EncodeError {}