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! {
mounts: children;
mount: leaf;
}
attributes! {
pub trait MountsAttrs {
label; task; folder; project; assignee; labels; status;
}
}
pub(crate) const FAMILY: Family = Family { module: "mounts", roots: &["mounts"], declare };
pub const MOUNT_FIELDS: &[&str] = &["label", "task", "folder", "project", "assignee", "labels", "status"];
pub const MOUNT_STATUSES: &[&str] = &["proposed", "todo", "doing", "blocked", "done", "cancelled", "declined"];
const MOUNTS: Vocabulary = Vocabulary {
format: "mounts",
noun: "a thread's mounted tasks",
root: "mounts",
tags: &[
TagRule::new("mounts", &[]).attrs(&[]),
TagRule::new("mount", &["mounts"]).key("label").required(&["label"]).attrs(MOUNT_FIELDS),
],
from_tree: mounts_of,
};
fn declare(root: El) -> Result<Declared, String> {
declare_document(&root, &MOUNTS)
}
fn js_len(s: &str) -> usize {
s.encode_utf16().count()
}
fn is_text(v: &Json, max: usize) -> bool {
matches!(v, Json::Str(s) if !s.trim().is_empty() && js_len(s) <= max)
}
fn is_texts(v: &Json) -> bool {
matches!(v, Json::Arr(items) if items.iter().all(|x| is_text(x, 200)))
}
fn assignee_ok(v: &Json) -> bool {
let Json::Str(s) = v else { return false };
let id = s.strip_prefix("user:").or_else(|| s.strip_prefix("agent:"));
matches!(id, Some(id) if !id.is_empty() && !id.contains(|c: char| c == ':' || c.is_whitespace()))
}
pub fn mount_of(t: &DocTree) -> Result<Json, String> {
let label = t.attr_of("label");
let Some(Json::Str(name)) = label.filter(|v| is_text(v, 200)) else {
let said = label.map(Json::text).unwrap_or_else(|| "undefined".into());
return Err(format!("mount(): label is a short text that names it in its thread, not {said}"));
};
let at = format!("mount().label({})", Json::Str(name.clone()).text());
for k in ["task", "folder", "project", "assignee"] {
if let Some(v) = t.attr_of(k).filter(|v| !is_text(v, 1000)) {
return Err(format!("{at} {k} is a text (a ref or an address), not {}", v.text()));
}
}
if t.attr_of("labels").is_some_and(|v| !is_texts(v)) {
return Err(format!("{at} labels is a list of texts"));
}
if let Some(v) = t.attr_of("status") {
let known = matches!(v, Json::Arr(items) if items.iter().all(|s| matches!(s, Json::Str(s) if MOUNT_STATUSES.contains(&s.as_str()))));
if !is_texts(v) || !known {
return Err(format!("{at} status is a list of {}", MOUNT_STATUSES.join(", ")));
}
}
if let Some(v) = t.attr_of("assignee").filter(|v| !assignee_ok(v)) {
return Err(format!("{at} assignee is user:<id> or agent:<id>, not {}", v.text()));
}
let query = ["folder", "project", "assignee", "labels", "status"].iter().any(|k| t.attr_of(k).is_some());
match (t.attr_of("task").is_some(), query) {
(true, true) => return Err(format!("{at} mounts one task, or a list (folder, project, assignee, labels, status): not both")),
(false, false) => return Err(format!("{at} mounts nothing: give task, folder, project, or a query (assignee, labels, status)")),
_ => {}
}
let mut out = Json::obj();
for k in MOUNT_FIELDS {
match t.attr_of(k) {
Some(Json::Arr(items)) if items.is_empty() => {}
Some(v) => {
out.set(k, v.clone());
}
None => {}
}
}
Ok(out)
}
fn mounts_of(t: &DocTree) -> Result<Json, String> {
let mut mounts = Vec::new();
let mut seen: Vec<String> = Vec::new();
for c in t.children.iter().filter(|c| c.tag == "mount") {
let m = mount_of(c)?;
let label = m.get("label").and_then(Json::as_str).unwrap_or_default().to_string();
if seen.contains(&label) {
return Err(format!("two mounts are labelled {}: a label names one mount in its thread", Json::Str(label).text()));
}
seen.push(label);
mounts.push(m);
}
Ok(Json::obj().with("mounts", Json::Arr(mounts)))
}