use super::element::{fn_name, rust_name, Child, El};
use super::json::Json;
use super::Declared;
#[derive(Clone, Debug)]
pub struct DocTree {
pub tag: &'static str,
pub attrs: Vec<(String, Json)>,
pub children: Vec<DocTree>,
pub source: Option<Json>,
}
impl DocTree {
pub fn attr_of(&self, name: &str) -> Option<&Json> {
self.attrs.iter().find(|(k, _)| k == name).map(|(_, v)| v)
}
pub fn record(&self) -> Json {
Json::Obj(self.attrs.clone())
}
pub fn single(&self, tag: &str) -> Option<&DocTree> {
self.children.iter().find(|c| c.tag == tag)
}
}
pub struct TagRule {
pub tag: &'static str,
pub parents: &'static [&'static str],
pub key: Option<&'static str>,
pub single: bool,
pub required: &'static [&'static str],
pub attrs: Option<&'static [&'static str]>,
}
impl TagRule {
pub const fn new(tag: &'static str, parents: &'static [&'static str]) -> TagRule {
TagRule { tag, parents, key: None, single: false, required: &[], attrs: None }
}
pub const fn key(mut self, key: &'static str) -> TagRule {
self.key = Some(key);
self
}
pub const fn single(mut self) -> TagRule {
self.single = true;
self
}
pub const fn required(mut self, required: &'static [&'static str]) -> TagRule {
self.required = required;
self
}
pub const fn attrs(mut self, attrs: &'static [&'static str]) -> TagRule {
self.attrs = Some(attrs);
self
}
}
pub struct Vocabulary {
pub format: &'static str,
pub noun: &'static str,
pub root: &'static str,
pub tags: &'static [TagRule],
pub from_tree: fn(&DocTree) -> Result<Json, String>,
}
impl Vocabulary {
pub fn rule(&self, tag: &str) -> Option<&TagRule> {
self.tags.iter().find(|r| r.tag == tag)
}
}
fn call(el: &El, key: Option<&str>) -> String {
match key.and_then(|k| el.attr_of(k).map(|v| (k, v))) {
Some((k, v @ Json::Str(_))) => format!("{}().{}({})", fn_name(el.tag), rust_name(k), v.text()),
_ => format!("{}()", fn_name(el.tag)),
}
}
fn plain(v: &Json, what: &str) -> Result<(), String> {
match v {
Json::Num(n) if !n.is_finite() => Err(format!("{what} is {n}, not a finite number")),
Json::Arr(items) => items.iter().enumerate().try_for_each(|(i, x)| plain(x, &format!("{what}[{i}]"))),
Json::Obj(entries) => entries.iter().try_for_each(|(k, x)| plain(x, &format!("{what}.{k}"))),
_ => Ok(()),
}
}
pub fn js_string(v: Option<&Json>) -> String {
match v {
None => "undefined".into(),
Some(Json::Str(s)) => s.clone(),
Some(Json::Arr(items)) => items.iter().map(|x| if *x == Json::Null { String::new() } else { js_string(Some(x)) }).collect::<Vec<_>>().join(","),
Some(Json::Obj(_)) => "[object Object]".into(),
Some(other) => other.text(),
}
}
pub fn tree_of_element(root: &El, v: &Vocabulary) -> Result<DocTree, String> {
fn visit(el: &El, parent: Option<&'static str>, v: &Vocabulary) -> Result<DocTree, String> {
let Some(rule) = v.rule(el.tag) else {
let tags: Vec<String> = v.tags.iter().map(|r| format!("{}()", fn_name(r.tag))).collect();
return Err(format!("{}() is not an element of {} ({})", fn_name(el.tag), v.noun, tags.join(", ")));
};
match parent {
None if !rule.parents.is_empty() => return Err(format!("{} in code is one {}() element, not {}()", v.noun, fn_name(v.root), fn_name(el.tag))),
Some(p) if !rule.parents.contains(&p) => {
let ps: Vec<String> = rule.parents.iter().map(|p| format!("{}()", fn_name(p))).collect();
return Err(format!("{}() stands in {}, not in {}()", fn_name(el.tag), ps.join(" or "), fn_name(p)));
}
_ => {}
}
let at = call(el, rule.key);
for (k, value) in &el.attrs {
if let Some(allowed) = rule.attrs {
if !allowed.contains(&k.as_str()) {
let names: Vec<String> = allowed.iter().map(|a| rust_name(a)).collect();
return Err(format!("{at}: {} is not read ({})", rust_name(k), names.join(", ")));
}
}
plain(value, &format!("{at} {}", rust_name(k)))?;
}
for k in rule.required {
if el.attr_of(k).is_none() {
return Err(format!("{at} needs {}", rust_name(k)));
}
}
for c in &el.children {
if let Child::Text(t) = c {
if !t.trim().is_empty() {
return Err(format!("{at}: text is not read ({:?})", t.trim().chars().take(40).collect::<String>()));
}
}
}
let children = el.child_elements().map(|c| visit(c, Some(el.tag), v)).collect::<Result<Vec<_>, _>>()?;
let mut seen: Vec<String> = Vec::new();
for c in &children {
let r = v.rule(c.tag).expect("a child's tag was checked");
let id = match (r.key.filter(|key| c.attr_of(key).is_some()), r.single) {
(Some(key), _) => format!("{}#{}", c.tag, js_string(c.attr_of(key))),
(None, true) => c.tag.to_string(),
_ => continue,
};
if seen.contains(&id) {
return Err(match r.key.filter(|key| c.attr_of(key).is_some()) {
Some(key) => format!("two {}() in {}() have {} {:?}", fn_name(c.tag), fn_name(el.tag), rust_name(key), js_string(c.attr_of(key))),
None => format!("{}() has one {}()", fn_name(el.tag), fn_name(c.tag)),
});
}
seen.push(id);
}
Ok(DocTree { tag: el.tag, attrs: el.attrs.clone(), children, source: el.source_json() })
}
visit(root, None, v)
}
pub fn identities(children: &[DocTree], v: &Vocabulary) -> Vec<String> {
let mut nth: Vec<(&str, usize)> = Vec::new();
children
.iter()
.map(|c| {
let r = v.rule(c.tag);
if let Some(k) = r.and_then(|r| r.key).and_then(|key| c.attr_of(key)) {
return format!("{}#{}", c.tag, k.as_str().map(str::to_string).unwrap_or_else(|| k.text()));
}
let i = match nth.iter_mut().find(|(t, _)| *t == c.tag) {
Some(entry) => {
entry.1 += 1;
entry.1 - 1
}
None => {
nth.push((c.tag, 1));
0
}
};
if r.is_some_and(|r| r.single) {
c.tag.to_string()
} else {
format!("{}@{i}", c.tag)
}
})
.collect()
}
pub fn tree_paths(tree: &DocTree, v: &Vocabulary, visit: &mut dyn FnMut(&DocTree, &str)) {
fn walk(t: &DocTree, path: &str, v: &Vocabulary, visit: &mut dyn FnMut(&DocTree, &str)) {
visit(t, path);
let ids = identities(&t.children, v);
for (c, id) in t.children.iter().zip(ids) {
let p = if path.is_empty() { id } else { format!("{path}/{id}") };
walk(c, &p, v, visit);
}
}
walk(tree, "", v, visit);
}
pub fn declare_document(root: &El, v: &Vocabulary) -> Result<Declared, String> {
let tree = tree_of_element(root, v)?;
let document = (v.from_tree)(&tree)?;
let mut sources = Json::obj();
tree_paths(&tree, v, &mut |t, path| {
if let Some(s) = &t.source {
sources.set(path, s.clone());
}
});
Ok(Declared::Document { format: v.format.to_string(), document, sources })
}