use std::collections::HashMap;
use crate::{Finding, RepoPath, Spanned, YamlError};
pub const STAMPED_MANIFEST_KEYS: &[&str] = &["producer", "generated", "counts"];
pub const SOKF_VERSION: &str = "0.6";
#[derive(Debug, Clone, Default, PartialEq, Eq)]
pub struct Manifest {
pub sokf: Option<String>,
pub name: Option<String>,
pub scope: Option<String>,
pub renamed_from: Vec<(String, usize)>,
pub description: Option<String>,
pub unique: Option<String>,
pub extends: Option<String>,
pub stamped_present: Vec<(String, usize)>,
pub line_of: HashMap<String, usize>,
pub problems: Vec<Finding>,
}
impl Manifest {
pub fn capped(&self) -> bool {
self.unique.as_deref() == Some("knowledge")
}
}
pub fn manifest(yaml: Result<Spanned, YamlError>, path: &RepoPath) -> Manifest {
let mut m = Manifest::default();
let node = match yaml {
Ok(n) => n,
Err(e) => {
m.problems.push(Finding::error(
path.clone(),
Some(e.line()),
"SOKF_FORMAT_8",
format!("manifest: {e}"),
));
return m;
}
};
let Some(entries) = node.as_map() else {
m.problems.push(Finding::error(
path.clone(),
Some(node.line),
"SOKF_FORMAT_8",
"the manifest must be a map",
));
return m;
};
for (key, value) in entries {
m.line_of.insert(key.value.clone(), key.line);
match key.value.as_str() {
"sokf" => m.sokf = value.scalar_text(),
"name" => m.name = value.as_str().map(str::to_string),
"scope" => m.scope = value.as_str().map(str::to_string),
"renamed-from" => {
let Some(items) = value.as_seq() else {
m.problems.push(Finding::error(
path.clone(),
Some(value.line),
"SOKF_FORMAT_11",
"`renamed-from` must be a list of `<scope>/<name>` entries",
));
continue;
};
for item in items {
match item.as_str() {
Some(old) => m.renamed_from.push((old.to_string(), item.line)),
None => m.problems.push(Finding::error(
path.clone(),
Some(item.line),
"SOKF_FORMAT_11",
"each `renamed-from` entry must be a `<scope>/<name>` string",
)),
}
}
}
"description" => m.description = value.as_str().map(str::to_string),
"unique" => m.unique = Some(value.scalar_text().unwrap_or_default()),
"extends" => m.extends = Some(value.scalar_text().unwrap_or_default()),
k if STAMPED_MANIFEST_KEYS.contains(&k) => {
m.stamped_present.push((k.to_string(), key.line));
}
_ => {}
}
}
m
}
#[cfg(test)]
mod tests {
use super::*;
use crate::yaml;
#[test]
fn manifest_parses_and_notes_stamped_keys() {
let path = RepoPath::parse("/k/manifest.sokf.yaml").unwrap();
let m = manifest(
yaml::parse(
"sokf: \"0.6\"\nname: n\nscope: test\nrenamed-from: [test/old, 1]\ndescription: d\ngenerated: x\n",
1,
),
&path,
);
assert_eq!(m.sokf.as_deref(), Some("0.6"));
assert_eq!(m.name.as_deref(), Some("n"));
assert_eq!(m.scope.as_deref(), Some("test"));
assert_eq!(m.renamed_from, vec![("test/old".to_string(), 4)]);
assert_eq!(m.problems.len(), 1, "{:?}", m.problems);
assert_eq!(m.stamped_present, vec![("generated".to_string(), 6)]);
assert_eq!(m.line_of["name"], 2);
let m = manifest(yaml::parse("sokf: 0.6\n", 1), &path);
assert_eq!(m.sokf.as_deref(), Some("0.6"));
let m = manifest(yaml::parse("renamed-from: test/old\n", 1), &path);
assert_eq!(m.renamed_from, [] as [(std::string::String, usize); 0]);
assert_eq!(m.problems.len(), 1, "{:?}", m.problems);
assert_eq!(m.problems[0].criterion, "SOKF_FORMAT_11");
assert_eq!(m.problems[0].line, Some(1));
assert!(
manifest(yaml::parse("- x\n", 1), &path).problems[0]
.message
.contains("must be a map")
);
assert!(
manifest(yaml::parse("a: &x 1\n", 1), &path).problems[0]
.message
.contains("anchor")
);
}
}