use crate::tokens::Attribute;
use proc_macro2::TokenStream;
use quote::{format_ident, quote, ToTokens, TokenStreamExt};
impl ToTokens for Attribute {
fn to_tokens(&self, tokens: &mut TokenStream) {
match &self {
Attribute::ImplicitToggle { name } => {
let ident = format_ident!("{}", name);
let attribute_name = name.to_string();
tokens.append_all(quote! {
if #ident { concat!(#attribute_name, " ") } else { "" }
});
}
Attribute::ExplicitToggle { name, value } => {
let attribute_name = name.to_string();
tokens.append_all(quote! {
if #value { concat!(#attribute_name, " ") } else { "" }
});
}
Attribute::Constant { name } => {
let attribute_name = name.to_string();
tokens.append_all(quote! {
concat!(#attribute_name, " ")
});
}
Attribute::ConstantLiteral { name, literal } => {
let attribute_name = &name
.clone()
.to_string()
.trim_start_matches("r#")
.to_string();
tokens.append_all(quote! {
concat!(#attribute_name, "=\"", #literal, "\" ")
});
}
Attribute::ConstantGroup { name, contents } => {
let attribute_name = &name
.clone()
.to_string()
.trim_start_matches("r#")
.to_string();
tokens.append_all(quote! {
concat!(#attribute_name, "=\"") + &tidos::sanitize!(#contents) + "\" "
});
}
}
}
}
impl Attribute {
pub fn to_tokens_custom_element(&self) -> TokenStream {
match &self {
Attribute::ImplicitToggle { name } => {
let field = format_ident!("{}", name);
quote! { #field }
}
Attribute::ExplicitToggle { name, value } => {
let field = format_ident!("{}", name);
quote! { #field: #value }
}
Attribute::Constant { name } => {
let field = format_ident!("{}", name);
quote! { #field: true }
}
Attribute::ConstantLiteral { name, literal } => {
let field = format_ident!("{}", name);
quote! { #field: #literal }
}
Attribute::ConstantGroup { name, contents } => {
let field = format_ident!("{}", name);
quote! { #field: #contents }
}
}
}
}