#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum Severity {
Off,
Warn,
Error,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub enum PolicyId {
RequireRoot,
LinkedWorktreeOnly,
BranchWorktreeOnly,
AddExplicit,
FinalNewline,
MarkdownAutofix,
ForbiddenRootContext,
DependentVersionBump,
CatalogSpecifiers,
ForbiddenStrings,
}
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub struct PolicyDescriptor {
pub id: PolicyId,
pub name: &'static str,
pub default_severity: Severity,
pub warn_safe: bool,
pub accepts_options: bool,
pub off_unless_listed: bool,
}
pub const POLICY_REGISTRY: &[PolicyDescriptor] = &[
PolicyDescriptor {
id: PolicyId::RequireRoot,
name: "require-root",
default_severity: Severity::Error,
warn_safe: false,
accepts_options: false,
off_unless_listed: false,
},
PolicyDescriptor {
id: PolicyId::LinkedWorktreeOnly,
name: "linked-worktree-only",
default_severity: Severity::Error,
warn_safe: false,
accepts_options: false,
off_unless_listed: false,
},
PolicyDescriptor {
id: PolicyId::BranchWorktreeOnly,
name: "branch-worktree-only",
default_severity: Severity::Error,
warn_safe: true,
accepts_options: false,
off_unless_listed: false,
},
PolicyDescriptor {
id: PolicyId::AddExplicit,
name: "add-explicit",
default_severity: Severity::Error,
warn_safe: false,
accepts_options: false,
off_unless_listed: false,
},
PolicyDescriptor {
id: PolicyId::FinalNewline,
name: "final-newline",
default_severity: Severity::Warn,
warn_safe: true,
accepts_options: false,
off_unless_listed: false,
},
PolicyDescriptor {
id: PolicyId::MarkdownAutofix,
name: "markdown/autofix",
default_severity: Severity::Warn,
warn_safe: true,
accepts_options: true,
off_unless_listed: true,
},
PolicyDescriptor {
id: PolicyId::ForbiddenRootContext,
name: "mono/forbidden-root-context",
default_severity: Severity::Error,
warn_safe: true,
accepts_options: false,
off_unless_listed: true,
},
PolicyDescriptor {
id: PolicyId::DependentVersionBump,
name: "mono/dependent-version-bump",
default_severity: Severity::Error,
warn_safe: false,
accepts_options: false,
off_unless_listed: true,
},
PolicyDescriptor {
id: PolicyId::CatalogSpecifiers,
name: "mono/catalog-specifiers",
default_severity: Severity::Error,
warn_safe: false,
accepts_options: false,
off_unless_listed: true,
},
PolicyDescriptor {
id: PolicyId::ForbiddenStrings,
name: "security/forbidden-strings",
default_severity: Severity::Error,
warn_safe: false,
accepts_options: true,
off_unless_listed: true,
},
];
pub fn policy_by_name(name: &str) -> Option<&'static PolicyDescriptor> {
let mut index: usize = 0;
while index < POLICY_REGISTRY.len() {
let descriptor: &PolicyDescriptor = &POLICY_REGISTRY[index];
index += 1;
if descriptor.name == name {
return Some(descriptor);
}
}
return None;
}
pub fn policy_descriptor(id: PolicyId) -> &'static PolicyDescriptor {
for descriptor in POLICY_REGISTRY {
if descriptor.id == id {
return descriptor;
}
}
unreachable!("every PolicyId variant has a POLICY_REGISTRY row");
}
pub fn severity_from_name(name: &str) -> Option<Severity> {
if name == "off" {
return Some(Severity::Off);
}
if name == "warn" {
return Some(Severity::Warn);
}
if name == "error" {
return Some(Severity::Error);
}
return None;
}
pub fn severity_name(severity: Severity) -> &'static str {
if severity == Severity::Off {
return "off";
}
if severity == Severity::Warn {
return "warn";
}
return "error";
}
#[cfg(test)]
#[path = "policy_registry_tests.rs"]
mod tests;