Skip to content

Commit

Permalink
feat(macros): allow Android domain parts to have underscores (#964)
Browse files Browse the repository at this point in the history
* feat(macros): allow Android domain parts to have underscores

Ref tauri-apps/tauri#9707

* fill change file

* no need to unescape package

* Update .changes/android-package-underscores.md
  • Loading branch information
lucasfernog authored Aug 21, 2024
1 parent 508d4ab commit 4cd5341
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
6 changes: 6 additions & 0 deletions .changes/android-package-underscores.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"tao-macros": patch
"tao": patch
---

Allow Android domain names to include `_1` as escaped `_` characters - required because `_` is the separator for domain parts.
20 changes: 16 additions & 4 deletions tao-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ impl Parse for AndroidFnInput {
/// 5. List of extra types your Rust function expects. Pass empty array if the function doesn't need any arugments.
/// - If your function takes an arguments as reference with a lifetime tied to the [`JNIEnv`], it should use `'local` as the lifetime name as it is the
/// lifetime name generated by the macro.
/// Note that all rust functions should expect the first two parameters to be [`JNIEnv`] and [`JClass`] so make sure they are imported into scope).
/// Note that all rust functions should expect the first two parameters to be [`JNIEnv`] and [`JClass`] so make sure they are imported into scope).
/// 6. (Optional) Return type of your rust function.
/// - If your function returns a reference with a lifetime tied to the [`JNIEnv`], it should use `'local` as the lifetime name as it is the
/// lifetime name generated by the macro.
Expand Down Expand Up @@ -202,7 +202,7 @@ pub fn android_fn(tokens: TokenStream) -> TokenStream {
} = tokens;

let domain = domain.to_string();
let package = package.to_string().replace('_', "_1").replace('-', "_1");
let package = package.to_string().replace('_', "_1");
let class = class.to_string();
let args = args
.into_iter()
Expand Down Expand Up @@ -273,6 +273,11 @@ impl Parse for GeneratePackageNameInput {
/// 1. snake_case representation of the reversed domain of the app.
/// 2. snake_case representation of the package name.
///
/// Note that `_` is the separator for the domain parts.
/// For instance the `com.tauri.app` identifier should be represented as
/// `generate_package_name!(com_tauri, app)`.
/// To escape the `_` character you can use `_1` which aligns with the default NDK implementation.
///
/// ## Example
///
/// ```
Expand All @@ -295,8 +300,15 @@ pub fn generate_package_name(tokens: TokenStream) -> TokenStream {
let tokens = parse_macro_input!(tokens as GeneratePackageNameInput);
let GeneratePackageNameInput { domain, package } = tokens;

let domain = domain.to_string().replace('_', "/");
let package = package.to_string().replace('-', "_");
// note that this character is invalid in an identifier so it's safe to use as replacement
const TEMP_ESCAPE_UNDERSCORE_REPLACEMENT: &str = "<";

let domain = domain
.to_string()
.replace("_1", TEMP_ESCAPE_UNDERSCORE_REPLACEMENT)
.replace('_', "/")
.replace(TEMP_ESCAPE_UNDERSCORE_REPLACEMENT, "_");
let package = package.to_string();

let path = format!("{}/{}", domain, package);
let litstr = LitStr::new(&path, proc_macro2::Span::call_site());
Expand Down

0 comments on commit 4cd5341

Please sign in to comment.