1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
//! Shared error type for oxideav.
use thiserror::Error;
pub type Result<T> = std::result::Result<T, Error>;
#[derive(Debug, Error)]
pub enum Error {
#[error("I/O error: {0}")]
Io(#[from] std::io::Error),
#[error("unsupported: {0}")]
Unsupported(String),
#[error("invalid data: {0}")]
InvalidData(String),
#[error("end of stream")]
Eof,
#[error("need more data")]
NeedMore,
#[error("format not found: {0}")]
FormatNotFound(String),
#[error("codec not found: {0}")]
CodecNotFound(String),
/// A decoder (or arena pool) refused to allocate or proceed because
/// doing so would exceed a configured [`DecoderLimits`](crate::DecoderLimits)
/// cap, or because a pool has no free slot. This is the canonical
/// "DoS protection fired" error — callers should treat it as a hard
/// rejection of the input or a transient backpressure signal, never
/// retry blindly.
#[error("resource exhausted: {0}")]
ResourceExhausted(String),
#[error("{0}")]
Other(String),
}
impl Error {
pub fn unsupported(msg: impl Into<String>) -> Self {
Self::Unsupported(msg.into())
}
pub fn invalid(msg: impl Into<String>) -> Self {
Self::InvalidData(msg.into())
}
pub fn other(msg: impl Into<String>) -> Self {
Self::Other(msg.into())
}
/// Construct a [`Error::ResourceExhausted`] with the given message.
/// Use this from any decoder that has just hit a `DecoderLimits` cap
/// or an arena-pool exhaustion.
pub fn resource_exhausted(msg: impl Into<String>) -> Self {
Self::ResourceExhausted(msg.into())
}
}