-
Notifications
You must be signed in to change notification settings - Fork 24
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support "format nodes"? #9
Comments
Just popping in to say I'd also benefit from something like this. Maybe it would be possible to specify an additional arbitrary delimiter, in addition to |
The use cases for special, non-valid-Rust syntax inside blocks can probably be vastly different, e.g. when thinking about all the things template engines are doing. So I'm hesitant to start implementing that here, since I feel like it's too high level for syn-rsx which tries to be generic plumbing for people implementing their own proc macros. However, I've implemented a use quote::quote;
use syn::{Expr, ExprLit, Token};
use syn_rsx::{parse2_with_config, ParserConfig};
let tokens = quote! {
<div>{% "{} ({})", text, value}</div>
};
let config = ParserConfig::new().transform_block(|input| {
if input.peek(Token![%]) {
input.parse::<Token![%]>()?;
let format_string = input.parse::<ExprLit>()?;
let mut values = vec![];
loop {
if input.is_empty() {
break;
}
input.parse::<Token![,]>()?;
values.push(input.parse::<Expr>()?);
}
Ok(Some(quote! { format_args!(#format_string, #(#values),*) }))
} else {
Ok(None)
}
});
parse2_with_config(tokens, config).unwrap(); Hope that covers it. If that doesn't help or there's something missing, please let me know. |
Published |
Wow, thanks! Will let you know how it goes. |
This works great! Thank you! |
Closing as this seems to be solved. |
I'm currently reviewing a PR that moves https://docs.rs/mox from snax to syn-rsx and I quite like most of what we get out of the change. One thing we're having to give up, though, is the ability to add sigils to the delimiters of expressions we parse in order to have a shorthand for string formatters, like this:
I chose
{% ... }
for the delimiters because it was (a) easy enough to implement with snax, (b) didn't collide with other common rust sigils and (c) reminded me of python's formatter sigil. Definitely open to alternative ways of spelling it out.With the switch to syn-rsx, the above becomes:
I'm of the opinion that formatting strings for XML-ish things is a common-enough operation that it deserves a shorthand, although I'll note that JSX itself gets by just fine without it. I'll also note that JS has tilde-format-strings, which we don't :*(. What do you think?
The text was updated successfully, but these errors were encountered: