From f69fdd672149bb2a61b8a431df09de6eadc63540 Mon Sep 17 00:00:00 2001 From: Baptiste de Montangon Date: Fri, 1 Sep 2023 20:26:39 +0200 Subject: [PATCH] gate debug code behind a feature --- README.md | 4 + examples/counter/Cargo.toml | 4 +- examples/counter_plurals/Cargo.toml | 4 +- examples/hello_world_actix/Cargo.toml | 4 +- examples/hello_world_axum/Cargo.toml | 4 +- examples/interpolation/Cargo.toml | 4 +- examples/namespaces/Cargo.toml | 4 +- leptos_i18n/Cargo.toml | 1 + leptos_i18n/src/lib.rs | 1 + leptos_i18n_macro/Cargo.toml | 1 + .../src/load_locales/interpolate.rs | 87 +++++++++++-------- tests/namespaces/Cargo.toml | 4 +- tests/no_namespaces/Cargo.toml | 4 +- 13 files changed, 83 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index f5b17bb2..fb954255 100644 --- a/README.md +++ b/README.md @@ -429,6 +429,10 @@ The `serde` feature implement `serde::Serialize` and `serde::Deserialize` for th The `nightly` feature enable to do `i18n()` to get the locale instead of `i18n.get_locale()` and `i18n(new_locale)` instead of `i18n.set_locale(new_locale)`. +The `debug_interpolations` feature enable the macros to generate code to emit a warning if a key is supplied twice in interpolations. +This is a feature as this code is not "necessary" and could slow compile times, +advice is to enable it for debug builds but disable it for release builds. + ## Contributing Errors are a bit clunky or obscure for now, there is a lot of edge cases and I did not had time to track every failing scenario, feel free to open an issue on github so I can improve those. diff --git a/examples/counter/Cargo.toml b/examples/counter/Cargo.toml index aede7096..e3764175 100644 --- a/examples/counter/Cargo.toml +++ b/examples/counter/Cargo.toml @@ -14,7 +14,9 @@ actix-web = { version = "4.3.1", optional = true, features = ["macros"] } leptos = "0.4.8" leptos_meta = { version = "0.4" } leptos_actix = { version = "0.4.8", optional = true } -leptos_i18n.path = "../../leptos_i18n" +leptos_i18n = { path = "../../leptos_i18n", features = [ + "debug_interpolations", +] } serde = { version = "1", features = ["derive"] } console_error_panic_hook = { version = "0.1", optional = true } wasm-bindgen = { version = "=0.2.87", optional = true } diff --git a/examples/counter_plurals/Cargo.toml b/examples/counter_plurals/Cargo.toml index aede7096..e3764175 100644 --- a/examples/counter_plurals/Cargo.toml +++ b/examples/counter_plurals/Cargo.toml @@ -14,7 +14,9 @@ actix-web = { version = "4.3.1", optional = true, features = ["macros"] } leptos = "0.4.8" leptos_meta = { version = "0.4" } leptos_actix = { version = "0.4.8", optional = true } -leptos_i18n.path = "../../leptos_i18n" +leptos_i18n = { path = "../../leptos_i18n", features = [ + "debug_interpolations", +] } serde = { version = "1", features = ["derive"] } console_error_panic_hook = { version = "0.1", optional = true } wasm-bindgen = { version = "=0.2.87", optional = true } diff --git a/examples/hello_world_actix/Cargo.toml b/examples/hello_world_actix/Cargo.toml index a75e823d..83d5f94d 100644 --- a/examples/hello_world_actix/Cargo.toml +++ b/examples/hello_world_actix/Cargo.toml @@ -14,7 +14,9 @@ actix-web = { version = "4.3.1", optional = true, features = ["macros"] } leptos = "0.4.8" leptos_meta = { version = "0.4" } leptos_actix = { version = "0.4.8", optional = true } -leptos_i18n.path = "../../leptos_i18n" +leptos_i18n = { path = "../../leptos_i18n", features = [ + "debug_interpolations", +] } serde = { version = "1", features = ["derive"] } console_error_panic_hook = { version = "0.1", optional = true } wasm-bindgen = { version = "=0.2.87", optional = true } diff --git a/examples/hello_world_axum/Cargo.toml b/examples/hello_world_axum/Cargo.toml index 78a64f39..797be1ee 100644 --- a/examples/hello_world_axum/Cargo.toml +++ b/examples/hello_world_axum/Cargo.toml @@ -13,7 +13,9 @@ axum = { version = "0.6", optional = true } leptos = "0.4.8" leptos_meta = { version = "0.4" } leptos_axum = { version = "0.4", optional = true } -leptos_i18n.path = "../../leptos_i18n" +leptos_i18n = { path = "../../leptos_i18n", features = [ + "debug_interpolations", +] } serde = { version = "1", features = ["derive"] } console_error_panic_hook = { version = "0.1", optional = true } wasm-bindgen = { version = "0.2", optional = true } diff --git a/examples/interpolation/Cargo.toml b/examples/interpolation/Cargo.toml index 19f26ce8..f9bf30ae 100644 --- a/examples/interpolation/Cargo.toml +++ b/examples/interpolation/Cargo.toml @@ -14,7 +14,9 @@ actix-web = { version = "4.3.1", optional = true, features = ["macros"] } leptos = "0.4.8" leptos_meta = { version = "0.4" } leptos_actix = { version = "0.4.8", optional = true } -leptos_i18n.path = "../../leptos_i18n" +leptos_i18n = { path = "../../leptos_i18n", features = [ + "debug_interpolations", +] } serde = { version = "1", features = ["derive"] } console_error_panic_hook = { version = "0.1", optional = true } wasm-bindgen = { version = "=0.2.87", optional = true } diff --git a/examples/namespaces/Cargo.toml b/examples/namespaces/Cargo.toml index 1e1a164e..62d64001 100644 --- a/examples/namespaces/Cargo.toml +++ b/examples/namespaces/Cargo.toml @@ -14,7 +14,9 @@ actix-web = { version = "4.3.1", optional = true, features = ["macros"] } leptos = "0.4.8" leptos_meta = { version = "0.4" } leptos_actix = { version = "0.4.8", optional = true } -leptos_i18n.path = "../../leptos_i18n" +leptos_i18n = { path = "../../leptos_i18n", features = [ + "debug_interpolations", +] } serde = { version = "1", features = ["derive"] } console_error_panic_hook = { version = "0.1", optional = true } wasm-bindgen = { version = "=0.2.87", optional = true } diff --git a/leptos_i18n/Cargo.toml b/leptos_i18n/Cargo.toml index cc8788a2..9cf79b85 100644 --- a/leptos_i18n/Cargo.toml +++ b/leptos_i18n/Cargo.toml @@ -35,6 +35,7 @@ ssr = ["leptos/ssr", "leptos_meta/ssr"] actix = ["ssr", "dep:actix-web"] axum = ["ssr", "dep:axum", "dep:leptos_axum"] serde = ["leptos_i18n_macro/serde", "dep:serde"] +debug_interpolations = ["leptos_i18n_macro/debug_interpolations"] [package.metadata.cargo-all-features] diff --git a/leptos_i18n/src/lib.rs b/leptos_i18n/src/lib.rs index 309df16c..c6cab38b 100644 --- a/leptos_i18n/src/lib.rs +++ b/leptos_i18n/src/lib.rs @@ -35,6 +35,7 @@ //! - `hydrate`: Enable this feature when building for the client. //! - `actix`: Enable this feature when building for the server with actix as the backend (can't be enabled with the `axum` feature). //! - `axum`: Enable this feature when building for the server with axum as the backend (can't be enabled with the `axum` feature). +//! - `debug_interpolations`: Enable the macros to generate code to emit a warning if a key is supplied twice in interpolations. //! - `cookie` (*Default*): Enable this feature to set a cookie on the client to remember the last locale set. //! //! # A Simple Counter diff --git a/leptos_i18n_macro/Cargo.toml b/leptos_i18n_macro/Cargo.toml index 42f3ea82..bee54c3f 100644 --- a/leptos_i18n_macro/Cargo.toml +++ b/leptos_i18n_macro/Cargo.toml @@ -22,3 +22,4 @@ syn = "2.0" [features] serde = [] +debug_interpolations = [] diff --git a/leptos_i18n_macro/src/load_locales/interpolate.rs b/leptos_i18n_macro/src/load_locales/interpolate.rs index 71745735..b2e58c7f 100644 --- a/leptos_i18n_macro/src/load_locales/interpolate.rs +++ b/leptos_i18n_macro/src/load_locales/interpolate.rs @@ -227,43 +227,60 @@ impl Interpolation { } }; - let left_generics_empty = - Self::generate_generics(left_fields, None, right_fields, |field| &field.generic); - let left_generics_already_set = Self::generate_generics( - left_fields, - Some({ - let field_gen = &field.generic; - quote!(#field_gen: #output_field_generic) - }), - right_fields, - quoted_gen, - ); - let right_generics_empty = Self::generate_generics( - left_fields, - Some(quote!(EmptyInterpolateValue)), - right_fields, - quoted_gen, - ); - let right_generics_already_set = - Self::generate_generics(left_fields, Some(&field.generic), right_fields, |field| { - &field.generic - }); - - let compile_warning = match field.kind { - InterpolateKey::Count(_) => "variable `count` is already set".to_string(), - InterpolateKey::Variable(_) => format!("variable `{}` is already set", field.name), - InterpolateKey::Component(_) => format!("component `{}` is already set", field.name), - }; + if cfg!(feature = "debug_interpolations") { + let left_generics_empty = + Self::generate_generics(left_fields, None, right_fields, |field| &field.generic); + let left_generics_already_set = Self::generate_generics( + left_fields, + Some({ + let field_gen = &field.generic; + quote!(#field_gen: #output_field_generic) + }), + right_fields, + quoted_gen, + ); + let right_generics_empty = Self::generate_generics( + left_fields, + Some(quote!(EmptyInterpolateValue)), + right_fields, + quoted_gen, + ); + let right_generics_already_set = + Self::generate_generics(left_fields, Some(&field.generic), right_fields, |field| { + &field.generic + }); + + let compile_warning = match field.kind { + InterpolateKey::Count(_) => "variable `count` is already set".to_string(), + InterpolateKey::Variable(_) => format!("variable `{}` is already set", field.name), + InterpolateKey::Component(_) => { + format!("component `{}` is already set", field.name) + } + }; - quote! { - #[allow(non_camel_case_types)] - impl<#(#left_generics_empty,)*> #ident<#(#right_generics_empty,)*> { - #set_function + quote! { + #[allow(non_camel_case_types)] + impl<#(#left_generics_empty,)*> #ident<#(#right_generics_empty,)*> { + #set_function + } + #[allow(non_camel_case_types)] + impl<#(#left_generics_already_set,)*> #ident<#(#right_generics_already_set,)*> { + #[deprecated(note = #compile_warning)] + #set_function + } } - #[allow(non_camel_case_types)] - impl<#(#left_generics_already_set,)*> #ident<#(#right_generics_already_set,)*> { - #[deprecated(note = #compile_warning)] - #set_function + } else { + let left_generics = + Self::generate_generics(left_fields, Some(&field.generic), right_fields, |field| { + &field.generic + }); + let right_generics = left_generics.clone(); + + quote! { + #[allow(non_camel_case_types)] + impl<#(#left_generics,)*> #ident<#(#right_generics,)*> { + #set_function + } } } } diff --git a/tests/namespaces/Cargo.toml b/tests/namespaces/Cargo.toml index 1e1a164e..62d64001 100644 --- a/tests/namespaces/Cargo.toml +++ b/tests/namespaces/Cargo.toml @@ -14,7 +14,9 @@ actix-web = { version = "4.3.1", optional = true, features = ["macros"] } leptos = "0.4.8" leptos_meta = { version = "0.4" } leptos_actix = { version = "0.4.8", optional = true } -leptos_i18n.path = "../../leptos_i18n" +leptos_i18n = { path = "../../leptos_i18n", features = [ + "debug_interpolations", +] } serde = { version = "1", features = ["derive"] } console_error_panic_hook = { version = "0.1", optional = true } wasm-bindgen = { version = "=0.2.87", optional = true } diff --git a/tests/no_namespaces/Cargo.toml b/tests/no_namespaces/Cargo.toml index b77faa42..058e834b 100644 --- a/tests/no_namespaces/Cargo.toml +++ b/tests/no_namespaces/Cargo.toml @@ -14,7 +14,9 @@ actix-web = { version = "4.3.1", optional = true, features = ["macros"] } leptos = "0.4.8" leptos_meta = { version = "0.4" } leptos_actix = { version = "0.4.8", optional = true } -leptos_i18n.path = "../../leptos_i18n" +leptos_i18n = { path = "../../leptos_i18n", features = [ + "debug_interpolations", +] } serde = { version = "1", features = ["derive"] } console_error_panic_hook = { version = "0.1", optional = true } wasm-bindgen = { version = "=0.2.87", optional = true }