mit_commit_message_lints/external/
vcs.rs

1use std::convert::Infallible;
2
3use miette::{Diagnostic, Result};
4use thiserror::Error;
5
6/// A wrapper around accessing different values from a VCS config
7pub trait Vcs {
8    /// # Errors
9    ///
10    /// If we can't read the config, or it's not parsable into a bool
11    fn entries(&self, glob: Option<&str>) -> Result<Vec<String>>;
12    /// # Errors
13    ///
14    /// If we can't read the config, or it's not parsable into a bool
15    fn get_bool(&self, name: &str) -> Result<Option<bool>>;
16    /// # Errors
17    ///
18    /// If we can't read the config, or it's not parsable into a &str
19    fn get_str(&self, name: &str) -> Result<Option<&str>>;
20    /// # Errors
21    ///
22    /// If we can't read the config, or it's not parsable into an i64
23    fn get_i64(&self, name: &str) -> Result<Option<i64>>;
24    /// # Errors
25    ///
26    /// If the config fails to write
27    fn set_str(&mut self, name: &str, value: &str) -> Result<()>;
28    /// # Errors
29    ///
30    /// If the config fails to write
31    fn set_i64(&mut self, name: &str, value: i64) -> Result<()>;
32    /// # Errors
33    ///
34    /// If the config fails to write
35    fn remove(&mut self, name: &str) -> Result<()>;
36
37    /// The state of the repository currently
38    ///
39    /// None if there is no repository, and we only have config
40    fn state(&self) -> Option<RepoState>;
41}
42
43/// State of the repository
44#[derive(Debug, Copy, Clone)]
45pub enum RepoState {
46    /// No other state is in progress
47    Clean,
48    /// Merging
49    Merge,
50    /// Reverting commit
51    Revert,
52    /// Reverting the sequence of commits
53    RevertSequence,
54    /// Cherry-picking commit
55    CherryPick,
56    /// Cherry-picking sequence of commits
57    CherryPickSequence,
58    /// git-bisect is in progress
59    Bisect,
60    /// Repository is in rebase
61    Rebase,
62    /// Repository is in interactive rebase
63    RebaseInteractive,
64    /// Repository is applying rebase merge
65    RebaseMerge,
66    /// Repository is applying mailbox
67    ApplyMailbox,
68    /// Repository is applying a mailbox patch or rebasing
69    ApplyMailboxOrRebase,
70}
71
72/// Errors relating to different VCS implementations
73#[derive(Error, Debug, Diagnostic)]
74pub enum Error {
75    /// Libgit2 sourced errors
76    #[error("failed to interact with git repository: {0}")]
77    #[diagnostic(
78        url(docsrs),
79        code(mit_commit_message_lints::external::vcs::error::git2_io)
80    )]
81    Git2Io(git2::Error),
82    /// Failed to parse an int from the in-memory vcs
83    #[error("failed to read int from in memory datastore: {0}")]
84    #[diagnostic(
85        url(docsrs),
86        code(mit_commit_message_lints::external::vcs::error::in_memory_parse_int)
87    )]
88    InMemoryParseInt(std::num::ParseIntError),
89    /// Failed to parse a bool from the in-memory vcs
90    #[error("failed to read bool from in memory datastore: {0}")]
91    #[diagnostic(
92        url(docsrs),
93        code(mit_commit_message_lints::external::vcs::error::in_memory_parse_bool)
94    )]
95    InMemoryParseBool(std::str::ParseBoolError),
96    /// IO Failure when accessing VCS
97    #[error("failed to read git-mit config")]
98    #[diagnostic(url(docsrs), code(mit_commit_message_lints::external::vcs::error::io))]
99    Io(std::io::Error),
100    /// Failure to glob the config key correctly
101    #[error("failed to parse glob {0}")]
102    #[diagnostic(
103        url(docsrs),
104        code(mit_commit_message_lints::external::vcs::error::glob)
105    )]
106    Glob(glob::PatternError),
107    /// Infallible
108    #[error(transparent)]
109    #[diagnostic(
110        url(docsrs),
111        code(mit_commit_message_lints::external::vcs::error::infallible)
112    )]
113    Infallible(Infallible),
114}