use super::documents::{declare_document, DocTree, TagRule, Vocabulary};
use super::element::{attributes, elements, rust_name};
use super::{Declared, Family};
pub use super::{fragment, json, El, Json, Value};
elements! {
letter: children;
postcard: children;
to: leaf;
from: leaf;
front: leaf;
paragraph: leaf;
}
attributes! {
pub trait PostalAttrs {
color; double_sided; mail_class; size;
name; company; line1; line2; city; region; postal_code; country;
image; text;
}
}
pub(crate) const FAMILY: Family = Family { module: "postal", roots: &["letter", "postcard"], declare };
pub const ADDRESS_FIELDS: &[&str] = &["name", "company", "line1", "line2", "city", "region", "postalCode", "country"];
pub const LETTER_FIELDS: &[&str] = &["color", "doubleSided", "mailClass"];
pub const POSTCARD_SIZES: &[&str] = &["4x6", "6x9", "6x11"];
pub const POSTCARD_FIELDS: &[&str] = &["size"];
const LETTER: Vocabulary = Vocabulary {
format: "letter",
noun: "a letter",
root: "letter",
tags: &[
TagRule::new("letter", &[]).attrs(LETTER_FIELDS),
TagRule::new("to", &["letter"]).single().attrs(ADDRESS_FIELDS),
TagRule::new("from", &["letter"]).single().attrs(ADDRESS_FIELDS),
TagRule::new("paragraph", &["letter"]).required(&["text"]).attrs(&["text"]),
],
from_tree: letter_of,
};
const POSTCARD: Vocabulary = Vocabulary {
format: "postcard",
noun: "a postcard",
root: "postcard",
tags: &[
TagRule::new("postcard", &[]).attrs(POSTCARD_FIELDS),
TagRule::new("to", &["postcard"]).single().attrs(ADDRESS_FIELDS),
TagRule::new("from", &["postcard"]).single().attrs(ADDRESS_FIELDS),
TagRule::new("front", &["postcard"]).single().required(&["image"]).attrs(&["image"]),
TagRule::new("paragraph", &["postcard"]).required(&["text"]).attrs(&["text"]),
],
from_tree: postcard_of,
};
fn declare(root: El) -> Result<Declared, String> {
declare_document(&root, if root.tag == "letter" { &LETTER } else { &POSTCARD })
}
fn check(t: &DocTree) -> Result<(), String> {
for k in ["color", "doubleSided"] {
if matches!(t.attr_of(k), Some(v) if !matches!(v, Json::Bool(_))) {
return Err(format!("letter(): {} is true or false", rust_name(k)));
}
}
if matches!(t.attr_of("mailClass"), Some(v) if *v != Json::from("first") && *v != Json::from("standard")) {
return Err(r#"letter(): mail_class is "first" or "standard""#.into());
}
for c in &t.children {
if c.tag == "paragraph" && !matches!(c.attr_of("text"), Some(Json::Str(_))) {
return Err("paragraph(): text is text".into());
}
if c.tag == "to" || c.tag == "from" {
if let Some((k, _)) = c.attrs.iter().find(|(_, v)| !matches!(v, Json::Str(_))) {
return Err(format!("{}(): {} is text", c.tag, rust_name(k)));
}
}
}
Ok(())
}
fn piece_of(t: &DocTree, mut out: Json) -> Result<Json, String> {
check(t)?;
for side in ["to", "from"] {
if let Some(a) = t.single(side) {
out.set(side, a.record());
}
}
if let Some(f) = t.single("front") {
match f.attr_of("image") {
Some(image @ Json::Str(_)) => {
out.set("front", image.clone());
}
_ => return Err("front(): image is the ref of a JPEG or PNG".into()),
}
}
let paragraphs: Vec<Json> = t.children.iter().filter(|c| c.tag == "paragraph").filter_map(|c| c.attr_of("text").cloned()).collect();
Ok(out.with("paragraphs", paragraphs))
}
fn letter_of(t: &DocTree) -> Result<Json, String> {
let mut head = Json::obj();
for k in LETTER_FIELDS {
if let Some(v) = t.attr_of(k).filter(|v| **v != Json::from("")) {
head.set(k, v.clone());
}
}
piece_of(t, head)
}
fn postcard_of(t: &DocTree) -> Result<Json, String> {
let mut head = Json::obj();
if let Some(size) = t.attr_of("size") {
let said = match size {
Json::Str(s) => POSTCARD_SIZES.iter().any(|p| p == s),
_ => false,
};
if !said {
let sizes: Vec<String> = POSTCARD_SIZES.iter().map(|s| format!("\"{s}\"")).collect();
return Err(format!("postcard(): size is {}, not {size:?}", sizes.join(", ")));
}
head.set("size", size.clone());
}
piece_of(t, head)
}