From 13e26188cdfb797ef2434e42ba1515ef34d8ae63 Mon Sep 17 00:00:00 2001 From: otaxhu Date: Fri, 2 May 2025 23:43:49 -0400 Subject: [PATCH] feat: Add suppport for trailing commas when calling formatx macro --- src/macros.rs | 26 +++++++++++++++++++++++--- 1 file changed, 23 insertions(+), 3 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index b13968e..c4ab014 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -34,7 +34,7 @@ /// ``` #[macro_export] macro_rules! formatx { - ($template: expr) => { + ($template: expr $(,)?) => { || -> ::std::result::Result { $crate::Template::new($template)?.text() }() @@ -52,11 +52,11 @@ macro_rules! formatx { #[macro_export] #[doc(hidden)] macro_rules! _formatx_internal { - ($template: expr, $name: ident = $value: expr) => { + ($template: expr, $name: ident = $value: expr $(,)?) => { $template.replace(stringify!($name), $value); }; - ($template: expr, $value: expr) => ( + ($template: expr, $value: expr $(,)?) => ( $template.replace_positional($value); ); @@ -147,4 +147,24 @@ mod tests { formatx_test!("{} {:?}", 'a', 'b'); formatx_test!("{} {:?}", "foo\n", "bar\n"); } + + #[test] + fn trailing_commas() { + formatx_test!( + "Hello", + ); + formatx_test!( + "Hello {}", + "world", + ); + formatx_test!( + "Hello {1} {0}", + "1", + "0", + ); + formatx_test!( + "Hello {name}", + name = "John", + ); + } }