dot_agent_core/
error.rs

1use std::path::PathBuf;
2use thiserror::Error;
3
4#[derive(Debug, Error)]
5pub enum DotAgentError {
6    #[error("Profile not found: {name}")]
7    ProfileNotFound { name: String },
8
9    #[error("Target directory does not exist: {path}")]
10    TargetNotFound { path: PathBuf },
11
12    #[error("Profile already exists: {name}")]
13    ProfileAlreadyExists { name: String },
14
15    #[error("Invalid profile name: '{name}' - must contain only alphanumeric, hyphen, underscore")]
16    InvalidProfileName { name: String },
17
18    #[error("Conflict detected - file exists with different content: {path}")]
19    Conflict { path: PathBuf },
20
21    #[error("Local modifications detected: {paths:?}")]
22    LocalModifications { paths: Vec<PathBuf> },
23
24    #[error("IO error: {0}")]
25    Io(#[from] std::io::Error),
26
27    #[error("TOML serialization error: {0}")]
28    TomlSer(#[from] toml::ser::Error),
29
30    #[error("TOML deserialization error: {0}")]
31    TomlDe(#[from] toml::de::Error),
32
33    #[error("Home directory not found")]
34    HomeNotFound,
35
36    #[error("GUI error: {0}")]
37    Gui(String),
38
39    #[error("Git error: {0}")]
40    Git(String),
41
42    #[error("Rule not found: {name}")]
43    RuleNotFound { name: String },
44
45    #[error("Rule already exists: {name}")]
46    RuleAlreadyExists { name: String },
47
48    #[error("Invalid rule name: '{name}' - must be alphanumeric, hyphen, underscore, 1-64 chars")]
49    InvalidRuleName { name: String },
50
51    #[error("Claude CLI not found. Install with: brew install claude")]
52    ClaudeNotFound,
53
54    #[error("Claude CLI execution failed: {message}")]
55    ClaudeExecutionFailed { message: String },
56
57    #[error("Glob pattern error: {0}")]
58    GlobPattern(#[from] glob::PatternError),
59
60    #[error("Glob error: {0}")]
61    Glob(#[from] glob::GlobError),
62
63    #[error("Snapshot not found: {id}")]
64    SnapshotNotFound { id: String },
65}
66
67pub type Result<T> = std::result::Result<T, DotAgentError>;
68
69impl DotAgentError {
70    pub fn exit_code(&self) -> i32 {
71        match self {
72            Self::ProfileNotFound { .. } => 2,
73            Self::TargetNotFound { .. } => 3,
74            Self::LocalModifications { .. } => 4,
75            Self::InvalidProfileName { .. } => 5,
76            Self::Conflict { .. } => 6,
77            Self::RuleNotFound { .. } => 7,
78            Self::RuleAlreadyExists { .. } => 8,
79            Self::InvalidRuleName { .. } => 9,
80            Self::ClaudeNotFound => 10,
81            Self::ClaudeExecutionFailed { .. } => 11,
82            Self::SnapshotNotFound { .. } => 12,
83            _ => 1,
84        }
85    }
86}