use serde::Serialize;
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Serialize)]
#[serde(rename_all = "kebab-case")]
pub enum Layer {
Policy,
HostModel,
RealHost,
OsSandbox,
Network,
}
impl Layer {
pub fn label(self) -> &'static str {
match self {
Self::Policy => "policy decision",
Self::HostModel => "host model (golden payloads)",
Self::RealHost => "real host",
Self::OsSandbox => "OS sandbox",
Self::Network => "network boundary",
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Evidence {
Held,
Broken(String),
Unexercised(String),
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(tag = "status", content = "reason", rename_all = "kebab-case")]
pub enum Status {
Pass,
Fail(String),
#[serde(rename = "not-tested-here")]
NotTested(String),
}
impl Status {
pub fn word(&self) -> &'static str {
match self {
Self::Pass => "PASS",
Self::Fail(_) => "FAIL",
Self::NotTested(_) => "NOT TESTED HERE",
}
}
pub fn reason(&self) -> Option<&str> {
match self {
Self::Pass => None,
Self::Fail(why) | Self::NotTested(why) => Some(why),
}
}
pub fn of(evidence: impl IntoIterator<Item = Evidence>, none: &str) -> Self {
let (mut held, mut broken, mut missing) = (0usize, Vec::new(), Vec::new());
for item in evidence {
match item {
Evidence::Held => held += 1,
Evidence::Broken(why) => push_unique(&mut broken, why),
Evidence::Unexercised(why) => push_unique(&mut missing, why),
}
}
if !broken.is_empty() {
Self::Fail(broken.join("; "))
} else if !missing.is_empty() {
Self::NotTested(missing.join("; "))
} else if held == 0 {
Self::NotTested(none.to_owned())
} else {
Self::Pass
}
}
}
fn push_unique(list: &mut Vec<String>, item: String) {
if !list.contains(&item) {
list.push(item);
}
}