use super::documents::{declare_document, DocTree, TagRule, Vocabulary};
use super::element::{attributes, elements};
use super::{Declared, Family};
pub use super::{fragment, json, El, Json, Value};
elements! {
firmware: children;
supports: leaf;
rollout: children;
health: leaf;
target: leaf;
}
attributes! {
pub trait FirmwareAttrs {
name;
version; image;
sha256;
size; sbom; device; security_version; title; note; revision;
firmware;
world; channel; canary; waves; settle_ms;
id; command; read; min; max; equals; unit; timeout_ms;
}
}
pub(crate) const FAMILY: Family = Family { module: "firmware", roots: &["firmware", "rollout"], declare };
pub const FIRMWARE_FIELDS: &[&str] = &["name", "version", "image", "sha256", "size", "sbom", "device", "securityVersion", "title", "note"];
pub const FIRMWARE_RELEASE_FIELDS: &[&str] = &["version", "image", "sha256", "sbom", "device"];
pub const ROLLOUT_FIELDS: &[&str] = &["firmware", "world", "channel", "canary", "waves", "settleMs", "title", "note"];
pub const HEALTH_FIELDS: &[&str] = &["id", "channel", "command", "read", "min", "max", "equals", "unit", "timeoutMs"];
const FIRMWARE: Vocabulary = Vocabulary {
format: "firmware",
noun: "a firmware",
root: "firmware",
tags: &[
TagRule::new("firmware", &[]).attrs(FIRMWARE_FIELDS),
TagRule::new("supports", &["firmware"]).key("revision").required(&["revision"]).attrs(&["revision"]),
TagRule::new("channel", &["firmware"]).key("id").required(&["id"]),
],
from_tree: firmware_of,
};
const ROLLOUT: Vocabulary = Vocabulary {
format: "rollout",
noun: "a firmware rollout",
root: "rollout",
tags: &[
TagRule::new("rollout", &[]).required(&["firmware", "world", "channel", "canary", "waves"]).attrs(ROLLOUT_FIELDS),
TagRule::new("health", &["rollout"]).key("id").required(&["id", "channel", "read"]).attrs(HEALTH_FIELDS),
TagRule::new("target", &["rollout"]).key("unit").required(&["unit"]).attrs(&["unit"]),
],
from_tree: rollout_of,
};
fn declare(root: El) -> Result<Declared, String> {
declare_document(&root, if root.tag == "firmware" { &FIRMWARE } else { &ROLLOUT })
}
fn list_of(t: &DocTree, tag: &str) -> Vec<Json> {
t.children.iter().filter(|c| c.tag == tag).map(DocTree::record).collect()
}
fn stated(v: Option<&Json>) -> bool {
!matches!(v, None | Some(Json::Null)) && !matches!(v, Some(Json::Str(s)) if s.is_empty()) && !matches!(v, Some(Json::Arr(a)) if a.is_empty())
}
fn firmware_of(t: &DocTree) -> Result<Json, String> {
if !FIRMWARE_RELEASE_FIELDS.iter().any(|k| stated(t.attr_of(k))) {
if !stated(t.attr_of("name")) {
return Err("firmware(): a firmware's declaration states its name; a release states version, image, sha256, sbom and device".into());
}
return Ok(with_children(t));
}
let missing: Vec<&str> = FIRMWARE_RELEASE_FIELDS.iter().copied().filter(|k| !stated(t.attr_of(k))).collect();
if !missing.is_empty() {
return Err(format!("firmware() needs {}: a release states all of {}", missing.join(", "), FIRMWARE_RELEASE_FIELDS.join(", ")));
}
let sha = t.attr_of("sha256");
if !matches!(sha, Some(Json::Str(s)) if s.len() == 64 && s.bytes().all(|b| b.is_ascii_digit() || (b'a'..=b'f').contains(&b))) {
let said = sha.map(Json::text).unwrap_or_else(|| "undefined".into());
return Err(format!("firmware() sha256 is the image's SHA-256 in 64 lowercase hex digits, not {said}"));
}
Ok(with_children(t))
}
fn with_children(t: &DocTree) -> Json {
let mut out = t.record();
let (supports, channels) = (list_of(t, "supports"), list_of(t, "channel"));
if !supports.is_empty() {
out.set("supports", Json::Arr(supports));
}
if !channels.is_empty() {
out.set("channels", Json::Arr(channels));
}
out
}
fn rollout_of(t: &DocTree) -> Result<Json, String> {
let mut out = t.record();
let (health, targets) = (list_of(t, "health"), list_of(t, "target"));
if !health.is_empty() {
out.set("health", Json::Arr(health));
}
if !targets.is_empty() {
out.set("targets", Json::Arr(targets));
}
Ok(out)
}