1pub type Result<T> = std::result::Result<T, TixError>;
10
11#[derive(Debug, thiserror::Error)]
13pub enum TixError {
14 #[error("io error: {0}")]
16 Io(#[from] std::io::Error),
17
18 #[error("config error: {0}")]
20 Config(String),
21
22 #[error("parse error: {0}")]
24 Parse(String),
25
26 #[error("not found: {0}")]
28 NotFound(String),
29
30 #[error("invalid input: {0}")]
32 InvalidInput(String),
33
34 #[error("editor error: {0}")]
36 Editor(String),
37
38 #[error("external tool error: {0}")]
40 External(String),
41
42 #[error("kitty error: {0}")]
44 Kitty(String),
45}
46
47impl TixError {
48 pub fn with_path(self, path: impl std::fmt::Display) -> Self {
50 match self {
51 TixError::Io(e) => TixError::Io(e),
52 TixError::Config(msg) => TixError::Config(format!("{path}: {msg}")),
53 TixError::Parse(msg) => TixError::Parse(format!("{path}: {msg}")),
54 TixError::NotFound(msg) => TixError::NotFound(format!("{path}: {msg}")),
55 other => other,
56 }
57 }
58}
59
60#[cfg(test)]
61mod tests {
62 use super::*;
63
64 #[test]
65 fn result_alias_resolves_to_tix_error() {
66 let err: TixError = TixError::InvalidInput("nope".into());
67 let r: Result<()> = Err(err);
68 assert!(r.is_err());
69 }
70
71 #[test]
72 fn config_error_carries_path_when_prefixed() {
73 let e = TixError::Config("bad keys".into()).with_path("tix.toml");
74 assert_eq!(e.to_string(), "config error: tix.toml: bad keys");
75 }
76}