diff options
author | Justin Worthe <justin@jemstep.com> | 2020-03-13 10:05:28 +0200 |
---|---|---|
committer | Justin Worthe <justin@jemstep.com> | 2020-03-13 10:05:28 +0200 |
commit | d1c587d7c5068d92d073e976d6bd9d3e9937e715 (patch) | |
tree | f149d379c673cb23b8d693c66ffd1353fe1cbf88 | |
parent | 7cba91c13e4c14e9eff792addec4daad03899b4b (diff) |
Refactored PolicyResult into its own file
-rw-r--r-- | src/lib.rs | 2 | ||||
-rw-r--r-- | src/main.rs | 2 | ||||
-rw-r--r-- | src/policies.rs | 61 | ||||
-rw-r--r-- | src/policies/policy_result.rs | 60 |
4 files changed, 64 insertions, 61 deletions
@@ -12,7 +12,7 @@ use crate::config::Config; use crate::fs::Fs; use crate::git::Git; use crate::gpg::Gpg; -use crate::policies::*; +use crate::policies::{policy_result::PolicyResult, *}; pub mod config; pub mod error; diff --git a/src/main.rs b/src/main.rs index e31a274..b1f0374 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,7 +8,7 @@ use capn::git::{Git, LiveGit}; use capn::gpg::{Gpg, LiveGpg}; use capn::logger; use capn::logger::{Logger, LoggingOpt}; -use capn::policies::PolicyResult; +use capn::policies::policy_result::PolicyResult; use capn::*; use log::*; diff --git a/src/policies.rs b/src/policies.rs index 990e141..edeba5f 100644 --- a/src/policies.rs +++ b/src/policies.rs @@ -1,77 +1,20 @@ +pub mod policy_result; use crate::config::VerifyGitCommitsConfig; use crate::fs::*; use crate::git::*; use crate::gpg::*; use crate::keyring::*; +use self::policy_result::PolicyResult; use git2::Oid; use rayon::prelude::*; use std::collections::HashSet; use std::error::Error; -use std::fmt; -use std::iter; use std::path::PathBuf; use std::time::Instant; use log::*; -#[derive(Debug, Clone)] -pub enum PolicyResult { - Ok, - UnsignedCommit(Oid), - UnsignedMergeCommit(Oid), - NotEnoughAuthors(Oid), - InvalidAuthorEmail(Oid, String), - MissingAuthorEmail(Oid), - InvalidCommitterEmail(Oid, String), - MissingCommitterEmail(Oid), - NotRebased(Oid), -} - -impl PolicyResult { - pub fn and(self, res: PolicyResult) -> PolicyResult { - match self { - PolicyResult::Ok => res, - x => x, - } - } - pub fn is_ok(&self) -> bool { - match self { - PolicyResult::Ok => true, - _ => false, - } - } - pub fn is_err(&self) -> bool { - !self.is_ok() - } -} - -impl fmt::Display for PolicyResult { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - use PolicyResult::*; - - match self { - Ok => write!(f, "Ok"), - UnsignedCommit(id) => write!(f, "Commit does not have a valid GPG signature: {}", id), - UnsignedMergeCommit(id) => write!(f, "Commit does not have a valid GPG signature: {}. This is a merge commit, please note that if there were conflicts that needed to be resolved then the commit needs a signature.", id), - NotEnoughAuthors(id) => write!(f, "Merge commit needs to have multiple authors in the branch: {}", id), - InvalidAuthorEmail(id, email) => write!(f, "Commit has an invalid author email ({}): {}", email, id), - MissingAuthorEmail(id) => write!(f, "Commit does not have an author email: {}", id), - InvalidCommitterEmail(id, email) => write!(f, "Commit has an invalid committer email ({}): {}", email, id), - MissingCommitterEmail(id) => write!(f, "Commit does not have a committer email: {}", id), - NotRebased(id) => write!(f, "Merge commit needs to be rebased on the mainline before it can be merged: {}", id) - } - } -} - -impl iter::FromIterator<PolicyResult> for PolicyResult { - fn from_iter<I: IntoIterator<Item = PolicyResult>>(iter: I) -> Self { - iter.into_iter() - .find(PolicyResult::is_err) - .unwrap_or(PolicyResult::Ok) - } -} - pub fn prepend_branch_name<F: Fs, G: Git>( git: &G, commit_file: PathBuf, diff --git a/src/policies/policy_result.rs b/src/policies/policy_result.rs new file mode 100644 index 0000000..e12ff42 --- /dev/null +++ b/src/policies/policy_result.rs @@ -0,0 +1,60 @@ +use git2::Oid; +use std::fmt; +use std::iter; + +#[derive(Debug, Clone)] +pub enum PolicyResult { + Ok, + UnsignedCommit(Oid), + UnsignedMergeCommit(Oid), + NotEnoughAuthors(Oid), + InvalidAuthorEmail(Oid, String), + MissingAuthorEmail(Oid), + InvalidCommitterEmail(Oid, String), + MissingCommitterEmail(Oid), + NotRebased(Oid), +} + +impl PolicyResult { + pub fn and(self, res: PolicyResult) -> PolicyResult { + match self { + PolicyResult::Ok => res, + x => x, + } + } + pub fn is_ok(&self) -> bool { + match self { + PolicyResult::Ok => true, + _ => false, + } + } + pub fn is_err(&self) -> bool { + !self.is_ok() + } +} + +impl fmt::Display for PolicyResult { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + use PolicyResult::*; + + match self { + Ok => write!(f, "Ok"), + UnsignedCommit(id) => write!(f, "Commit does not have a valid GPG signature: {}", id), + UnsignedMergeCommit(id) => write!(f, "Commit does not have a valid GPG signature: {}. This is a merge commit, please note that if there were conflicts that needed to be resolved then the commit needs a signature.", id), + NotEnoughAuthors(id) => write!(f, "Merge commit needs to have multiple authors in the branch: {}", id), + InvalidAuthorEmail(id, email) => write!(f, "Commit has an invalid author email ({}): {}", email, id), + MissingAuthorEmail(id) => write!(f, "Commit does not have an author email: {}", id), + InvalidCommitterEmail(id, email) => write!(f, "Commit has an invalid committer email ({}): {}", email, id), + MissingCommitterEmail(id) => write!(f, "Commit does not have a committer email: {}", id), + NotRebased(id) => write!(f, "Merge commit needs to be rebased on the mainline before it can be merged: {}", id) + } + } +} + +impl iter::FromIterator<PolicyResult> for PolicyResult { + fn from_iter<I: IntoIterator<Item = PolicyResult>>(iter: I) -> Self { + iter.into_iter() + .find(PolicyResult::is_err) + .unwrap_or(PolicyResult::Ok) + } +} |