I am currently struggling with a use case, where I want to achieve the following things:
This is a simplified version. Basically, I would have a macro to translate customized format to a function
example!(
name, // is an ident, mandatory
<expr1>, // is an expr, optional
[expr2], // is an expr, optional
)
name(Some(expr1), Some(expr2)) // my_macro!(name, <expr1>, [expr2])
name(None, Some(expr2)) // my_macro!(name, [expr2])
name(Some(expr1), None) // my_macro!(name, <expr1>)
name(None, None) // my_macro!(name)
I know that I can define multiple patterns to represent cases 1 through 4 and give a proper template. However, if am wondering if there is a more intelligent way or some sys-builtin macros (e.g stringify!) which can handler something like:
#[macro_export]
macro_rules! example {
($name:ident, $(($expr1:expr)),? $([$expr2:expr]),?) => {
let expr_1 = Some( $( $expr1 )? ) // or None
let expr_2 = Some( $( $expr2 )? ) // or None
name(expr_1, expr_2)
}
}