Skip to main content

csd/
error.rs

1use std::path::PathBuf;
2
3use thiserror::Error;
4
5/// All fallible operations in the csd library return this.
6pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error)]
9pub enum Error {
10    #[error("tmux command failed: {0}")]
11    Tmux(String),
12
13    #[error("tmux is not installed or not on PATH")]
14    TmuxMissing,
15
16    #[error("session {0:?} does not exist")]
17    NoSuchSession(String),
18
19    #[error("session {0:?} already exists")]
20    SessionExists(String),
21
22    #[error("invalid session name {0:?}: use only [A-Za-z0-9._-], starting with a letter, digit or underscore")]
23    InvalidSessionName(String),
24
25    #[error("could not resolve {what} directory")]
26    NoDir { what: &'static str },
27
28    #[error("input was not accepted by the TUI after {0} attempts")]
29    InputNotAccepted(u32),
30
31    #[error("unknown backend {0:?}")]
32    UnknownBackend(String),
33
34    #[error("invalid session id {0:?}: {1}")]
35    InvalidSessionId(String, String),
36
37    #[error("invalid permission mode {0:?}, expected one of {1}")]
38    InvalidPermissionMode(String, String),
39
40    #[error("conflicting permission flags: use at most one of --permission-mode, --auto-accept, --bypass-permissions, --yolo")]
41    ConflictingPermissionFlags,
42
43    #[error("failed to read {path:?}: {source}")]
44    Io {
45        path: PathBuf,
46        #[source]
47        source: std::io::Error,
48    },
49
50    #[error(transparent)]
51    Json(#[from] serde_json::Error),
52}
53
54impl Error {
55    pub(crate) fn io(path: impl Into<PathBuf>, source: std::io::Error) -> Self {
56        Error::Io {
57            path: path.into(),
58            source,
59        }
60    }
61}