use crate::check::context::Ctx;
use crate::check::finding::Finding;
pub(super) fn ids(ctx: &Ctx, list: &[usize]) -> Vec<String> {
list.iter().map(|&i| ctx.corpus.docs[i].id.to_string()).collect()
}
pub(super) fn remote_problem(ctx: &Ctx, text: &str, rel: Option<(&str, &str)>) -> Option<Option<String>> {
use crate::workspace::remotes::Remote;
Some(match crate::workspace::remotes::resolve(&ctx.corpus.cfg, text) {
Remote::Local => return None,
Remote::Absent { .. } => None,
Remote::Found { ty, .. } => match rel.map(|(kind, own)| (allowed_targets(kind), kind, own)) {
Some((Some([]), kind, own)) if ty != own => Some(format!("is a {ty}; rel.{kind} needs a {own}")),
Some((Some(list), kind, _)) if !list.is_empty() && !list.contains(&ty.as_str()) => {
Some(format!("is a {ty}, which rel.{kind} cannot point at"))
}
_ => None,
},
Remote::Missing { name, id } => Some(format!("names no document in the remote {name} ({id})")),
Remote::MissingAnchor { name, id, anchor } => Some(format!("{name}:{id} has no anchor #{anchor}")),
Remote::Ambiguous { name, ids } => Some(format!("is ambiguous in the remote {name}: {}", ids.join(", "))),
})
}
pub(super) fn remote_status(
ctx: &Ctx,
d: &crate::model::Doc,
line: usize,
kind: &str,
t: &str,
out: &mut Vec<Finding>,
) {
if !ctx.corpus.cfg.raw.check.remote_status || kind == "supersedes" {
return;
}
let Some((status, by)) = crate::workspace::remotes::status(&ctx.corpus.cfg, t) else { return };
if !matches!(status.as_str(), "superseded" | "archived" | "rejected") {
return;
}
let next = match by {
Some(new) => format!("next: spec set {} rel.{kind}-={t} rel.{kind}+={new} --write", d.id),
None => format!("next: spec set {} rel.{kind}-={t} --write", d.id),
};
out.push(Finding::warn(
"L3",
&d.path,
line,
format!("rel.{kind} target {t} is {status} in its repository; {next}"),
));
}
pub(super) fn allowed_targets(kind: &str) -> Option<&'static [&'static str]> {
match kind {
"supersedes" => Some(&[]),
"implements" => Some(&["sot", "adr", "page", "design"]),
"evidence" => Some(&["evidence", "research", "audit"]),
_ => None,
}
}