version_control_clean_check/
error.rs

1use thiserror::Error;
2
3/// Enumerates the possible error values that can be generated by this crate
4#[derive(Error, Debug)]
5#[non_exhaustive]
6pub enum VCSError {
7    /// Unable to detect a version control system
8    #[error("no vcs")]
9    NoVCS,
10    /// Found a type of file that was not allowed. Includes listing of disallowed files found
11    #[error("{}", format_disallowed_files(dirty_files, staged_files))]
12    NotAllowedFilesFound {
13        /// Dirty files found (note this will be empty if dirty files are allowed)
14        dirty_files: Vec<String>,
15        /// Staged files found (note this will be empty if staged files are allowed)
16        staged_files: Vec<String>,
17    },
18    /// Errors from the [git2](https://docs.rs/git2/latest/git2/) library
19    #[error(transparent)]
20    GitError(#[from] git2::Error),
21    /// Errors that were wrapped in anyhow by [cargo_utils](https://docs.rs/cargo-util/latest/cargo_util/)
22    #[error(transparent)]
23    Anyhow(#[from] anyhow::Error),
24}
25
26fn format_disallowed_files(dirty_files: &[String], staged_files: &[String]) -> String {
27    let mut files_list = String::new();
28    for file in dirty_files {
29        files_list.push_str("  * ");
30        files_list.push_str(file);
31        files_list.push_str(" (dirty)\n");
32    }
33    for file in staged_files {
34        files_list.push_str("  * ");
35        files_list.push_str(file);
36        files_list.push_str(" (staged)\n");
37    }
38
39    format!(
40        "disallowed files found:\n\
41         \n\
42         {}\n\
43         ",
44        files_list
45    )
46}