-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix error conversion and remove dead code
- Loading branch information
Showing
5 changed files
with
128 additions
and
100 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,43 @@ | ||
use std::{error::Error, fmt::Display}; | ||
use thiserror::Error; | ||
|
||
/// Enumerates the possible error values that can be generated by this crate | ||
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)] | ||
#[derive(Error, Debug)] | ||
#[non_exhaustive] | ||
pub enum VSCError { | ||
/// Unable to detect a version control system | ||
#[error("no vcs")] | ||
NoVCS, | ||
/// Found a type of file that was not allowed. Includes listing of disallowed files found | ||
#[error("{}", format_disallowed_files(dirty_files, staged_files))] | ||
NotAllowedFilesFound { | ||
/// Dirty files found (note this will be empty if dirty files are allowed) | ||
dirty_files: Vec<String>, | ||
/// Staged files found (note this will be empty if staged files are allowed) | ||
staged_files: Vec<String>, | ||
}, | ||
/// Errors produced outside of the ones from this library | ||
#[error(transparent)] | ||
Other(#[from] anyhow::Error), // source and Display delegate to anyhow::Error | ||
} | ||
|
||
impl Display for VSCError { | ||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||
match self { | ||
VSCError::NoVCS => write!(f, "no VCS found but this not allowed"), | ||
VSCError::NotAllowedFilesFound { | ||
dirty_files, | ||
staged_files, | ||
} => { | ||
let mut files_list = String::new(); | ||
for file in dirty_files { | ||
files_list.push_str(" * "); | ||
files_list.push_str(file); | ||
files_list.push_str(" (dirty)\n"); | ||
} | ||
for file in staged_files { | ||
files_list.push_str(" * "); | ||
files_list.push_str(file); | ||
files_list.push_str(" (staged)\n"); | ||
} | ||
|
||
write!( | ||
f, | ||
"disallowed files found:\n\ | ||
\n\ | ||
{}\n\ | ||
", | ||
files_list | ||
) | ||
} | ||
} | ||
fn format_disallowed_files(dirty_files: &[String], staged_files: &[String]) -> String { | ||
let mut files_list = String::new(); | ||
for file in dirty_files { | ||
files_list.push_str(" * "); | ||
files_list.push_str(file); | ||
files_list.push_str(" (dirty)\n"); | ||
} | ||
for file in staged_files { | ||
files_list.push_str(" * "); | ||
files_list.push_str(file); | ||
files_list.push_str(" (staged)\n"); | ||
} | ||
} | ||
|
||
impl Error for VSCError {} | ||
format!( | ||
"disallowed files found:\n\ | ||
\n\ | ||
{}\n\ | ||
", | ||
files_list | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters