From be265e88d06dca97f8155c7e34e58812c88d7640 Mon Sep 17 00:00:00 2001 From: Varphone Wong Date: Sun, 21 Jan 2024 01:51:14 +0800 Subject: [PATCH] Add colonsAdd colon assignment syntax for tr!, similar to struct field assignment For example: tr!("Hello, %{name}", name: "world"); --- crates/macro/src/lib.rs | 4 +++- crates/macro/src/tr.rs | 11 +++++++++-- tests/integration_tests.rs | 4 ++++ 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/crates/macro/src/lib.rs b/crates/macro/src/lib.rs index 90fdbc0..b895cf5 100644 --- a/crates/macro/src/lib.rs +++ b/crates/macro/src/lib.rs @@ -358,7 +358,9 @@ pub fn vakey(input: proc_macro::TokenStream) -> proc_macro::TokenStream { /// // => "Hello, Foo and Bar" (Key `tr_3eULVGYoyiBuaM27F93Mo7` for "Hello, %{name} and %{other}") /// /// // With locale and variables -/// tr!("Hallo, %{name}", locale = "de", name = "Jason"); +/// tr!("Hallo, %{name}", locale = "de", name => "Jason"); // Arrow style +/// tr!("Hallo, %{name}", locale = "de", name = "Jason"); // Asignment style +/// tr!("Hallo, %{name}", locale = "de", name : "Jason"); // Colon style /// // => "Hallo, Jason" (Key `tr_4Cct6Q289b12SkvF47dXIx` for "Hallo, %{name}") /// # } /// ``` diff --git a/crates/macro/src/tr.rs b/crates/macro/src/tr.rs index 008472c..79e3158 100644 --- a/crates/macro/src/tr.rs +++ b/crates/macro/src/tr.rs @@ -27,8 +27,15 @@ impl syn::parse::Parse for Argument { .map(|v| v.to_string()) .or_else(|_| input.parse::().map(|v| v.value())) .map_err(|_| input.error("Expected a `string` literal or an identifier"))?; - let _ = input.parse::()?; - let _ = input.parse::]>>()?; + if input.peek(Token![=>]) { + let _ = input.parse::]>()?; + } else if input.peek(Token![=]) { + let _ = input.parse::()?; + } else if input.peek(Token![:]) { + let _ = input.parse::()?; + } else { + return Err(input.error("Expected `=>`, `=` or `:`")); + } let value = input.parse::()?; Ok(Self { name, value }) } diff --git a/tests/integration_tests.rs b/tests/integration_tests.rs index 879ed06..43082cd 100644 --- a/tests/integration_tests.rs +++ b/tests/integration_tests.rs @@ -225,7 +225,11 @@ mod tests { ); rust_i18n::set_locale("zh-CN"); + assert_eq!(tr!("Hello, %{name}!", name => "world"), "你好,world!"); assert_eq!(tr!("Hello, %{name}!", name = "world"), "你好,world!"); + assert_eq!(tr!("Hello, %{name}!", name : "world"), "你好,world!"); + assert_eq!(tr!("Hello, %{name}!", name: "world"), "你好,world!"); + assert_eq!(tr!("Hello, %{name}!", name:"world"), "你好,world!"); rust_i18n::set_locale("en"); assert_eq!(tr!("Hello, %{name}!", name = "world"), "Hello, world!");