use std::collections::BTreeMap;
use std::fmt;
use crate::validation::report::Severity;
use crate::validation::rule_status::ValidationRuleStatus;
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub struct ValidationConfig {
pub complete: bool,
pub compliant: bool,
pub best_practice: bool,
pub types_in_uri: bool,
pub keep_going: bool,
}
impl Default for ValidationConfig {
fn default() -> Self {
Self {
complete: true,
compliant: true,
best_practice: false,
types_in_uri: false,
keep_going: true,
}
}
}
impl ValidationConfig {
pub fn all_on() -> Self {
Self {
complete: true,
compliant: true,
best_practice: true,
types_in_uri: true,
keep_going: true,
}
}
pub fn with_complete(mut self, on: bool) -> Self {
self.complete = on;
self
}
pub fn with_compliant(mut self, on: bool) -> Self {
self.compliant = on;
self
}
pub fn with_best_practice(mut self, on: bool) -> Self {
self.best_practice = on;
self
}
pub fn with_types_in_uri(mut self, on: bool) -> Self {
self.types_in_uri = on;
self
}
pub fn with_keep_going(mut self, on: bool) -> Self {
self.keep_going = on;
self
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum TopologyCompleteness {
#[default]
Conservative,
RequireKnownForNucleicAcids,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum ExternalValidationMode {
#[default]
Off,
ProvidedOnly,
ExternalAllowed,
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum RuleOverride {
Suppress,
Severity(Severity),
}
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnknownRule {
pub rule: String,
}
impl fmt::Display for UnknownRule {
fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
formatter,
"`{}` is not a recognized SBOL validation rule",
self.rule
)
}
}
impl std::error::Error for UnknownRule {}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct RuleOverrides {
overrides: BTreeMap<&'static str, RuleOverride>,
severity_floor: Option<Severity>,
severity_ceiling: Option<Severity>,
}
impl RuleOverrides {
pub fn set(
mut self,
rule: &str,
ovr: RuleOverride,
catalog: &[ValidationRuleStatus],
) -> Result<Self, UnknownRule> {
let canonical = canonical_rule_id(rule, catalog)?;
self.overrides.insert(canonical, ovr);
Ok(self)
}
pub fn with_severity_floor(mut self, floor: Severity) -> Self {
self.severity_floor = Some(floor);
self
}
pub fn with_severity_ceiling(mut self, ceiling: Severity) -> Self {
self.severity_ceiling = Some(ceiling);
self
}
pub fn iter(&self) -> impl Iterator<Item = (&'static str, RuleOverride)> + '_ {
self.overrides.iter().map(|(rule, ovr)| (*rule, *ovr))
}
pub fn severity_floor(&self) -> Option<Severity> {
self.severity_floor
}
pub fn severity_ceiling(&self) -> Option<Severity> {
self.severity_ceiling
}
pub fn resolved_severity(
&self,
rule: &'static str,
catalog_default: Severity,
) -> Option<Severity> {
let base = match self.overrides.get(rule).copied() {
Some(RuleOverride::Suppress) => return None,
Some(RuleOverride::Severity(severity)) => severity,
None => catalog_default,
};
let with_floor = match self.severity_floor {
Some(floor) if floor > base => floor,
_ => base,
};
let with_ceiling = match self.severity_ceiling {
Some(ceiling) if ceiling < with_floor => ceiling,
_ => with_floor,
};
Some(with_ceiling)
}
}
fn canonical_rule_id(
rule: &str,
catalog: &[ValidationRuleStatus],
) -> Result<&'static str, UnknownRule> {
catalog
.iter()
.find(|status| status.rule == rule)
.map(|status| status.rule)
.ok_or_else(|| UnknownRule {
rule: rule.to_owned(),
})
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub struct PolicyOptions {
pub hash_algorithm_registry: HashAlgorithmRegistry,
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[non_exhaustive]
pub enum HashAlgorithmRegistry {
#[default]
Conservative,
Strict,
Lenient,
}