Import protobuf-codegen-2.14.0
* Add OWNERS, Android.bp, and README.android.
* Hard code version number in src/lib.rs for now.
It could be in a smarter update_package.sh to get the
new version number from Cargo.tom. But until then,
the difference in lib.rs will be caught and fixed manually.
* Rename protoc_gen_rust to protoc-gen-rust for aprotoc plugin.
Bug: 143953733
Test: make
Change-Id: I9b3c3b9f2e7ad0eb203c26534f2b6ba5fac46eef
diff --git a/src/file.rs b/src/file.rs
new file mode 100644
index 0000000..0c21d99
--- /dev/null
+++ b/src/file.rs
@@ -0,0 +1,70 @@
+use crate::rust;
+use crate::rust_name::RustIdent;
+use crate::strx;
+
+// Copy-pasted from libsyntax.
+fn ident_start(c: char) -> bool {
+ (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || c == '_'
+}
+
+// Copy-pasted from libsyntax.
+fn ident_continue(c: char) -> bool {
+ (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') || (c >= '0' && c <= '9') || c == '_'
+}
+
+pub(crate) fn proto_path_to_rust_mod(path: &str) -> RustIdent {
+ let without_dir = strx::remove_to(path, '/');
+ let without_suffix = strx::remove_suffix(without_dir, ".proto");
+
+ let name = without_suffix
+ .chars()
+ .enumerate()
+ .map(|(i, c)| {
+ let valid = if i == 0 {
+ ident_start(c)
+ } else {
+ ident_continue(c)
+ };
+ if valid {
+ c
+ } else {
+ '_'
+ }
+ })
+ .collect::<String>();
+
+ let name = if rust::is_rust_keyword(&name) {
+ format!("{}_pb", name)
+ } else {
+ name
+ };
+ RustIdent::from(name)
+}
+
+#[cfg(test)]
+mod test {
+
+ use super::proto_path_to_rust_mod;
+ use crate::rust_name::RustIdent;
+
+ #[test]
+ fn test_mod_path_proto_ext() {
+ assert_eq!(
+ RustIdent::from("proto"),
+ proto_path_to_rust_mod("proto.proto")
+ );
+ }
+
+ #[test]
+ fn test_mod_path_unknown_ext() {
+ assert_eq!(
+ RustIdent::from("proto_proto3"),
+ proto_path_to_rust_mod("proto.proto3")
+ );
+ }
+
+ #[test]
+ fn test_mod_path_empty_ext() {
+ assert_eq!(RustIdent::from("proto"), proto_path_to_rust_mod("proto"));
+ }
+}