extern crate git_workarea;
use self::git_workarea::CommitId;
use super::commit::Commit;
use super::context::CheckGitContext;
use super::error::Result;
#[derive(Debug, Default)]
pub struct CheckResult {
warnings: Vec<String>,
alerts: Vec<String>,
errors: Vec<String>,
allow: bool,
pass: bool,
}
impl CheckResult {
pub fn new() -> Self {
CheckResult {
warnings: vec![],
alerts: vec![],
errors: vec![],
allow: false,
pass: true,
}
}
pub fn add_warning<S: ToString>(&mut self, warning: S) -> &mut Self {
self.warnings.push(warning.to_string());
self
}
pub fn add_alert<S: ToString>(&mut self, alert: S, should_block: bool) -> &mut Self {
self.alerts.push(alert.to_string());
self.pass = self.pass && !should_block;
self
}
pub fn add_error<S: ToString>(&mut self, error: S) -> &mut Self {
self.errors.push(error.to_string());
self.pass = false;
self
}
pub fn whitelist(&mut self) -> &mut Self {
self.allow = true;
self
}
pub fn warnings(&self) -> &Vec<String> {
&self.warnings
}
pub fn alerts(&self) -> &Vec<String> {
&self.alerts
}
pub fn errors(&self) -> &Vec<String> {
&self.errors
}
pub fn allowed(&self) -> bool {
self.allow
}
pub fn pass(&self) -> bool {
self.pass
}
pub fn combine(self, other: Self) -> Self {
CheckResult {
warnings: self.warnings.into_iter().chain(other.warnings.into_iter()).collect(),
alerts: self.alerts.into_iter().chain(other.alerts.into_iter()).collect(),
errors: self.errors.into_iter().chain(other.errors.into_iter()).collect(),
allow: self.allow || other.allow,
pass: self.pass && other.pass,
}
}
}
pub trait Check: Sync {
fn name(&self) -> &str;
fn check(&self, ctx: &CheckGitContext, commit: &Commit) -> Result<CheckResult>;
}
pub trait BranchCheck: Sync {
fn name(&self) -> &str;
fn check(&self, ctx: &CheckGitContext, commit: &CommitId) -> Result<CheckResult>;
}