git_worktree_cli/
error.rs1use thiserror::Error;
7
8#[derive(Error, Debug)]
10pub enum Error {
11 #[error("IO error: {0}")]
13 Io(#[from] std::io::Error),
14
15 #[error("Git command failed: {0}")]
17 Git(String),
18
19 #[error("Configuration error: {0}")]
21 Config(String),
22
23 #[error("API provider error: {0}")]
25 Provider(String),
26
27 #[error("Project root not found")]
29 ProjectRootNotFound,
30
31 #[error("Git directory not found in project")]
33 GitDirectoryNotFound,
34
35 #[error("Branch operation failed: {0}")]
37 Branch(String),
38
39 #[error("Hook execution failed: {0}")]
41 Hook(String),
42
43 #[error("Authentication error: {0}")]
45 Auth(String),
46
47 #[error("Network error: {0}")]
49 Network(String),
50
51 #[error("JSON parsing error: {0}")]
53 Json(String),
54
55 #[error("Regex error: {0}")]
57 Regex(#[from] regex::Error),
58
59 #[error("{0}")]
61 Other(String),
62}
63
64pub type Result<T> = std::result::Result<T, Error>;
66
67impl Error {
69 pub fn msg<S: Into<String>>(msg: S) -> Self {
71 Error::Other(msg.into())
72 }
73
74 pub fn git<S: Into<String>>(msg: S) -> Self {
76 Error::Git(msg.into())
77 }
78
79 pub fn config<S: Into<String>>(msg: S) -> Self {
81 Error::Config(msg.into())
82 }
83
84 pub fn branch<S: Into<String>>(msg: S) -> Self {
86 Error::Branch(msg.into())
87 }
88
89 pub fn hook<S: Into<String>>(msg: S) -> Self {
91 Error::Hook(msg.into())
92 }
93
94 pub fn auth<S: Into<String>>(msg: S) -> Self {
96 Error::Auth(msg.into())
97 }
98
99 pub fn provider<S: Into<String>>(msg: S) -> Self {
101 Error::Provider(msg.into())
102 }
103
104 pub fn network<S: Into<String>>(msg: S) -> Self {
106 Error::Network(msg.into())
107 }
108}
109
110impl From<json5::Error> for Error {
112 fn from(err: json5::Error) -> Self {
113 Error::Config(err.to_string())
114 }
115}
116
117impl From<serde_json::Error> for Error {
118 fn from(err: serde_json::Error) -> Self {
119 Error::Json(err.to_string())
120 }
121}
122
123impl From<keyring::Error> for Error {
124 fn from(err: keyring::Error) -> Self {
125 Error::Auth(err.to_string())
126 }
127}
128
129impl From<reqwest::Error> for Error {
130 fn from(err: reqwest::Error) -> Self {
131 Error::Network(err.to_string())
132 }
133}
134
135impl From<std::env::VarError> for Error {
136 fn from(err: std::env::VarError) -> Self {
137 Error::Other(err.to_string())
138 }
139}
140
141impl From<std::string::FromUtf8Error> for Error {
142 fn from(err: std::string::FromUtf8Error) -> Self {
143 Error::Other(err.to_string())
144 }
145}