gnostr_asyncgit/gitui/gitui_error/
mod.rs1use std::{fmt::Display, io, string};
2
3#[derive(Debug)]
5pub enum Error {
6 StashList(git2::Error),
8 ReadLog(git2::Error),
10 OpenRepo(git2::Error),
12 FindGitDir(io::Error),
14 Term(io::Error),
16 GitDirUtf8(string::FromUtf8Error),
18 Config(figment::Error),
20 FileWatcher(notify::Error),
22 ReadRebaseStatusFile(io::Error),
24 ReadBranchName(io::Error),
26 BranchNameUtf8(Utf8Error),
28 GitDiff(io::Error),
30 GitDiffUtf8(string::FromUtf8Error),
32 GitShow(io::Error),
34 GitShowUtf8(string::FromUtf8Error),
36 GitShowMeta(git2::Error),
38 NotOnBranch,
40 GetHead(git2::Error),
42 CurrentBranchName(git2::Error),
44 GetCurrentBranchUpstream(git2::Error),
46 GetCurrentBranchUpstreamUtf8(Utf8Error),
48 RemoteNameUtf8(Utf8Error),
50 CannotDeleteCurrentBranch,
52 BranchNameRequired,
54 IsBranchMerged(git2::Error),
56 GetRemote(git2::Error),
58 ReadGitConfig(git2::Error),
60 ReadGitConfigUtf8(Utf8Error),
62 DeleteGitConfig(git2::Error),
64 SetGitConfig(git2::Error),
66 RemoteHasNoName,
68 ReadOid(git2::Error),
70 ArgMustBePositiveNumber,
72 ArgInvalidRegex(regex::Error),
74 Clipboard(arboard::Error),
76 FindGitRev(git2::Error),
78 NoEditorSet,
80 GitStatus(git2::Error),
82 CmdAlreadyRunning,
84 StashWorkTreeEmpty,
86 CouldntAwaitCmd(io::Error),
88 NoRepoWorkdir,
90 SpawnCmd(io::Error),
92 CmdBadExit(String, Option<i32>),
94 CouldntReadCmdOutput(io::Error),
96 ListGitReferences(git2::Error),
98 OpenLogFile(io::Error),
100 PromptAborted,
102 NoMoreEvents,
104}
105
106impl std::error::Error for Error {}
108
109impl Display for Error {
111 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
112 match self {
113 Error::StashList(e) => f.write_fmt(format_args!("Couldn't list stash: {}", e)),
114 Error::ReadLog(e) => f.write_fmt(format_args!("Couldn't read log: {}", e)),
115 Error::OpenRepo(e) => match e.code() {
116 git2::ErrorCode::NotFound => f.write_str("No .git found in the current directory"),
117 _ => f.write_fmt(format_args!("Couldn't open repo: {e:?}")),
118 },
119 Error::FindGitDir(e) => f.write_fmt(format_args!("Couldn't find git directory: {}", e)),
120 Error::Term(e) => f.write_fmt(format_args!("Terminal error: {}", e)),
121 Error::GitDirUtf8(_e) => f.write_str("Git directory not valid UTF-8"),
122 Error::Config(e) => f.write_fmt(format_args!("Configuration error: {}", e)),
123 Error::FileWatcher(e) => f.write_fmt(format_args!("File watcher error: {}", e)),
124 Error::ReadRebaseStatusFile(e) => {
125 f.write_fmt(format_args!("Couldn't read rebase status file: {}", e))
126 }
127 Error::ReadBranchName(e) => {
128 f.write_fmt(format_args!("Couldn't read branch name: {}", e))
129 }
130 Error::BranchNameUtf8(_e) => f.write_str("Branch name error"),
131 Error::GitDiff(e) => f.write_fmt(format_args!("Git diff error: {}", e)),
132 Error::GitDiffUtf8(e) => {
133 f.write_fmt(format_args!("Git diff output is not valid UTF-8: {}", e))
134 }
135 Error::GitShow(e) => f.write_fmt(format_args!("Git show error: {}", e)),
136 Error::GitShowUtf8(e) => {
137 f.write_fmt(format_args!("Git show output is not valid UTF-8: {}", e))
138 }
139 Error::GitShowMeta(e) => f.write_fmt(format_args!("Git show metadata error: {}", e)),
140 Error::NotOnBranch => f.write_str("Head is not a branch"),
141 Error::GetHead(e) => f.write_fmt(format_args!("Couldn't get HEAD: {}", e)),
142 Error::CurrentBranchName(e) => {
143 f.write_fmt(format_args!("Couldn't get current branch name: {}", e))
144 }
145 Error::GetCurrentBranchUpstream(e) => {
146 f.write_fmt(format_args!("Couldn't get current branch upstream: {}", e))
147 }
148 Error::GetCurrentBranchUpstreamUtf8(_e) => {
149 f.write_str("Current branch upstream is not valid UTF-8")
150 }
151 Error::RemoteNameUtf8(_e) => f.write_str("Remote name is not valid UTF-8"),
152 Error::CannotDeleteCurrentBranch => f.write_str("Cannot delete current branch"),
153 Error::BranchNameRequired => f.write_str("Branch name required"),
154 Error::IsBranchMerged(e) => {
155 f.write_fmt(format_args!("Couldn't check if branch is merged: {}", e))
156 }
157 Error::GetRemote(e) => f.write_fmt(format_args!("Couldn't get remote: {}", e)),
158 Error::ReadGitConfig(e) => f.write_fmt(format_args!("Couldn't read git config: {}", e)),
159 Error::ReadGitConfigUtf8(_e) => f.write_str("Git config is not valid UTF-8"),
160 Error::DeleteGitConfig(e) => {
161 f.write_fmt(format_args!("Couldn't delete git config: {}", e))
162 }
163 Error::SetGitConfig(e) => f.write_fmt(format_args!("Couldn't set git config: {}", e)),
164 Error::RemoteHasNoName => f.write_str("Remote has no name"),
165 Error::ReadOid(e) => f.write_fmt(format_args!("Couldn't read OID: {}", e)),
166 Error::ArgMustBePositiveNumber => f.write_str("Value must be a number greater than 0"),
167 Error::ArgInvalidRegex(e) => f.write_fmt(format_args!("Invalid regex: {}", e)),
168 Error::Clipboard(e) => f.write_fmt(format_args!("Clipboard error: {}", e)),
169 Error::FindGitRev(e) => f.write_fmt(format_args!("Couldn't find git revision: {}", e)),
170 Error::NoEditorSet => f.write_fmt(format_args!(
171 "No editor environment variable set ({})",
172 crate::gitui::ops::show::EDITOR_VARS.join(", ")
173 )),
174 Error::GitStatus(e) => f.write_fmt(format_args!("Git status error: {}", e)),
175 Error::CmdAlreadyRunning => f.write_str("A command is already running"),
176 Error::StashWorkTreeEmpty => f.write_str("Cannot stash: working tree is empty"),
177 Error::CouldntAwaitCmd(e) => f.write_fmt(format_args!("Couldn't await command: {}", e)),
178 Error::NoRepoWorkdir => f.write_str("No repository working directory"),
179 Error::SpawnCmd(e) => f.write_fmt(format_args!("Failed to spawn command: {}", e)),
180 Error::CmdBadExit(args, code) => f.write_fmt(format_args!(
181 "'{}' exited with code: {}",
182 args,
183 code.map(|c| c.to_string())
184 .unwrap_or_else(|| "".to_string())
185 )),
186 Error::CouldntReadCmdOutput(e) => {
187 f.write_fmt(format_args!("Couldn't read command output: {}", e))
188 }
189 Error::ListGitReferences(e) => {
190 f.write_fmt(format_args!("Couldn't list git references: {}", e))
191 }
192 Error::OpenLogFile(e) => f.write_fmt(format_args!("Couldn't open log file: {}", e)),
193 Error::PromptAborted => f.write_str("Aborted"),
194 Error::NoMoreEvents => unimplemented!(),
195 }
196 }
197}
198
199#[derive(Debug)]
201pub enum Utf8Error {
202 Str(std::str::Utf8Error),
204 String(string::FromUtf8Error),
206}
207
208impl std::error::Error for Utf8Error {}
210
211impl Display for Utf8Error {
213 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
214 f.write_str("not valid UTF-8")
215 }
216}