use std::fmt::{self, Write};
use crate::ast;
impl fmt::Display for ast::DashIdent {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
for pair in self.0.pairs() {
pair.value().fmt(f)?;
if pair.punct().is_some() {
f.write_char('-')?;
}
}
Ok(())
}
}
impl fmt::Display for ast::Doctype {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("<!DOCTYPE html>")
}
}
impl ast::Value {
pub fn inlined(&self) -> Option<String> {
match self {
ast::Value::Text(text) => Some(text.value()),
ast::Value::Braced(expr) => match syn::parse2(expr.clone()) {
Ok(syn::Expr::Lit(expr_lit)) => match &expr_lit.lit {
syn::Lit::Str(str) => Some(str.value()),
syn::Lit::Byte(byte) => Some(byte.value().to_string()),
syn::Lit::Char(char) => Some(char.value().to_string()),
syn::Lit::Int(int) => Some(int.base10_digits().to_string()),
syn::Lit::Float(float) => {
Some(float.base10_digits().to_string())
}
syn::Lit::Bool(bool) => Some(bool.value().to_string()),
_ => None,
},
_ => None,
},
}
}
}
#[cfg(test)]
mod tests {
use quote::quote;
use super::*;
#[test]
fn fmt_dash_ident() {
assert_eq!(
syn::parse2::<ast::DashIdent>(quote! {foo})
.unwrap()
.to_string(),
"foo"
);
assert_eq!(
syn::parse2::<ast::DashIdent>(quote! {foo-bar})
.unwrap()
.to_string(),
"foo-bar"
);
assert_eq!(
syn::parse2::<ast::DashIdent>(quote! {foo-bar-baz})
.unwrap()
.to_string(),
"foo-bar-baz"
);
}
#[test]
fn fmt_doctype() {
assert_eq!(ast::Doctype.to_string(), "<!DOCTYPE html>");
}
}