Skip to main content

rich_micro/
error.rs

1//! Errors.
2
3use std::fmt;
4
5/// Why an asset, a package or a lookup was rejected. Messages name the
6/// offending value and, where it helps, what would be accepted.
7#[derive(Clone, Debug, PartialEq, Eq)]
8pub enum MicroError {
9    /// A name outside `[a-z0-9_-]` segments joined by `/`.
10    InvalidName(String),
11    /// A cell size that is not allowed (zero, more than 4 columns, or more
12    /// than 2 rows).
13    InvalidSize(String),
14    /// Alt text is mandatory and must be printable.
15    InvalidAlt(String),
16    /// A fallback that does not fit the asset's cells, or holds whitespace or
17    /// control characters.
18    InvalidFallback(String),
19    /// A malformed or unsupported manifest or pack file.
20    Manifest(String),
21    /// A package that breaks a hard limit (sizes, dimensions, frames).
22    Limit(String),
23    /// A path that escapes the package: absolute, `..`, or a link out of it.
24    UnsafePath(String),
25    /// An image whose header could not be read or whose format is not allowed.
26    Image(String),
27    /// No asset of this name in any layer.
28    UnknownAsset(String),
29    /// An I/O or archive error, with the path it happened on.
30    Io(String),
31}
32
33impl fmt::Display for MicroError {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        match self {
36            MicroError::InvalidName(name) => write!(
37                f,
38                "invalid micro asset name {name:?}: use lowercase letters, digits, `_` and `-`, \
39                 with `/` between namespaces"
40            ),
41            MicroError::InvalidSize(message)
42            | MicroError::InvalidAlt(message)
43            | MicroError::InvalidFallback(message)
44            | MicroError::Manifest(message)
45            | MicroError::Limit(message)
46            | MicroError::UnsafePath(message)
47            | MicroError::Image(message)
48            | MicroError::Io(message) => f.write_str(message),
49            MicroError::UnknownAsset(name) => write!(f, "unknown micro asset {name:?}"),
50        }
51    }
52}
53
54impl std::error::Error for MicroError {}