Skip to main content

tsaur_core/
error.rs

1//! Error type and stable exit codes.
2
3#[derive(Debug, thiserror::Error)]
4pub enum Error {
5    #[error("I/O error: {0}")]
6    Io(#[from] std::io::Error),
7    #[error("not a T-saur archive (bad magic)")]
8    BadMagic,
9    #[error("unsupported format version {0}: this build reads version 1 only, a newer T-saur is needed")]
10    Version(u16),
11    #[error("archive is corrupt: {0}")]
12    Corrupt(String),
13    #[error("hash mismatch: {0}")]
14    HashMismatch(String),
15    #[error("resource limit exceeded: {0}")]
16    Limit(String),
17    #[error("refused by policy: {0}")]
18    Policy(String),
19    #[error("cryptography: {0}")]
20    Crypto(String),
21    #[error("missing data: {0}")]
22    Missing(String),
23    #[error("encoding error: {0}")]
24    Encoding(String),
25    #[error("invalid argument: {0}")]
26    Invalid(String),
27}
28
29pub type Result<T> = std::result::Result<T, Error>;
30
31impl Error {
32    /// Stable process exit codes used by the CLI:
33    /// 1 generic, 2 corrupt/integrity, 3 policy, 4 limits, 5 missing data, 6 crypto, 7 invalid argument.
34    pub fn exit_code(&self) -> i32 {
35        match self {
36            Error::BadMagic | Error::Version(_) | Error::Corrupt(_) | Error::HashMismatch(_) => 2,
37            Error::Policy(_) => 3,
38            Error::Limit(_) => 4,
39            Error::Missing(_) => 5,
40            Error::Crypto(_) => 6,
41            Error::Invalid(_) | Error::Encoding(_) => 7,
42            Error::Io(_) => 1,
43        }
44    }
45}