Skip to main content

shaul_core/
error.rs

1use thiserror::Error;
2
3/// Errors produced by the core engine, store, finalizers and backends.
4#[derive(Debug, Error)]
5#[non_exhaustive]
6pub enum Error {
7    /// The supplied link is malformed or empty.
8    #[error("invalid link: {0}")]
9    InvalidLink(String),
10    /// No registered parser accepted the link.
11    #[error("no parser accepted link: {0}")]
12    NoParser(String),
13    /// No registered backend supports the resolved resource.
14    #[error("no backend supports resource: {0}")]
15    NoBackend(String),
16    /// No task with the given identifier exists in the store.
17    #[error("task not found: {0}")]
18    TaskNotFound(String),
19    /// The task is not in a state that allows the requested operation.
20    #[error("invalid task state: {0}")]
21    InvalidState(String),
22    /// A downloaded file did not match the expected checksum.
23    #[error("checksum mismatch: expected {expected}, got {actual}")]
24    Verification {
25        /// Expected digest formatted as `<algorithm>:<hex>`.
26        expected: String,
27        /// Digest computed from the downloaded file.
28        actual: String,
29    },
30    /// Verification was required but no checksum is available.
31    #[error("no checksum available for verification")]
32    ChecksumMissing,
33    /// Archive extraction failed.
34    #[error("extraction failed: {0}")]
35    Extraction(String),
36    /// The task was cancelled.
37    #[error("cancelled")]
38    Cancelled,
39    /// The backend reported an error.
40    #[error("backend error: {0}")]
41    Backend(String),
42    /// The configuration is invalid or inconsistent.
43    #[error("config error: {0}")]
44    Config(String),
45    /// An I/O operation failed.
46    #[error("io error: {0}")]
47    Io(#[from] std::io::Error),
48    /// JSON serialization or deserialization failed.
49    #[error("json error: {0}")]
50    Json(#[from] serde_json::Error),
51    /// TOML serialization or deserialization failed.
52    #[error("toml error: {0}")]
53    Toml(String),
54    /// A URL could not be parsed.
55    #[error("url error: {0}")]
56    Url(#[from] url::ParseError),
57    /// An error that does not fit any other category.
58    #[error("{0}")]
59    Other(String),
60}
61
62impl From<toml::de::Error> for Error {
63    fn from(value: toml::de::Error) -> Self {
64        Error::Toml(value.to_string())
65    }
66}
67
68impl From<toml::ser::Error> for Error {
69    fn from(value: toml::ser::Error) -> Self {
70        Error::Toml(value.to_string())
71    }
72}
73
74impl Error {
75    /// Returns `true` if this error represents a cancellation.
76    pub fn is_cancelled(&self) -> bool {
77        matches!(self, Error::Cancelled)
78    }
79}
80
81/// Result type used throughout the crate.
82pub type Result<T> = std::result::Result<T, Error>;