Skip to main content

wipe_core/
error.rs

1//! Error type shared across `wipe-core`.
2
3use std::path::Path;
4
5/// Convenient result alias for the crate.
6pub type Result<T> = std::result::Result<T, Error>;
7
8/// All errors that `wipe-core` can produce.
9#[derive(thiserror::Error, Debug)]
10pub enum Error {
11    /// No `.wipe` board was found at the given path or any of its ancestors.
12    #[error("no wipe board found in `{0}` or any parent directory; run `wipe init` first")]
13    NotInitialized(String),
14
15    /// A `.wipe` board already exists where one was about to be created.
16    #[error("a wipe board already exists at `{0}`")]
17    AlreadyInitialized(String),
18
19    /// A ticket with the given ID does not exist.
20    #[error("ticket `{0}` not found")]
21    TicketNotFound(String),
22
23    /// A list with the given ID does not exist.
24    #[error("list `{0}` not found")]
25    ListNotFound(String),
26
27    /// A generic, contextual error message.
28    #[error("{0}")]
29    Message(String),
30
31    /// An underlying filesystem error.
32    #[error(transparent)]
33    Io(#[from] std::io::Error),
34
35    /// A JSON (de)serialization error.
36    #[error(transparent)]
37    Json(#[from] serde_json::Error),
38}
39
40impl Error {
41    /// Build a [`Error::Message`] from anything string-like.
42    pub fn msg(m: impl Into<String>) -> Self {
43        Error::Message(m.into())
44    }
45
46    /// Build a [`Error::NotInitialized`] from a path.
47    pub fn not_initialized(p: impl AsRef<Path>) -> Self {
48        Error::NotInitialized(p.as_ref().display().to_string())
49    }
50}