use super::documents::{declare_document, DocTree, TagRule, Vocabulary};
use super::element::{attributes, elements, fn_name, rust_name};
use super::{Declared, Family};
pub use super::{fragment, json, El, Json, Value};
elements! {
scout: children;
configuration: leaf;
workload: children;
case: leaf;
judge: leaf;
replacement: children;
layer: children;
capability: children;
evidence: leaf;
decision: leaf;
differentiation: leaf;
economics: leaf;
}
attributes! {
pub trait ScoutsAttrs {
title; budget; repeats; margin; relative_margin; error_floor; min_calls; min_cases; resamples; alpha; seed;
max_cases; readme;
id; model; harness; effort; instructions; max_tokens; incumbent;
needs; sample; context;
from_; prompt; system; check; rubric; tools; turns; source; terms;
product; vendor; url; as_of; by_; kind;
need; have; run; labour; ref_; note; claim; choice; because; text; price; customers;
}
}
pub(crate) const FAMILY: Family = Family { module: "scouts", roots: &["scout", "replacement"], declare };
pub const SCOUT_FIELDS: &[&str] = &[
"title", "budget", "repeats", "margin", "relativeMargin", "errorFloor", "minCalls", "minCases", "resamples", "alpha", "seed", "maxCases", "readme",
];
pub const CONFIGURATION_FIELDS: &[&str] = &["id", "model", "harness", "effort", "instructions", "maxTokens", "incumbent"];
pub const WORKLOAD_FIELDS: &[&str] = &["id", "title", "needs", "sample", "context"];
pub const CASE_FIELDS: &[&str] = &["id", "from", "prompt", "system", "check", "rubric", "tools", "turns", "source", "terms"];
pub const JUDGE_FIELDS: &[&str] = &["model", "effort"];
pub const CASE_ORIGINS: &[&str] = &["public", "synthetic", "own-run"];
pub const WORKLOAD_NEEDS: &[&str] = &["tools", "structured", "image"];
pub const CHECK_KINDS: &[&str] = &["exact", "oneOf", "number", "contains", "regex", "json", "letter", "literal", "tool", "program", "kernel"];
pub const SOLID_MEASURES: &[&str] = &["volume", "bbox", "bodies", "holes", "mass", "gap", "gaps", "faces"];
pub const CIRCUIT_MEASURES: &[&str] = &["parts", "nets", "connections"];
pub const REPLACEMENT_FIELDS: &[&str] = &["product", "vendor", "url", "asOf", "by", "readme"];
pub const LAYER_FIELDS: &[&str] = &["kind", "title"];
pub const CAPABILITY_FIELDS: &[&str] = &["id", "title", "need", "have", "effort", "run", "labour", "ref", "note"];
pub const EVIDENCE_FIELDS: &[&str] = &["source", "claim"];
pub const DECISION_FIELDS: &[&str] = &["choice", "because"];
pub const ECONOMICS_FIELDS: &[&str] = &["price", "customers", "margin", "note"];
pub const LAYER_KINDS: &[&str] = &["visible", "invisible", "integrations", "operations", "economics"];
pub const CAPABILITY_NEEDS: &[&str] = &["must", "should", "later"];
pub const CAPABILITY_HAVES: &[&str] = &["built", "partial", "none"];
pub const CAPABILITY_EFFORTS: &[&str] = &["S", "M", "L", "XL"];
pub const EVIDENCE_CLAIMS: &[&str] = &["verified", "checked", "reported"];
pub const DECISION_CHOICES: &[&str] = &["build", "buy", "integrate", "skip"];
const SCOUT: Vocabulary = Vocabulary {
format: "scout",
noun: "a scout's comparison",
root: "scout",
tags: &[
TagRule::new("scout", &[]).required(&["budget"]).attrs(SCOUT_FIELDS),
TagRule::new("configuration", &["scout"]).key("id").required(&["id", "model"]).attrs(CONFIGURATION_FIELDS),
TagRule::new("workload", &["scout"]).key("id").required(&["id"]).attrs(WORKLOAD_FIELDS),
TagRule::new("case", &["workload"]).key("id").required(&["id", "from", "prompt"]).attrs(CASE_FIELDS),
TagRule::new("judge", &["scout"]).key("model").required(&["model"]).attrs(JUDGE_FIELDS),
],
from_tree: scout_of,
};
const REPLACEMENT: Vocabulary = Vocabulary {
format: "replacement",
noun: "a replacement-cost analysis",
root: "replacement",
tags: &[
TagRule::new("replacement", &[]).required(&["product", "asOf"]).attrs(REPLACEMENT_FIELDS),
TagRule::new("layer", &["replacement"]).key("title").required(&["kind", "title"]).attrs(LAYER_FIELDS),
TagRule::new("capability", &["layer"]).key("id").required(&["id", "title", "need", "have"]).attrs(CAPABILITY_FIELDS),
TagRule::new("evidence", &["capability"]).key("source").required(&["source", "claim"]).attrs(EVIDENCE_FIELDS),
TagRule::new("decision", &["replacement"]).single().required(&["choice", "because"]).attrs(DECISION_FIELDS),
TagRule::new("differentiation", &["replacement"]).single().required(&["text"]).attrs(&["text"]),
TagRule::new("economics", &["replacement"]).single().attrs(ECONOMICS_FIELDS),
],
from_tree: replacement_of,
};
fn declare(root: El) -> Result<Declared, String> {
declare_document(&root, if root.tag == "scout" { &SCOUT } else { &REPLACEMENT })
}
fn call(t: &DocTree) -> String {
match t.attr_of("id") {
Some(v @ Json::Str(_)) => format!("{}().id({})", fn_name(t.tag), v.text()),
_ => format!("{}()", fn_name(t.tag)),
}
}
fn said(v: Option<&Json>) -> String {
v.map(Json::text).unwrap_or_else(|| "undefined".into())
}
fn text(t: &DocTree, k: &str) -> Result<(), String> {
match t.attr_of(k) {
Some(Json::Str(s)) if !s.is_empty() => Ok(()),
Some(v) => Err(format!("{} {} is text, not {}", call(t), rust_name(k), v.text())),
None => Ok(()),
}
}
fn one_of(t: &DocTree, k: &str, allowed: &[&str]) -> Result<(), String> {
match t.attr_of(k) {
Some(Json::Str(s)) if allowed.contains(&s.as_str()) => Ok(()),
Some(v) => Err(format!("{} {} is one of {}, not {}", call(t), rust_name(k), allowed.join(", "), v.text())),
None => Ok(()),
}
}
fn positive(t: &DocTree, k: &str, integer: bool, zero: bool) -> Result<(), String> {
match t.attr_of(k) {
Some(Json::Num(n)) if n.is_finite() && (if zero { *n >= 0.0 } else { *n > 0.0 }) && (!integer || n.fract() == 0.0) => Ok(()),
Some(v) => Err(format!(
"{} {} is a {}number{}, not {}",
call(t),
rust_name(k),
if integer { "whole " } else { "" },
if zero { " of 0 or more" } else { " above 0" },
v.text()
)),
None => Ok(()),
}
}
fn is_number(v: &Json) -> bool {
matches!(v, Json::Num(n) if n.is_finite())
}
fn check_of(t: &DocTree) -> Result<(), String> {
let Some(c) = t.attr_of("check") else { return Ok(()) };
let Json::Obj(entries) = c else {
return Err(format!("{} check is an object ({{ exact: \"42\" }}), not {}", call(t), c.text()));
};
let kinds: Vec<&str> = entries.iter().map(|(k, _)| k.as_str()).filter(|k| *k != "tolerance").collect();
if kinds.len() != 1 || !CHECK_KINDS.contains(&kinds[0]) {
return Err(format!(
"{} check is exactly one of {}{}",
call(t),
CHECK_KINDS.join(", "),
if kinds.is_empty() { String::new() } else { format!(" (it has {})", kinds.join(" and ")) }
));
}
let kind = kinds[0];
if c.get("tolerance").is_some() && kind != "number" {
return Err(format!("{} check: tolerance goes with number", call(t)));
}
if let Some(n) = c.get("number") {
let tolerance_ok = c.get("tolerance").is_none_or(|x| matches!(x, Json::Num(n) if *n >= 0.0));
if !is_number(n) || !tolerance_ok {
return Err(format!("{} check: number and tolerance are numbers", call(t)));
}
}
if let Some(v) = c.get("exact").filter(|v| !matches!(v, Json::Str(_))) {
return Err(format!("{} check: exact is text, not {}", call(t), v.text()));
}
for k in ["oneOf", "contains"] {
if let Some(v) = c.get(k) {
let ok = matches!(v, Json::Arr(items) if !items.is_empty() && items.iter().all(|s| matches!(s, Json::Str(s) if !s.is_empty())));
if !ok {
return Err(format!("{} check: {} is a list of texts", call(t), k));
}
}
}
if let Some(v) = c.get("regex").filter(|v| !matches!(v, Json::Str(s) if !s.is_empty())) {
return Err(format!("{} check: regex is text, not {}", call(t), v.text()));
}
if let Some(v) = c.get("json").filter(|v| !matches!(v, Json::Obj(_))) {
return Err(format!("{} check: json is the object the answer must hold, not {}", call(t), v.text()));
}
if let Some(v) = c.get("letter") {
let ok = matches!(v, Json::Str(s) if s.len() == 1 && s.chars().next().is_some_and(|c| c.is_ascii_uppercase()));
if !ok {
return Err(format!("{} check: letter is one capital letter, not {}", call(t), v.text()));
}
}
for k in ["literal", "tool"] {
if let Some(v) = c.get(k).filter(|v| !matches!(v, Json::Str(s) if !s.is_empty())) {
return Err(format!("{} check: {} is text, not {}", call(t), k, v.text()));
}
}
if c.get("tool").is_some() && t.attr_of("tools").is_none() {
return Err(format!("{} check: tool needs the case's tools", call(t)));
}
if let Some(p) = c.get("program") {
let Json::Obj(_) = p else {
return Err(format!("{} check: program is {{ data: [[x, y], …] }}, not {}", call(t), p.text()));
};
let data_ok = matches!(p.get("data"), Some(Json::Arr(rows)) if !rows.is_empty() && rows.iter().all(|r| {
matches!(r, Json::Arr(pair) if pair.len() == 2 && pair.iter().all(is_number))
}));
if !data_ok {
return Err(format!("{} check: program is {{ data: [[x, y], …] }}", call(t)));
}
if let Some(v) = p.get("tolerance").filter(|v| !matches!(v, Json::Num(n) if *n >= 0.0)) {
return Err(format!("{} check: program tolerance is a number of 0 or more, not {}", call(t), v.text()));
}
}
if let Some(k) = c.get("kernel") {
kernel_check(t, k)?;
}
Ok(())
}
fn kernel_file(name: &str) -> Option<bool> {
if name.contains('/') {
return None;
}
let lower = name.to_ascii_lowercase();
let ext = [".tsx", ".jsx", ".py", ".rs"].into_iter().find(|e| lower.ends_with(e))?;
let stem = &lower[..lower.len() - ext.len()];
if stem.len() > 3 && stem.ends_with(".3d") {
Some(true)
} else if stem.len() > 4 && stem.ends_with(".sch") {
Some(false)
} else {
None
}
}
const SIZE_FORM: &str = "a size is a number, { nominal, upper, lower }, { nominal, class } or { min, max }";
const HOLE_FORM: &str = "holes is a count, or a list of groups { count, diameter, through?, depth?, axis?, at?, position? }";
const GAPS_FORM: &str = "gaps is a list of { between: [body, body], gap }";
const FACES_FORM: &str = "faces is a list of { name?, between: [face, face], distance }, a face being { body?, at: [x, y, z], normal? }";
fn is_count(v: &Json) -> bool {
matches!(v, Json::Num(n) if *n >= 0.0 && n.fract() == 0.0)
}
fn is_point(v: &Json) -> bool {
matches!(v, Json::Arr(xs) if xs.len() == 3 && xs.iter().all(is_number))
}
fn is_direction(v: &Json) -> bool {
is_point(v) && matches!(v, Json::Arr(xs) if xs.iter().any(|x| !matches!(x, Json::Num(n) if *n == 0.0)))
}
fn is_text(v: &Json) -> bool {
matches!(v, Json::Str(s) if !s.is_empty())
}
fn only_fields(v: &Json, names: &[&str]) -> bool {
matches!(v, Json::Obj(entries) if entries.iter().all(|(k, _)| names.contains(&k.as_str())))
}
fn size_problem(v: &Json) -> Option<String> {
if let Json::Num(n) = v {
return if n.is_finite() && *n >= 0.0 { None } else { Some("a size is not below 0".into()) };
}
let Json::Obj(entries) = v else { return Some(SIZE_FORM.into()) };
if let Some((key, _)) = entries.iter().find(|(k, _)| !["nominal", "upper", "lower", "class", "min", "max"].contains(&k.as_str())) {
return Some(format!("a size has no field {key} (nominal, upper, lower, class, min, max)"));
}
let number_field = |k: &str| v.get(k).map(|x| matches!(x, Json::Num(n) if n.is_finite()));
if let Some(nominal) = v.get("nominal") {
if v.get("min").is_some() || v.get("max").is_some() {
return Some("a size is a nominal with deviations or a class, or a range, not both".into());
}
let Json::Num(nominal) = nominal else { return Some(SIZE_FORM.into()) };
if !nominal.is_finite() || *nominal < 0.0 {
return Some(SIZE_FORM.into());
}
if let Some(class) = v.get("class") {
if v.get("upper").is_some() || v.get("lower").is_some() {
return Some("give the class or the deviations, not both".into());
}
return super::tolerances::tolerance_limits(*nominal, &Json::obj().with("class", class.clone())).err();
}
for k in ["upper", "lower"] {
if number_field(k) == Some(false) {
return Some(format!("{k} is a number of millimetres"));
}
}
if let (Some(Json::Num(u)), Some(Json::Num(l))) = (v.get("upper"), v.get("lower")) {
if u < l {
return Some("the upper deviation is below the lower".into());
}
}
return None;
}
if v.get("class").is_some() || v.get("upper").is_some() || v.get("lower").is_some() {
return Some("deviations and a class go with a nominal".into());
}
if v.get("min").is_none() && v.get("max").is_none() {
return Some(SIZE_FORM.into());
}
for k in ["min", "max"] {
if number_field(k) == Some(false) {
return Some(format!("{k} is a number of millimetres"));
}
}
if let (Some(Json::Num(lo)), Some(Json::Num(hi))) = (v.get("min"), v.get("max")) {
if lo > hi {
return Some("the range's min is above its max".into());
}
}
None
}
fn kernel_check(t: &DocTree, k: &Json) -> Result<(), String> {
let solid = match (k, k.get("as")) {
(Json::Obj(_), Some(Json::Str(name))) => kernel_file(name),
_ => None,
};
let Some(solid) = solid else {
return Err(format!("{} check: kernel is {{ as: \"bracket.3d.tsx\" | \"divider.sch.tsx\", expect }} — a solid or a schematic, one file", call(t)));
};
if let Json::Obj(entries) = k {
if let Some((key, _)) = entries.iter().find(|(key, _)| !["as", "expect", "tolerance", "oriented"].contains(&key.as_str())) {
return Err(format!("{} check: kernel has no {}", call(t), key));
}
}
let allowed = if solid { SOLID_MEASURES } else { CIRCUIT_MEASURES };
let Some(Json::Obj(expect)) = k.get("expect").filter(|e| matches!(e, Json::Obj(x) if !x.is_empty())) else {
return Err(format!("{} check: kernel expect names at least one of {}", call(t), allowed.join(", ")));
};
let refuse = |message: String| -> Result<(), String> { Err(format!("{} check: kernel {}", call(t), message)) };
let size = |what: String, v: Option<&Json>| -> Result<(), String> {
match v.map_or(Some(SIZE_FORM.to_string()), size_problem) {
Some(problem) => refuse(format!("{what}: {problem}")),
None => Ok(()),
}
};
for (key, v) in expect {
if !allowed.contains(&key.as_str()) {
return Err(format!("{} check: a {} has no measure {} ({})", call(t), if solid { "solid" } else { "schematic" }, key, allowed.join(", ")));
}
if ["bodies", "parts", "nets"].contains(&key.as_str()) && !is_count(v) {
return refuse(format!("{key} is a count"));
}
if key == "holes" {
match v {
Json::Arr(groups) if !groups.is_empty() => {
for (i, h) in groups.iter().enumerate() {
let at = format!("holes[{i}]");
if !matches!(h, Json::Obj(_)) {
return refuse(HOLE_FORM.into());
}
if let Json::Obj(fields) = h {
if let Some((f, _)) = fields.iter().find(|(f, _)| !["count", "diameter", "through", "depth", "axis", "at", "position"].contains(&f.as_str())) {
return refuse(format!("{at} has no field {f}"));
}
}
if !h.get("count").is_some_and(is_count) {
return refuse(format!("{at} count is a count"));
}
if h.get("diameter").is_none() {
return refuse(format!("{at} needs a diameter"));
}
size(format!("{at} diameter"), h.get("diameter"))?;
if h.get("depth").is_some() {
size(format!("{at} depth"), h.get("depth"))?;
}
if h.get("through").is_some_and(|x| !matches!(x, Json::Bool(_))) {
return refuse(format!("{at} through is true or false"));
}
if h.get("axis").is_some_and(|x| !is_direction(x)) {
return refuse(format!("{at} axis is a direction [x, y, z]"));
}
if h.get("at").is_some_and(|x| !matches!(x, Json::Arr(ps) if !ps.is_empty() && ps.iter().all(is_point))) {
return refuse(format!("{at} at is a list of points [x, y, z]"));
}
if h.get("position").is_some_and(|x| !matches!(x, Json::Num(n) if *n > 0.0 && n.is_finite())) {
return refuse(format!("{at} position is mm above 0"));
}
}
}
_ if is_count(v) => {}
_ => return refuse(HOLE_FORM.into()),
}
}
if key == "volume" {
match v {
Json::Num(n) if !(*n > 0.0 && n.is_finite()) => return refuse("volume is mm³, more than 0".into()),
Json::Num(_) => {}
other => size("volume".into(), Some(other))?,
}
}
if key == "bbox" {
let Json::Arr(sides) = v else { return refuse("bbox is [x, y, z] mm".into()) };
if sides.len() != 3 {
return refuse("bbox is [x, y, z] mm".into());
}
for (i, s) in sides.iter().enumerate() {
size(format!("bbox[{i}]"), Some(s))?;
}
}
if key == "mass" {
let positive = |f: &str| matches!(v.get(f), Some(Json::Num(n)) if *n > 0.0 && n.is_finite());
let signed = |f: &str| v.get(f).is_none_or(is_number);
if !(only_fields(v, &["nominal", "upper", "lower", "density"]) && positive("nominal") && positive("density") && signed("upper") && signed("lower")) {
return refuse("mass is { nominal, upper, lower, density } in grams and g/cm³".into());
}
}
if key == "gap" {
size("gap".into(), Some(v))?;
}
if key == "gaps" {
let Json::Arr(pairs) = v else { return refuse(GAPS_FORM.into()) };
if pairs.is_empty() {
return refuse(GAPS_FORM.into());
}
for (i, g) in pairs.iter().enumerate() {
let ok = only_fields(g, &["between", "gap"])
&& matches!(g.get("between"), Some(Json::Arr(names)) if names.len() == 2 && names.iter().all(is_text) && names[0] != names[1]);
if !ok {
return refuse(GAPS_FORM.into());
}
size(format!("gaps[{i}] gap"), g.get("gap"))?;
}
}
if key == "faces" {
let Json::Arr(rows) = v else { return refuse(FACES_FORM.into()) };
if rows.is_empty() {
return refuse(FACES_FORM.into());
}
let face = |s: &Json| {
only_fields(s, &["body", "at", "normal"])
&& s.get("at").is_some_and(is_point)
&& s.get("body").is_none_or(is_text)
&& s.get("normal").is_none_or(is_direction)
};
for (i, f) in rows.iter().enumerate() {
let ok = only_fields(f, &["name", "between", "distance"])
&& f.get("name").is_none_or(is_text)
&& matches!(f.get("between"), Some(Json::Arr(ends)) if ends.len() == 2 && ends.iter().all(face));
if !ok {
return refuse(FACES_FORM.into());
}
size(format!("faces[{i}] distance"), f.get("distance"))?;
}
}
let pin = |p: &Json| matches!(p, Json::Str(s) if s.split('.').count() == 2 && s.split('.').all(|x| !x.is_empty() && !x.contains(char::is_whitespace)));
if key == "connections" && !matches!(v, Json::Arr(gs) if !gs.is_empty() && gs.iter().all(|g| matches!(g, Json::Arr(ps) if ps.len() >= 2 && ps.iter().all(pin)))) {
return Err(format!("{} check: kernel connections is a list of pin groups ([[\"R1.2\", \"R2.1\"]]), each on one net", call(t)));
}
}
if let Some(v) = k.get("tolerance").filter(|v| !matches!(v, Json::Num(n) if *n >= 0.0 && *n < 1.0)) {
return Err(format!("{} check: kernel tolerance is a share, 0 to under 1, not {}", call(t), v.text()));
}
if k.get("oriented").is_some_and(|v| !matches!(v, Json::Bool(_)) || !solid) {
return Err(format!("{} check: kernel oriented is true or false, on a solid", call(t)));
}
Ok(())
}
fn tools_of(t: &DocTree) -> Result<(), String> {
let Some(tools) = t.attr_of("tools") else { return Ok(()) };
let Json::Arr(items) = tools else {
return Err(format!("{} tools is a list of {{ name, parameters, results }}, not {}", call(t), tools.text()));
};
if items.is_empty() {
return Err(format!("{} tools is a list of {{ name, parameters, results }}", call(t)));
}
let mut names: Vec<&str> = Vec::new();
for tool in items {
let Json::Obj(_) = tool else {
return Err(format!("{} a tool is {{ name, parameters, results }}, not {}", call(t), tool.text()));
};
let name = match tool.get("name") {
Some(Json::Str(s)) if !s.is_empty() && s.len() <= 64 && s.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-') => s.as_str(),
_ => return Err(format!("{} a tool's name is a word of letters, digits, _ and -", call(t))),
};
if names.contains(&name) {
return Err(format!("{} has two tools named {}", call(t), name));
}
names.push(name);
if !matches!(tool.get("parameters"), Some(Json::Obj(_))) {
return Err(format!("{} tool {}: parameters is a JSON Schema object", call(t), name));
}
if let Some(Json::Arr(results)) = tool.get("results") {
for r in results {
let ok = matches!(r, Json::Obj(_))
&& matches!(r.get("result"), Some(Json::Str(_)))
&& r.get("when").is_none_or(|w| matches!(w, Json::Obj(entries) if entries.iter().all(|(_, v)| matches!(v, Json::Str(_)))));
if !ok {
return Err(format!(
"{} tool {}: a result is {{ when?: {{ <argument>: <text it contains> }}, result: <text> }}",
call(t),
name
));
}
}
} else if tool.get("results").is_some() {
return Err(format!("{} tool {}: results is a list", call(t), name));
}
}
positive(t, "turns", true, false)
}
fn case_of(t: &DocTree) -> Result<Json, String> {
for k in ["id", "prompt", "system", "rubric", "source"] {
text(t, k)?;
}
one_of(t, "from", CASE_ORIGINS)?;
check_of(t)?;
tools_of(t)?;
if t.attr_of("turns").is_some() && t.attr_of("tools").is_none() {
return Err(format!("{} turns goes with tools", call(t)));
}
if t.attr_of("check").is_some() == t.attr_of("rubric").is_some() {
return Err(format!("{} is scored by exactly one of check (a program) or rubric (a judge panel)", call(t)));
}
if let Some(v) = t.attr_of("terms").filter(|v| !matches!(v, Json::Arr(items) if items.iter().all(|s| matches!(s, Json::Str(s) if !s.is_empty())))) {
return Err(format!("{} terms is a list of term ids, not {}", call(t), v.text()));
}
Ok(fields(t, CASE_FIELDS))
}
fn configuration_of(c: &DocTree) -> Result<Json, String> {
for k in ["id", "model", "harness", "effort", "instructions"] {
text(c, k)?;
}
positive(c, "maxTokens", true, false)?;
if let Some(v) = c.attr_of("incumbent").filter(|v| !matches!(v, Json::Bool(_))) {
return Err(format!("{} incumbent is true or false, not {}", call(c), v.text()));
}
Ok(fields(c, CONFIGURATION_FIELDS))
}
fn workload_of(w: &DocTree) -> Result<Json, String> {
text(w, "id")?;
text(w, "title")?;
positive(w, "sample", true, false)?;
positive(w, "context", true, false)?;
if let Some(v) = w.attr_of("needs").filter(|v| {
!matches!(v, Json::Arr(items) if items.iter().all(|n| matches!(n, Json::Str(s) if WORKLOAD_NEEDS.contains(&s.as_str()))))
}) {
return Err(format!("{} needs is a list of {}, not {}", call(w), WORKLOAD_NEEDS.join(", "), v.text()));
}
let cases = w.children.iter().map(case_of).collect::<Result<Vec<_>, _>>()?;
Ok(fields(w, WORKLOAD_FIELDS).with("cases", Json::Arr(cases)))
}
fn judge_of(j: &DocTree) -> Result<Json, String> {
text(j, "model")?;
text(j, "effort")?;
Ok(fields(j, JUDGE_FIELDS))
}
fn scout_of(t: &DocTree) -> Result<Json, String> {
text(t, "title")?;
text(t, "readme")?;
positive(t, "budget", false, false)?;
for k in ["repeats", "minCalls", "minCases", "resamples", "maxCases"] {
positive(t, k, true, false)?;
}
for k in ["margin", "relativeMargin", "errorFloor"] {
positive(t, k, false, true)?;
}
positive(t, "alpha", false, false)?;
if let Some(a) = t.attr_of("alpha").and_then(Json::as_f64).filter(|a| *a >= 1.0) {
return Err(format!("<scout> alpha is below 1, not {}", a));
}
if let Some(v) = t.attr_of("seed").filter(|v| !matches!(v, Json::Num(n) if n.fract() == 0.0 && n.abs() <= 9_007_199_254_740_991.0)) {
return Err(format!("<scout> seed is a whole number, not {}", v.text()));
}
let configurations = t.children.iter().filter(|c| c.tag == "configuration").map(configuration_of).collect::<Result<Vec<_>, _>>()?;
let incumbents = configurations.iter().filter(|c| matches!(c.get("incumbent"), Some(Json::Bool(true)))).count();
if incumbents > 1 {
return Err("<scout> has one incumbent configuration".into());
}
let workloads = t.children.iter().filter(|w| w.tag == "workload").map(workload_of).collect::<Result<Vec<_>, _>>()?;
let judges = t.children.iter().filter(|j| j.tag == "judge").map(judge_of).collect::<Result<Vec<_>, _>>()?;
Ok(fields(t, SCOUT_FIELDS)
.with("configurations", Json::Arr(configurations))
.with("workloads", Json::Arr(workloads))
.with("judges", Json::Arr(judges)))
}
fn capability_of(c: &DocTree) -> Result<Json, String> {
for k in ["id", "title", "ref", "note"] {
text(c, k)?;
}
one_of(c, "need", CAPABILITY_NEEDS)?;
one_of(c, "have", CAPABILITY_HAVES)?;
one_of(c, "effort", CAPABILITY_EFFORTS)?;
positive(c, "run", false, true)?;
positive(c, "labour", false, true)?;
let evidence = c
.children
.iter()
.filter(|e| e.tag == "evidence")
.map(|e| {
text(e, "source")?;
one_of(e, "claim", EVIDENCE_CLAIMS)?;
Ok(fields(e, EVIDENCE_FIELDS))
})
.collect::<Result<Vec<_>, String>>()?;
Ok(fields(c, CAPABILITY_FIELDS).with("evidence", Json::Arr(evidence)))
}
fn replacement_of(t: &DocTree) -> Result<Json, String> {
for k in ["product", "vendor", "url", "by", "readme"] {
text(t, k)?;
}
let as_of_ok = matches!(t.attr_of("asOf"), Some(Json::Str(s)) if {
let b = s.as_bytes();
b.len() == 10 && b[4] == b'-' && b[7] == b'-' && b.iter().enumerate().all(|(i, c)| i == 4 || i == 7 || c.is_ascii_digit())
});
if !as_of_ok {
return Err(format!("<replacement> asOf is a day (2026-10-14), not {}", said(t.attr_of("asOf"))));
}
let layers = t
.children
.iter()
.filter(|l| l.tag == "layer")
.map(|l| {
one_of(l, "kind", LAYER_KINDS)?;
text(l, "title")?;
let capabilities = l.children.iter().filter(|c| c.tag == "capability").map(capability_of).collect::<Result<Vec<_>, _>>()?;
Ok(fields(l, LAYER_FIELDS).with("capabilities", Json::Arr(capabilities)))
})
.collect::<Result<Vec<_>, String>>()?;
let mut doc = fields(t, REPLACEMENT_FIELDS).with("layers", Json::Arr(layers));
if let Some(d) = t.single("decision") {
one_of(d, "choice", DECISION_CHOICES)?;
text(d, "because")?;
doc.set("decision", fields(d, DECISION_FIELDS));
}
if let Some(d) = t.single("differentiation") {
text(d, "text")?;
if let Some(v) = d.attr_of("text") {
doc.set("differentiation", v.clone());
}
}
if let Some(e) = t.single("economics") {
for k in ECONOMICS_FIELDS {
text(e, k)?;
}
doc.set("economics", fields(e, ECONOMICS_FIELDS));
}
Ok(doc)
}
fn fields(t: &DocTree, keys: &[&str]) -> Json {
let mut out = Json::obj();
for k in keys {
if let Some(v) = t.attr_of(k) {
out.set(k, v.clone());
}
}
out
}