From 10c5ee1159288b6313e59fb78e03c0ed434cdddb Mon Sep 17 00:00:00 2001
From: Vlasislav Kashin <99754299+vloldik@users.noreply.github.com>
Date: Fri, 11 Jul 2025 03:46:50 +0300
Subject: [PATCH] Java: added compiled metadata generation
---
src/lib.rs | 2 +-
.../metadata.rs | 17 ++-
src/phonenumberutil/generated/mod.rs | 3 +
.../generated}/test_metadata.rs | 20 +++-
src/phonenumberutil/helper_constants/mod.rs | 2 -
src/phonenumberutil/helper_functions.rs | 7 +-
src/phonenumberutil/mod.rs | 1 +
src/tests/mod.rs | 1 -
src/tests/tests.rs | 4 +-
tools/java/Readme.md | 12 +-
.../i18n/phonenumbers/CopyrightNotice.java | 21 +---
tools/java/rust-build/pom.xml | 11 +-
...Xml.java => BuildMetadataRustFromXml.java} | 75 ++++++++-----
.../google/i18n/phonenumbers/EntryPoint.java | 6 +-
...erator.java => RustMetadataGenerator.java} | 106 +++++-------------
...java => BuildMetadataRustFromXmlTest.java} | 93 +++++----------
...st.java => RustMetadataGeneratorTest.java} | 55 ++++-----
tools/scripts/generate_metadata.sh | 33 ++++++
18 files changed, 222 insertions(+), 247 deletions(-)
rename src/phonenumberutil/{helper_constants => generated}/metadata.rs (99%)
create mode 100644 src/phonenumberutil/generated/mod.rs
rename src/{tests => phonenumberutil/generated}/test_metadata.rs (99%)
rename tools/java/rust-build/src/com/google/i18n/phonenumbers/{BuildMetadataCppFromXml.java => BuildMetadataRustFromXml.java} (74%)
rename tools/java/rust-build/src/com/google/i18n/phonenumbers/{CppMetadataGenerator.java => RustMetadataGenerator.java} (55%)
rename tools/java/rust-build/test/com/google/i18n/phonenumbers/{BuildMetadataCppFromXmlTest.java => BuildMetadataRustFromXmlTest.java} (56%)
rename tools/java/rust-build/test/com/google/i18n/phonenumbers/{CppMetadataGeneratorTest.java => RustMetadataGeneratorTest.java} (58%)
create mode 100644 tools/scripts/generate_metadata.sh
diff --git a/src/lib.rs b/src/lib.rs
index 172b27f..e3d6aa3 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -32,4 +32,4 @@ pub use phonenumberutil::{
};
pub use proto_gen::phonemetadata;
pub use proto_gen::phonenumber;
-mod tests;
\ No newline at end of file
+mod tests;
diff --git a/src/phonenumberutil/helper_constants/metadata.rs b/src/phonenumberutil/generated/metadata.rs
similarity index 99%
rename from src/phonenumberutil/helper_constants/metadata.rs
rename to src/phonenumberutil/generated/metadata.rs
index 82419a3..5b6389f 100644
--- a/src/phonenumberutil/helper_constants/metadata.rs
+++ b/src/phonenumberutil/generated/metadata.rs
@@ -1,3 +1,19 @@
+/*
+ * Copyright (C) 2011 The Libphonenumber Authors
+ * Copyright (C) 2025 Vladislav Kashin (modified)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
pub const METADATA: [u8; 201802] = [
0x0A, 0xE9, 0x01, 0x0A, 0x1D, 0x12, 0x17, 0x28, 0x3F, 0x3A, 0x5B, 0x30, 0x31,
@@ -15525,4 +15541,3 @@ pub const METADATA: [u8; 201802] = [
0xFF, 0x01, 0xE2, 0x01, 0x0B, 0x48, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0x01
];
-
diff --git a/src/phonenumberutil/generated/mod.rs b/src/phonenumberutil/generated/mod.rs
new file mode 100644
index 0000000..6c7373e
--- /dev/null
+++ b/src/phonenumberutil/generated/mod.rs
@@ -0,0 +1,3 @@
+pub mod metadata;
+pub mod test_metadata;
+
diff --git a/src/tests/test_metadata.rs b/src/phonenumberutil/generated/test_metadata.rs
similarity index 99%
rename from src/tests/test_metadata.rs
rename to src/phonenumberutil/generated/test_metadata.rs
index e3be2a2..2c59a1f 100644
--- a/src/tests/test_metadata.rs
+++ b/src/phonenumberutil/generated/test_metadata.rs
@@ -1,5 +1,21 @@
+/*
+ * Copyright (C) 2011 The Libphonenumber Authors
+ * Copyright (C) 2025 Vladislav Kashin (modified)
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
-pub const METADATA: [u8; 13260] = [
+pub const TEST_METADATA: [u8; 13260] = [
0x0A, 0xAD, 0x01, 0x0A, 0x09, 0x12, 0x05, 0x5C, 0x64, 0x7B, 0x36, 0x7D, 0x48,
0x06, 0x12, 0x0F, 0x12, 0x05, 0x5C, 0x64, 0x7B, 0x36, 0x7D, 0x32, 0x06, 0x31,
0x32, 0x33, 0x34, 0x35, 0x36, 0x1A, 0x0B, 0x48, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF,
@@ -1020,4 +1036,4 @@ pub const METADATA: [u8; 13260] = [
0x48, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xCA, 0x01,
0x0B, 0x48, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01, 0xE2,
0x01, 0x0B, 0x48, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0x01
-];
\ No newline at end of file
+];
diff --git a/src/phonenumberutil/helper_constants/mod.rs b/src/phonenumberutil/helper_constants/mod.rs
index da7f7a0..372cdc2 100644
--- a/src/phonenumberutil/helper_constants/mod.rs
+++ b/src/phonenumberutil/helper_constants/mod.rs
@@ -1,5 +1,3 @@
mod helper_constants;
-mod metadata;
pub(super) use helper_constants::{*};
-pub(super) use metadata::METADATA;
diff --git a/src/phonenumberutil/helper_functions.rs b/src/phonenumberutil/helper_functions.rs
index 490c1dd..35a2e73 100644
--- a/src/phonenumberutil/helper_functions.rs
+++ b/src/phonenumberutil/helper_functions.rs
@@ -4,18 +4,17 @@ use protobuf::Message;
use strum::IntoEnumIterator;
use crate::{
- interfaces::MatcherApi,
- proto_gen::{
+ interfaces::MatcherApi, phonenumberutil::generated::metadata::METADATA, proto_gen::{
phonemetadata::{PhoneMetadata, PhoneMetadataCollection, PhoneNumberDesc},
phonenumber::PhoneNumber,
- },
+ }
};
use super::{
PhoneNumberFormat, PhoneNumberType, ValidNumberLenType,
errors::ValidationResultErr,
helper_constants::{
- METADATA, OPTIONAL_EXT_SUFFIX, PLUS_SIGN, POSSIBLE_CHARS_AFTER_EXT_LABEL,
+ OPTIONAL_EXT_SUFFIX, PLUS_SIGN, POSSIBLE_CHARS_AFTER_EXT_LABEL,
POSSIBLE_SEPARATORS_BETWEEN_NUMBER_AND_EXT_LABEL, RFC3966_EXTN_PREFIX, RFC3966_PREFIX,
},
};
diff --git a/src/phonenumberutil/mod.rs b/src/phonenumberutil/mod.rs
index 21d2abb..7aa9989 100644
--- a/src/phonenumberutil/mod.rs
+++ b/src/phonenumberutil/mod.rs
@@ -6,6 +6,7 @@ pub mod phonenumberutil;
mod phone_number_regexps_and_mappings;
pub(self) mod helper_types;
pub(self) mod comparisons;
+pub(crate) mod generated;
use std::sync::LazyLock;
diff --git a/src/tests/mod.rs b/src/tests/mod.rs
index 54cdcab..14f0038 100644
--- a/src/tests/mod.rs
+++ b/src/tests/mod.rs
@@ -1,2 +1 @@
mod tests;
-mod test_metadata;
\ No newline at end of file
diff --git a/src/tests/tests.rs b/src/tests/tests.rs
index 58aef3e..c9ba7f3 100644
--- a/src/tests/tests.rs
+++ b/src/tests/tests.rs
@@ -10,13 +10,13 @@ use protobuf::Message;
use crate::{errors::ParseError, phonemetadata::PhoneMetadataCollection, phonenumber::PhoneNumber, PhoneNumberUtil};
-use super::test_metadata::METADATA;
+use crate::phonenumberutil::generated::test_metadata::TEST_METADATA;
// This setup function simulates getting the PhoneNumberUtil instance for each test.
fn get_phone_util() -> PhoneNumberUtil {
- let metadata = PhoneMetadataCollection::parse_from_bytes(&METADATA)
+ let metadata = PhoneMetadataCollection::parse_from_bytes(&TEST_METADATA)
.expect("Metadata should be valid");
// In a real scenario, this would likely return a singleton instance.
return PhoneNumberUtil::new_for_metadata(metadata);
diff --git a/tools/java/Readme.md b/tools/java/Readme.md
index a958d94..608f0c7 100644
--- a/tools/java/Readme.md
+++ b/tools/java/Readme.md
@@ -1 +1,11 @@
-## This directory contains script for autogeneration of metadata in rust
\ No newline at end of file
+## This directory contains script for autogeneration of metadata in rust
+
+To build from source cd /tools/java and
+```
+mvn install
+```
+
+Example command on build generator
+```
+java -jar tools\java\rust-build\target\rust-build-1.0-SNAPSHOT-jar-with-dependencies.jar BuildMetadataRustFromXml resources\PhoneNumberMetadata.xml ./test.rs metadata --const-name=test
+```
\ No newline at end of file
diff --git a/tools/java/common/src/com/google/i18n/phonenumbers/CopyrightNotice.java b/tools/java/common/src/com/google/i18n/phonenumbers/CopyrightNotice.java
index d8cd9e2..03fdd30 100644
--- a/tools/java/common/src/com/google/i18n/phonenumbers/CopyrightNotice.java
+++ b/tools/java/common/src/com/google/i18n/phonenumbers/CopyrightNotice.java
@@ -1,5 +1,6 @@
/*
* Copyright (C) 2011 The Libphonenumber Authors
+ * Copyright (C) %d Vladislav Kashin (modified)
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
@@ -24,18 +25,16 @@ import java.util.Formatter;
* Class containing the Apache copyright notice used by code generators.
*
* @author Philippe Liard
+ * @author Kashin Vladislav (modified for Rust code generation)
*/
public class CopyrightNotice {
private static final String TEXT_OPENING =
"/*\n";
- private static final String TEXT_OPENING_FOR_JAVASCRIPT =
- "/**\n" +
- " * @license\n";
-
private static final String TEXT =
" * Copyright (C) %d The Libphonenumber Authors\n" +
+ " * Copyright (C) %d Vladislav Kashin (modified)\n" +
" *\n" +
" * Licensed under the Apache License, Version 2.0 (the \"License\");\n" +
" * you may not use this file except in compliance with the License.\n" +
@@ -50,17 +49,9 @@ public class CopyrightNotice {
" * limitations under the License.\n" +
" */\n\n";
- static final void writeTo(Writer writer, int year) throws IOException {
- writeTo(writer, year, false);
- }
-
- static final void writeTo(Writer writer, int year, boolean isJavascript) throws IOException {
- if (isJavascript) {
- writer.write(TEXT_OPENING_FOR_JAVASCRIPT);
- } else {
- writer.write(TEXT_OPENING);
- }
+ static final void writeTo(Writer writer, int year, int yearSecondAuthor) throws IOException {
+ writer.write(TEXT_OPENING);
Formatter formatter = new Formatter(writer);
- formatter.format(TEXT, year);
+ formatter.format(TEXT, year, yearSecondAuthor);
}
}
diff --git a/tools/java/rust-build/pom.xml b/tools/java/rust-build/pom.xml
index 86407e0..3b9fec6 100644
--- a/tools/java/rust-build/pom.xml
+++ b/tools/java/rust-build/pom.xml
@@ -10,12 +10,13 @@
com.google.i18n.phonenumbers.tools
- cpp-build
+ rust-build
1.0-SNAPSHOT
- Libphonenumber C++ build tools
+ Libphonenumber Rust build tools
- C++ build tools that download dependencies under base/ from the Chromium source repository, and
- generate the C++ metadata code needed to build the libphonenumber library.
+ Rust build tools that download dependencies under base/ from the Chromium source repository, and
+ generate the Rust metadata code needed to build the libphonenumber library.
+ It depends on libphonenumber original Java library.
@@ -115,8 +116,6 @@
-
org.apache.maven.plugins
maven-assembly-plugin
diff --git a/tools/java/rust-build/src/com/google/i18n/phonenumbers/BuildMetadataCppFromXml.java b/tools/java/rust-build/src/com/google/i18n/phonenumbers/BuildMetadataRustFromXml.java
similarity index 74%
rename from tools/java/rust-build/src/com/google/i18n/phonenumbers/BuildMetadataCppFromXml.java
rename to tools/java/rust-build/src/com/google/i18n/phonenumbers/BuildMetadataRustFromXml.java
index ff25165..4f51d42 100644
--- a/tools/java/rust-build/src/com/google/i18n/phonenumbers/BuildMetadataCppFromXml.java
+++ b/tools/java/rust-build/src/com/google/i18n/phonenumbers/BuildMetadataRustFromXml.java
@@ -1,6 +1,7 @@
/*
* Copyright (C) 2011 The Libphonenumber Authors
- *
+ * Copyright (C) 2025 The Kashin Vladislav (modified)
+ *
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
@@ -16,7 +17,7 @@
package com.google.i18n.phonenumbers;
-import com.google.i18n.phonenumbers.CppMetadataGenerator.Type;
+import com.google.i18n.phonenumbers.RustMetadataGenerator.Type;
import java.io.ByteArrayOutputStream;
import java.io.File;
@@ -26,19 +27,22 @@ import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.nio.charset.Charset;
+import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
/**
- * This class generates the C++ code representation of the provided XML metadata file. It lets us
+ * This class generates the Rust code representation of the provided XML metadata file. It lets us
* embed metadata directly in a native binary. We link the object resulting from the compilation of
- * the code emitted by this class with the C++ phonenumber library.
+ * the code emitted by this class with the Rust rlibphonenumber library.
*
* @author Philippe Liard
* @author David Beaumont
+ *
+ * @author Kashin Vladislav (modified for Rust code generation)
*/
-public class BuildMetadataCppFromXml extends Command {
+public class BuildMetadataRustFromXml extends Command {
/** An enum encapsulating the variations of metadata that we can produce. */
public enum Variant {
@@ -93,22 +97,38 @@ public class BuildMetadataCppFromXml extends Command {
static final class Options {
private static final Pattern BASENAME_PATTERN =
Pattern.compile("(?:(test|lite)_)?([a-z_]+)");
-
- public static Options parse(String commandName, String[] args) {
- if (args.length == 4) {
- String inputXmlFilePath = args[1];
- String outputDirPath = args[2];
- Matcher basenameMatcher = BASENAME_PATTERN.matcher(args[3]);
+ private static final Pattern CONSTANT_NAME_PATTERN =
+ Pattern.compile("--const-name[ =]([a-zA-Z_]+)");
+ private static final String DEFAULT_METADATA_CONSTANT_NAME = "METADATA";
+ public static Options parse(String commandName, String[] argsArray) {
+ ArrayList args = new ArrayList(Arrays.asList(argsArray));
+ String constantName = DEFAULT_METADATA_CONSTANT_NAME;
+ if (args.size() == 5) {
+ for (int i = 0; i < args.size(); i++) {
+ String arg = args.get(i).toString();
+ Matcher matcher = CONSTANT_NAME_PATTERN.matcher(arg.toString());
+ if (matcher.matches()) {
+ constantName = matcher.group(1);
+ args.remove(arg);
+ break;
+ }
+ }
+ }
+ if (args.size() == 4) {
+ String inputXmlFilePath = args.get(1).toString();
+ String outputDirPath = args.get(2).toString();
+ Matcher basenameMatcher = BASENAME_PATTERN.matcher(args.get(3).toString());
if (basenameMatcher.matches()) {
Variant variant = Variant.parse(basenameMatcher.group(1));
Type type = Type.parse(basenameMatcher.group(2));
if (type != null && variant != null) {
- return new Options(inputXmlFilePath, outputDirPath, type, variant);
+ return new Options(inputXmlFilePath, outputDirPath, type, variant, constantName);
}
}
}
throw new IllegalArgumentException(String.format(
- "Usage: %s ( | test_ | lite_ )\n" +
+ "Usage: %s