Skip to content

Commit

Permalink
gate debug code behind a feature
Browse files Browse the repository at this point in the history
  • Loading branch information
Baptistemontan committed Sep 1, 2023
1 parent eb76da3 commit f69fdd6
Show file tree
Hide file tree
Showing 13 changed files with 83 additions and 43 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
4 changes: 3 additions & 1 deletion examples/counter/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion examples/counter_plurals/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion examples/hello_world_actix/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion examples/hello_world_axum/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion examples/interpolation/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion examples/namespaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
1 change: 1 addition & 0 deletions leptos_i18n/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down
1 change: 1 addition & 0 deletions leptos_i18n/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions leptos_i18n_macro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ syn = "2.0"

[features]
serde = []
debug_interpolations = []
87 changes: 52 additions & 35 deletions leptos_i18n_macro/src/load_locales/interpolate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion tests/namespaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
4 changes: 3 additions & 1 deletion tests/no_namespaces/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down

0 comments on commit f69fdd6

Please sign in to comment.