From 6785bade8f9a9636fc468223e33b9c12002ac593 Mon Sep 17 00:00:00 2001 From: Justin Worthe Date: Mon, 30 Mar 2020 10:50:06 +0200 Subject: Added a lazily-executed variant of policy result chaining --- src/policies.rs | 42 ++++++++++++++++++++---------------------- src/policies/policy_result.rs | 10 ++++++++++ 2 files changed, 30 insertions(+), 22 deletions(-) diff --git a/src/policies.rs b/src/policies.rs index 5eed65f..fb10aed 100644 --- a/src/policies.rs +++ b/src/policies.rs @@ -70,37 +70,35 @@ pub fn verify_git_commits( )?; if config.verify_email_addresses { - policy_result = policy_result.and(verify_email_addresses( - &config.author_domain, - &config.committer_domain, - ¬_manually_verified_commits, - )); + policy_result = policy_result.and_then(|| { + Ok(verify_email_addresses( + &config.author_domain, + &config.committer_domain, + ¬_manually_verified_commits, + )) + })?; } if config.verify_commit_signatures { - policy_result = policy_result.and(verify_commit_signatures::( - git, - &gpg, - ¬_manually_verified_commits, - &mut keyring, - )?); + policy_result = policy_result.and_then(|| { + verify_commit_signatures::( + git, + &gpg, + ¬_manually_verified_commits, + &mut keyring, + ) + })?; } if config.verify_different_authors { - policy_result = policy_result.and(verify_different_authors::( - &all_commits, - git, - &ref_update, - )?); + policy_result = policy_result + .and_then(|| verify_different_authors::(&all_commits, git, &ref_update))?; } if config.verify_rebased { - policy_result = policy_result.and(verify_rebased::( - &all_commits, - git, - &ref_update, - &config.override_tag_pattern, - )?); + policy_result = policy_result.and_then(|| { + verify_rebased::(&all_commits, git, &ref_update, &config.override_tag_pattern) + })?; } } diff --git a/src/policies/policy_result.rs b/src/policies/policy_result.rs index e12ff42..852c705 100644 --- a/src/policies/policy_result.rs +++ b/src/policies/policy_result.rs @@ -1,4 +1,5 @@ use git2::Oid; +use std::error::Error; use std::fmt; use std::iter; @@ -22,6 +23,15 @@ impl PolicyResult { x => x, } } + pub fn and_then( + self, + mut next: impl FnMut() -> Result>, + ) -> Result> { + match self { + PolicyResult::Ok => next(), + x => Ok(x), + } + } pub fn is_ok(&self) -> bool { match self { PolicyResult::Ok => true, -- cgit v1.2.3