Skip to content

Commit

Permalink
fix: html attribute args
Browse files Browse the repository at this point in the history
  • Loading branch information
zzau13 committed Jul 29, 2023
1 parent 6f0e66f commit 616daf3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 31 deletions.
14 changes: 5 additions & 9 deletions examples/bytes/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,23 +21,19 @@ fn _write_str(buffer: BytesMut) {
#[cfg(nightly)]
fn nightly(my_card: &Card) {
let mut buffer = BytesMut::new();
// TODO: unexpected token `;`, bad statement when pass without let
let _ = #[html(buffer)]
#[html(buffer)]
"{{> hello my_card }}";

#[cfg(nightly)]
"Why this runs and not the above";

println!("Proc macro attribute");
stdout().lock().write_all(&buffer).unwrap();
println!();

println!("Proc macro attribute auto");

// without comma or error
// `message: stable/nightly mismatch`
#[rustfmt::skip]
_write_str(#[html] "{{> hello my_card }}");
_write_str(
#[html]
"{{> hello my_card }}",
);

println!();

Expand Down
41 changes: 19 additions & 22 deletions yarte_derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use proc_macro::TokenStream;
use quote::{format_ident, quote};

use syn::parse::ParseBuffer;
use syn::parse::{ParseBuffer, ParseStream};
use syn::spanned::Spanned;

use yarte_codegen::{CodeGen, FmtCodeGen, HTMLCodeGen, TextCodeGen};
Expand Down Expand Up @@ -117,23 +117,6 @@ pub fn template_html_bytes(input: TokenStream) -> TokenStream {
build!(i, get_codegen, Default::default()).into()
}

#[proc_macro_derive(TemplateFixedMin, attributes(template))]
#[cfg(all(feature = "html-min", feature = "fixed"))]
/// # Work in Progress
/// Implements TemplateTrait with html minifier
pub fn template_html_min_ptr(input: TokenStream) -> TokenStream {
const PARENT: &str = "yarte";
fn get_codegen<'a>(s: &'a Struct) -> Box<dyn CodeGen + 'a> {
Box::new(yarte_codegen::FixedCodeGen::new(
yarte_codegen::HTMLMinFixedCodeGen(PARENT),
s,
PARENT,
))
}
let i = &syn::parse(input).unwrap();
build!(i, get_codegen, Default::default()).into()
}

#[proc_macro_derive(Serialize)]
#[cfg(feature = "json")]
pub fn serialize_json(i: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -389,8 +372,22 @@ pub fn ywrite_min(i: TokenStream) -> TokenStream {
.into()
}

// TODO: PoC
// TODO: Simplify and infer type
struct TemplateArg {
pub s: syn::LitStr,
_d: Option<syn::Token![;]>,
_c: Option<syn::Token![,]>
}

impl syn::parse::Parse for TemplateArg {
fn parse(input: ParseStream) -> syn::Result<Self> {
Ok(TemplateArg {
s: input.parse()?,
_d: input.parse()?,
_c: input.parse()?
})
}
}

#[proc_macro_attribute]
#[cfg(feature = "bytes-buf")]
pub fn html(args: TokenStream, input: TokenStream) -> TokenStream {
Expand Down Expand Up @@ -424,8 +421,8 @@ pub fn html(args: TokenStream, input: TokenStream) -> TokenStream {
!args_is_empty,
))
};
let input: syn::LitStr = match syn::parse(input) {
Ok(i) => i,
let input = match syn::parse::<TemplateArg>(input) {
Ok(i) => i.s,
Err(e) => return e.into_compile_error().into(),
};
let input = quote! {
Expand Down

0 comments on commit 616daf3

Please sign in to comment.