use crate::scanner::{Attribute, AttributeValue, Token};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SerializeError {
TagName(String),
AttributeName(String),
}
impl core::fmt::Display for SerializeError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Self::TagName(name) => {
write!(f, "tag name `{name}` is not a valid AML name")
}
Self::AttributeName(name) => {
write!(f, "attribute name `{name}` is not a valid AML name")
}
}
}
}
impl core::error::Error for SerializeError {}
fn is_valid_name(name: &str) -> bool {
!name.is_empty()
&& name
.chars()
.all(|ch| ch.is_ascii_lowercase() || ch.is_ascii_digit() || ch == '-' || ch == '_')
}
fn push_text(text: &str, out: &mut String) {
for ch in text.chars() {
match ch {
'[' => out.push_str("[["),
']' => out.push_str("]]"),
_ => out.push(ch),
}
}
}
fn push_attr_value(value: &str, out: &mut String) {
for ch in value.chars() {
match ch {
'\\' => out.push_str("\\\\"),
'"' => out.push_str("\\\""),
'\n' => out.push_str("\\n"),
'\t' => out.push_str("\\t"),
_ => out.push(ch),
}
}
}
fn push_attribute(attr: &Attribute, out: &mut String) -> Result<(), SerializeError> {
if !is_valid_name(&attr.name) {
return Err(SerializeError::AttributeName(attr.name.clone()));
}
out.push(' ');
out.push_str(&attr.name);
match &attr.value {
AttributeValue::Flag => {}
AttributeValue::String(value) | AttributeValue::Ident(value) => {
out.push_str("=\"");
push_attr_value(value, out);
out.push('"');
}
}
Ok(())
}
pub fn to_aml(tokens: &[Token]) -> Result<String, SerializeError> {
let mut out = String::new();
for token in tokens {
match token {
Token::Text(text) => push_text(text, &mut out),
Token::OpenTag {
name,
attributes,
self_closing,
} => {
if !is_valid_name(name) {
return Err(SerializeError::TagName(name.clone()));
}
out.push('[');
out.push_str(name);
for attr in attributes {
push_attribute(attr, &mut out)?;
}
if *self_closing {
out.push_str(" /]");
} else {
out.push(']');
}
}
Token::CloseTag { name } => {
if !is_valid_name(name) {
return Err(SerializeError::TagName(name.clone()));
}
out.push_str("[/");
out.push_str(name);
out.push(']');
}
Token::Eof => {}
}
}
Ok(out)
}
#[cfg(test)]
mod tests {
use super::*;
use crate::scanner::Scanner;
fn scan(src: &str) -> Vec<Token> {
Scanner::new(src.as_bytes())
.expect("scanner rejected input")
.scan_all()
.expect("scan failed")
}
fn normalize(tokens: &[Token]) -> Vec<Token> {
let mut out: Vec<Token> = Vec::new();
for token in tokens {
match (out.last_mut(), token) {
(_, Token::Eof) => {}
(_, Token::Text(text)) if text.is_empty() => {}
(Some(Token::Text(prev)), Token::Text(text)) => prev.push_str(text),
_ => out.push(quote_values(token)),
}
}
out
}
fn quote_values(token: &Token) -> Token {
let mut token = token.clone();
if let Token::OpenTag { attributes, .. } = &mut token {
for attr in attributes {
if let AttributeValue::Ident(value) = &attr.value {
attr.value = AttributeValue::String(value.clone());
}
}
}
token
}
fn assert_round_trips(src: &str) {
let tokens = normalize(&scan(src));
let aml = to_aml(&tokens).expect("serialization failed");
let reparsed = normalize(&scan(&aml));
assert_eq!(
tokens, reparsed,
"round trip changed the token stream\n source: {src:?}\n emitted: {aml:?}"
);
}
#[test]
fn round_trips_every_construct() {
for src in [
"[page mode=document][text]hello[/text][/page]",
"[page mode=document][spacer lines=1 /][/page]",
"[page mode=document][text bold dim fg=red]x[/text][/page]",
r#"[page mode=document][link href="/a?b=c&d=e"]go[/link][/page]"#,
"[page mode=document][text]a [text bold]b[/text] c[/text][/page]",
"[page mode=document][hr style=single /][/page]",
r#"[page mode=document][input name="q" maxlen=20 password /][/page]"#,
"[page mode=document][box border=rounded][text]in[/text][/box][/page]",
] {
assert_round_trips(src);
}
}
#[test]
fn round_trips_metacharacters_in_both_contexts() {
assert_round_trips(r#"[page mode=document][text]a[[b]]c$d\e[/text][/page]"#);
assert_round_trips(r#"[page mode=document][link href="a\"b\\c"]t[/link][/page]"#);
assert_round_trips("[page mode=document][text]trailing backslash \\[/text][/page]");
assert_round_trips("[page mode=document][text]$$literal$dollar[/text][/page]");
}
#[test]
fn text_that_looks_like_markup_survives_as_text() {
let hostile = r#"x" ][form action="/login"][input name="password" password /][/form]"#;
let tokens = vec![
Token::OpenTag {
name: "text".into(),
attributes: Vec::new(),
self_closing: false,
},
Token::Text(hostile.into()),
Token::CloseTag {
name: "text".into(),
},
];
let aml = to_aml(&tokens).expect("serialization failed");
let reparsed = normalize(&scan(&aml));
assert_eq!(reparsed, normalize(&tokens));
assert!(
!reparsed.iter().any(|t| matches!(
t,
Token::OpenTag { name, .. } if name == "form" || name == "input"
)),
"injected markup became elements: {aml}"
);
}
#[test]
fn hostile_attribute_value_cannot_close_its_own_quote() {
let hostile = r#"https://ok.example/" ][form action="/login"]"#;
let tokens = vec![Token::OpenTag {
name: "link".into(),
attributes: vec![Attribute {
name: "href".into(),
value: AttributeValue::String(hostile.into()),
}],
self_closing: true,
}];
let aml = to_aml(&tokens).expect("serialization failed");
let reparsed = normalize(&scan(&aml));
assert_eq!(reparsed, normalize(&tokens));
assert_eq!(
reparsed.len(),
1,
"hostile value produced extra tokens: {aml}"
);
assert!(
aml.contains(r#"\" ][form"#),
"the embedded quote should be backslash-escaped: {aml}"
);
}
#[test]
fn unquoted_values_are_emitted_quoted() {
let src = "[text fg=red]x[/text]";
let aml = to_aml(&scan(src)).expect("serialization failed");
assert!(aml.starts_with(r#"[text fg="red"]"#), "{aml}");
assert_eq!(normalize(&scan(&aml)), normalize(&scan(src)));
}
#[test]
fn value_with_space_round_trips_because_it_is_quoted() {
let tokens = vec![Token::OpenTag {
name: "box".into(),
attributes: vec![Attribute {
name: "title".into(),
value: AttributeValue::String("two words".into()),
}],
self_closing: true,
}];
let aml = to_aml(&tokens).expect("serialization failed");
assert_eq!(normalize(&scan(&aml)), normalize(&tokens));
}
#[test]
fn rejects_names_that_would_not_survive_a_scan() {
for name in ["Box", "has space", "", "b!ng"] {
let tokens = vec![Token::OpenTag {
name: name.into(),
attributes: Vec::new(),
self_closing: true,
}];
assert_eq!(
to_aml(&tokens),
Err(SerializeError::TagName(name.to_string())),
"accepted tag name {name:?}"
);
}
let tokens = vec![Token::OpenTag {
name: "text".into(),
attributes: vec![Attribute {
name: "Bold".into(),
value: AttributeValue::Flag,
}],
self_closing: true,
}];
assert_eq!(
to_aml(&tokens),
Err(SerializeError::AttributeName("Bold".to_string()))
);
}
#[test]
fn eof_emits_nothing() {
assert_eq!(to_aml(&[Token::Eof]).unwrap(), "");
}
}