1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
use thiserror::Error;

/// Enumerates the possible error values that can be generated by this crate
#[derive(Error, Debug)]
#[non_exhaustive]
pub enum VCSError {
    /// 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 from the [git2](https://docs.rs/git2/latest/git2/) library
    #[error(transparent)]
    GitError(#[from] git2::Error),
    /// Errors that were wrapped in anyhow by [cargo_utils](https://docs.rs/cargo-util/latest/cargo_util/)
    #[error(transparent)]
    Anyhow(#[from] anyhow::Error),
}

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");
    }

    format!(
        "disallowed files found:\n\
         \n\
         {}\n\
         ",
        files_list
    )
}