use std::{collections::HashMap, sync::OnceLock};
use regex::Regex;
use serde::{Deserialize, Serialize};
use crate::{Finding, RepoPath, Spanned, Yaml, YamlError, markdown::is_url};
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Status {
Draft,
Stable,
Deprecated,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum Resource {
One(String),
List(Vec<String>),
}
impl Resource {
pub fn entries(&self) -> &[String] {
match self {
Resource::One(r) => std::slice::from_ref(r),
Resource::List(rs) => rs,
}
}
pub fn one(&self) -> Option<&str> {
match self {
Resource::One(r) => Some(r),
Resource::List(_) => None,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SourceEntry {
pub resource: String,
pub id: Option<String>,
pub title: Option<String>,
pub line: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct LinkEntry {
pub rel: String,
pub to: String,
pub note: Option<String>,
pub line: usize,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct VerifiedEntry {
pub by: String,
pub at: String,
pub line: usize,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct CoreFields {
pub type_: Option<String>,
pub id: Option<String>,
pub title: Option<String>,
pub description: Option<String>,
pub tags: Option<Vec<String>>,
pub resource: Option<Resource>,
pub status: Option<Status>,
pub sources: Vec<SourceEntry>,
pub links: Vec<LinkEntry>,
pub verified: Vec<VerifiedEntry>,
pub extra: Vec<(String, Spanned)>,
pub lines: HashMap<String, usize>,
pub problems: Vec<Finding>,
pub present: bool,
}
pub const CORE_KEYS: &[&str] = &[
"type",
"id",
"title",
"description",
"tags",
"resource",
"status",
"sources",
"links",
"verified",
];
pub const STAMPED_DOCUMENT_KEYS: &[&str] = &["generated"];
impl CoreFields {
pub fn extra(&self, key: &str) -> Option<&Spanned> {
self.extra.iter().find(|(k, _)| k == key).map(|(_, v)| v)
}
pub fn line_of(&self, key: &str) -> Option<usize> {
self.lines.get(key).copied()
}
fn fail(
&mut self,
path: &RepoPath,
line: Option<usize>,
criterion: &'static str,
msg: impl Into<String>,
) {
self.problems
.push(Finding::error(path.clone(), line, criterion, msg));
}
}
fn iso_datetime() -> &'static Regex {
static ISO: OnceLock<Regex> = OnceLock::new();
ISO.get_or_init(|| {
Regex::new(r"^\d{4}-\d{2}-\d{2}(T\d{2}:\d{2}(:\d{2}(\.\d+)?)?(Z|[+-]\d{2}:?\d{2})?)?$")
.expect("literal")
})
}
pub fn core_fields(yaml: Option<Result<&Spanned, &YamlError>>, path: &RepoPath) -> CoreFields {
let mut core = CoreFields::default();
let node = match yaml {
None => {
core.fail(
path,
None,
"SOKF_FORMAT_17",
"the document has no frontmatter",
);
return core;
}
Some(Err(e)) => {
core.fail(
path,
Some(e.line()),
"SOKF_FORMAT_18",
format!("frontmatter: {e}"),
);
return core;
}
Some(Ok(node)) => node,
};
let Some(entries) = node.as_map() else {
core.fail(
path,
Some(node.line),
"SOKF_FORMAT_18",
format!("the frontmatter must be a map, not a {}", node.type_name()),
);
return core;
};
core.present = true;
for (key, value) in entries {
core.lines.insert(key.value.clone(), key.line);
let line = Some(value.line);
match key.value.as_str() {
"type" => match value.as_str() {
Some(s) if !s.trim().is_empty() => core.type_ = Some(s.to_string()),
Some(_) => core.fail(path, line, "SOKF_FORMAT_19", "`type` is empty"),
None => core.fail(path, line, "SOKF_FORMAT_19", "`type` must be a string"),
},
"id" => {
if let Some(s) = value.scalar_text() {
core.id = Some(s);
}
if value.as_str().is_none() {
core.fail(path, line, "SOKF_FORMAT_32", "`id` must be a string");
}
}
"title" => match value.as_str() {
Some(s) => core.title = Some(s.to_string()),
None => core.fail(path, line, "SOKF_FORMAT_26", "`title` must be a string"),
},
"description" => match value.as_str() {
Some(s) => core.description = Some(s.to_string()),
None => core.fail(
path,
line,
"SOKF_FORMAT_26",
"`description` must be a string",
),
},
"tags" => match value.as_seq().map(|items| {
items
.iter()
.map(|i| i.as_str().map(str::to_string))
.collect::<Option<Vec<_>>>()
}) {
Some(Some(tags)) => core.tags = Some(tags),
_ => core.fail(
path,
line,
"SOKF_FORMAT_27",
"`tags` must be a list of strings",
),
},
"resource" => match resource(value) {
Some(r) => core.resource = Some(r),
None => core.fail(
path,
line,
"SOKF_FORMAT_29",
"`resource` must be a `/` path or a URL, or a list of them",
),
},
"status" => match value.as_str() {
Some("draft") => core.status = Some(Status::Draft),
Some("stable") => core.status = Some(Status::Stable),
Some("deprecated") => core.status = Some(Status::Deprecated),
_ => core.fail(
path,
line,
"SOKF_FORMAT_28",
"`status` must be `draft`, `stable`, or `deprecated`",
),
},
"sources" => sources(&mut core, value, path),
"links" => links(&mut core, value, path),
"verified" => verified(&mut core, value, path),
_ => core.extra.push((key.value.clone(), value.clone())),
}
}
core
}
fn resource(value: &Spanned) -> Option<Resource> {
let valid = |s: &str| RepoPath::parse(s).is_some() || is_url(s);
if let Some(s) = value.as_str() {
return valid(s).then(|| Resource::One(s.to_string()));
}
let entries = value
.as_seq()?
.iter()
.map(|i| i.as_str().filter(|s| valid(s)).map(str::to_string))
.collect::<Option<Vec<_>>>()?;
(!entries.is_empty()).then_some(Resource::List(entries))
}
struct EntryRules {
field: &'static str,
allowed: &'static [&'static str],
allowed_text: &'static str,
shape: &'static str,
unknown: &'static str,
not_string: fn(&str) -> &'static str,
}
fn each_entry(
core: &mut CoreFields,
value: &Spanned,
path: &RepoPath,
rules: &EntryRules,
mut add: impl FnMut(&mut CoreFields, &Spanned, bool),
) {
let field = rules.field;
let Some(items) = value.as_seq() else {
core.fail(
path,
Some(value.line),
rules.shape,
format!("`{field}` must be a list of maps"),
);
return;
};
for item in items {
let Some(entries) = item.as_map() else {
core.fail(
path,
Some(item.line),
rules.shape,
format!("each `{field}` entry must be a map"),
);
continue;
};
let mut ok = true;
for (k, v) in entries {
if !rules.allowed.contains(&k.value.as_str()) {
core.fail(
path,
Some(k.line),
rules.unknown,
format!(
"a `{field}` entry has `{}`. Only {} are allowed",
k.value, rules.allowed_text
),
);
ok = false;
} else if v.as_str().is_none() {
core.fail(
path,
Some(v.line),
(rules.not_string)(&k.value),
format!("`{field}[].{}` must be a string", k.value),
);
ok = false;
}
}
add(core, item, ok);
}
}
fn text_of(item: &Spanned, key: &str) -> Option<String> {
item.get(key).and_then(|v| v.as_str()).map(str::to_string)
}
fn sources(core: &mut CoreFields, value: &Spanned, path: &RepoPath) {
let rules = EntryRules {
field: "sources",
allowed: &["resource", "id", "title"],
allowed_text: "`resource`, `id`, and `title`",
shape: "SOKF_FORMAT_38",
unknown: "SOKF_FORMAT_38",
not_string: |k| {
if k == "resource" {
"SOKF_FORMAT_37"
} else {
"SOKF_FORMAT_38"
}
},
};
each_entry(core, value, path, &rules, |core, item, ok| {
let Some(resource) = text_of(item, "resource") else {
core.fail(
path,
Some(item.line),
"SOKF_FORMAT_37",
"a `sources` entry has no `resource`",
);
return;
};
if ok {
core.sources.push(SourceEntry {
resource,
id: text_of(item, "id"),
title: text_of(item, "title"),
line: item.line,
});
}
});
}
fn links(core: &mut CoreFields, value: &Spanned, path: &RepoPath) {
let rules = EntryRules {
field: "links",
allowed: &["rel", "to", "note"],
allowed_text: "`rel`, `to`, and `note`",
shape: "SOKF_FORMAT_50",
unknown: "SOKF_FORMAT_51",
not_string: |k| {
if k == "note" {
"SOKF_FORMAT_51"
} else {
"SOKF_FORMAT_50"
}
},
};
each_entry(core, value, path, &rules, |core, item, ok| {
let (Some(rel), Some(to)) = (text_of(item, "rel"), text_of(item, "to")) else {
core.fail(
path,
Some(item.line),
"SOKF_FORMAT_50",
"a `links` entry needs `rel` and `to`",
);
return;
};
if ok {
core.links.push(LinkEntry {
rel,
to,
note: text_of(item, "note"),
line: item.line,
});
}
});
}
fn verified(core: &mut CoreFields, value: &Spanned, path: &RepoPath) {
let items: Vec<&Spanned> = match &value.value {
Yaml::Seq(items) => items.iter().collect(),
Yaml::Map(_) => vec![value],
_ => {
core.fail(
path,
Some(value.line),
"SOKF_FORMAT_42",
"`verified` must be a list of `{ by, at }` maps",
);
return;
}
};
for item in items {
let line = Some(item.line);
let by = item.get("by").and_then(|b| b.as_str());
let at = item.get("at").and_then(|a| a.scalar_text());
let (Some(by), Some(at)) = (by, at) else {
let key = if by.is_none() {
"SOKF_FORMAT_43"
} else {
"SOKF_FORMAT_44"
};
core.fail(path, line, key, "a `verified` entry needs `by` and `at`");
continue;
};
if !(by.starts_with("human:") || by.starts_with("process:")) {
core.fail(
path,
line,
"SOKF_FORMAT_43",
"`verified[].by` must be a `human:` or `process:` actor",
);
continue;
}
if !iso_datetime().is_match(&at) {
core.fail(
path,
line,
"SOKF_FORMAT_44",
"`verified[].at` must be an ISO 8601 datetime",
);
continue;
}
core.verified.push(VerifiedEntry {
by: by.to_string(),
at,
line: item.line,
});
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::yaml;
fn fields(text: &str) -> CoreFields {
let parsed = yaml::parse(text, 2);
core_fields(Some(parsed.as_ref()), &RepoPath::parse("/k/a.md").unwrap())
}
#[test]
fn parses_every_core_field() {
let c = fields(
"type: Subsystem\nid: subsystem-logging\ntitle: Logging\ndescription: d\nresource: /crates/x.rs\ntags: [core, logging]\nstatus: draft\nsources:\n - id: log4rs\n resource: https://crates.io/log4rs\n title: docs\nlinks:\n - rel: depends-on\n to: config\n note: n\nverified:\n - { by: human:rs, at: 2026-08-04T09:00:00Z }\nextra: 1\n",
);
assert!(c.problems.is_empty(), "{:?}", c.problems);
assert!(c.present);
assert_eq!(c.type_.as_deref(), Some("Subsystem"));
assert_eq!(c.id.as_deref(), Some("subsystem-logging"));
assert_eq!(
c.tags.as_ref().unwrap(),
&vec!["core".to_string(), "logging".to_string()]
);
assert_eq!(c.status, Some(Status::Draft));
assert_eq!(c.sources[0].id.as_deref(), Some("log4rs"));
assert_eq!(c.links[0].to, "config");
assert_eq!(c.verified[0].by, "human:rs");
assert_eq!(c.extra("extra").unwrap().as_int(), Some(1));
assert_eq!(c.line_of("id"), Some(3));
assert!(c.extra("nope").is_none());
assert_eq!(c.resource, Some(Resource::One("/crates/x.rs".into())));
let bare = fields("verified: { by: process:x, at: 2026-01-01 }\n");
assert_eq!(bare.verified.len(), 1);
}
#[test]
fn each_bad_shape_is_one_finding_naming_the_field() {
let c = fields(
"type: [x]\nid: 3\ntitle: 1\ndescription: []\ntags: x\nresource: relative\nstatus: nope\nsources:\n - resource: /x\n extra: 1\n - id: only\n - 3\nlinks:\n - rel: x\n - to: 1\n rel: r\n - rel: r\n to: t\n other: 1\nverified:\n - { by: agent/1, at: 2026-01-01 }\n - { by: human:x, at: soon }\n - { by: human:x }\n",
);
let messages: Vec<&str> = c.problems.iter().map(|f| f.message.as_str()).collect();
for needle in [
"`type` must be a string",
"`id` must be a string",
"`title` must be a string",
"`description` must be a string",
"`tags` must be a list of strings",
"`resource` must be a `/` path or a URL, or a list of them",
"`status` must be",
"has `extra`",
"no `resource`",
"each `sources` entry must be a map",
"needs `rel` and `to`",
"`links[].to` must be a string",
"has `other`",
"`human:` or `process:`",
"ISO 8601",
"needs `by` and `at`",
] {
assert!(
messages.iter().any(|m| m.contains(needle)),
"missing {needle:?} in {messages:?}"
);
}
assert_eq!(c.id.as_deref(), Some("3"));
assert!(c.sources.is_empty());
assert!(c.links.is_empty());
assert!(c.verified.is_empty());
let e = fields("type: ''\nsources: 1\nlinks: 1\nverified: 1\n");
assert!(
e.problems
.iter()
.any(|f| f.message.contains("`type` is empty"))
);
assert!(
e.problems
.iter()
.any(|f| f.message.contains("`sources` must be a list"))
);
assert!(
e.problems
.iter()
.any(|f| f.message.contains("`links` must be a list"))
);
assert!(
e.problems
.iter()
.any(|f| f.message.contains("`verified` must be a list"))
);
}
#[test]
fn a_resource_is_one_or_a_list() {
let list = fields("resource: [/a, https://x.y]\n");
let r = list.resource.unwrap();
assert_eq!(r.entries(), ["/a", "https://x.y"]);
assert_eq!(r.one(), None);
assert_eq!(Resource::One("/a".into()).entries(), ["/a"]);
for bad in [
"resource: []\n",
"resource: [/a, rel]\n",
"resource: [[/a]]\n",
] {
assert!(fields(bad).resource.is_none(), "{bad}");
assert_eq!(fields(bad).problems.len(), 1, "{bad}");
}
}
#[test]
fn missing_and_broken_frontmatter() {
let path = RepoPath::parse("/k/a.md").unwrap();
let none = core_fields(None, &path);
assert!(!none.present);
assert!(none.problems[0].message.contains("no frontmatter"));
let err = YamlError::Anchor { line: 3 };
let bad = core_fields(Some(Err(&err)), &path);
assert_eq!(bad.problems[0].line, Some(3));
let list = fields("- a\n");
assert!(list.problems[0].message.contains("must be a map"));
}
proptest::proptest! {
#![proptest_config(crate::proptest_cases::cases(256))]
#[test]
fn anchors_aliases_and_tags_are_errors_on_their_line(
before in 0usize..4,
name in "[a-z][a-z0-9]{0,5}",
) {
let pad: String = (0..before).map(|i| format!("k{i}: v\n")).collect();
let line = before + 1;
proptest::prop_assert_eq!(
yaml::parse(&format!("{pad}a: &{name} 1\n"), 1).unwrap_err(),
YamlError::Anchor { line }
);
proptest::prop_assert_eq!(
yaml::parse(&format!("{pad}a: *{name}\n"), 1).unwrap_err(),
YamlError::Alias { line }
);
proptest::prop_assert_eq!(
yaml::parse(&format!("{pad}a: !{name} 1\n"), 1).unwrap_err(),
YamlError::Tag { line }
);
}
#[test]
fn plain_scalars_follow_the_core_schema(
n in proptest::prelude::any::<i64>(),
b in proptest::prelude::any::<bool>(),
s in "[a-z][a-z ]{0,10}[a-z]",
) {
let get = |text: String| yaml::parse(&text, 1).unwrap().get("v").cloned().unwrap();
proptest::prop_assert_eq!(get(format!("v: {n}\n")).as_int(), Some(n));
proptest::prop_assert_eq!(get(format!("v: {b}\n")).as_bool(), Some(b));
let plain = get(format!("v: {s}\n"));
proptest::prop_assert_eq!(plain.as_str(), Some(s.as_str()));
let quoted = get(format!("v: \"{n}\"\n"));
let text = n.to_string();
proptest::prop_assert_eq!(quoted.as_str(), Some(text.as_str()));
}
}
}