Repository: tokio-rs/prost (language: rust)

Issue:
Case bug in proc macro generation
Using a proto file like so, with a default in it.
```
enum ERemoteClientBroadcastMsg {
	k_ERemoteClientBroadcastMsgDiscovery = 0;
}
message CMsgRemoteClientBroadcastHeader {
	optional ERemoteClientBroadcastMsg msg_type = 2 [default = k_ERemoteClientBroadcastMsgDiscovery];
}
```

Generates rust code like:
```
pub struct CMsgRemoteClientBroadcastHeader {
    #[prost(enumeration="ERemoteClientBroadcastMsg", optional, tag="2", default="KeRemoteClientBroadcastMsgDiscovery")]
    pub msg_type: ::std::option::Option<i32>,
}
```

Which has a case error in default specified in the proc macro. It should be generating the default to `KERemoteClientBroadcastMsgDiscovery`. This is with prost 0.6.1


Discussion / hints:
Thanks for the thorough report.  I've reproduced this, and I'm working on a fix.

Fix patch:
diff --git a/prost-build/src/ast.rs b/prost-build/src/ast.rs
index 5fd2d591b..cc023e2a6 100644
--- a/prost-build/src/ast.rs
+++ b/prost-build/src/ast.rs
@@ -1,4 +1,3 @@
-use prost_types;
 use prost_types::source_code_info::Location;
 
 /// Comments on a Protobuf item.
diff --git a/prost-build/src/code_generator.rs b/prost-build/src/code_generator.rs
index 7e5102879..ebf18a8a7 100644
--- a/prost-build/src/code_generator.rs
+++ b/prost-build/src/code_generator.rs
@@ -349,11 +349,12 @@ impl<'a> CodeGenerator<'a> {
                         .as_ref()
                         .and_then(|ty| ty.split('.').last())
                         .unwrap();
-                    strip_enum_prefix(enum_type, &enum_value)
+
+                    strip_enum_prefix(&to_upper_camel(&enum_type), &enum_value)
                 } else {
-                    default
+                    &enum_value
                 };
-                self.buf.push_str(&to_upper_camel(stripped_prefix));
+                self.buf.push_str(stripped_prefix);
             } else {
                 // TODO: this is only correct if the Protobuf escaping matches Rust escaping. To be
                 // safer, we should unescape the Protobuf string and re-escape it with the Rust
diff --git a/prost-build/src/lib.rs b/prost-build/src/lib.rs
index 3a25a0b73..733e31d68 100644
--- a/prost-build/src/lib.rs
+++ b/prost-build/src/lib.rs
@@ -692,7 +692,6 @@ pub fn protoc_include() -> &'static Path {
 #[cfg(test)]
 mod tests {
     use super::*;
-    use env_logger;
     use std::cell::RefCell;
     use std::rc::Rc;
 
diff --git a/protobuf/build.rs b/protobuf/build.rs
index 4762ed823..4ec8d1c08 100644
--- a/protobuf/build.rs
+++ b/protobuf/build.rs
@@ -1,7 +1,3 @@
-use prost_build;
-
-use tempfile;
-
 use std::env;
 use std::fs;
 use std::io::Cursor;


Test patch:
diff --git a/tests/src/default_enum_value.proto b/tests/src/default_enum_value.proto
index b41030c46..749d5764e 100644
--- a/tests/src/default_enum_value.proto
+++ b/tests/src/default_enum_value.proto
@@ -16,3 +16,11 @@ message Test {
   optional PrivacyLevel privacy_level_3 = 2 [default = PRIVACY_LEVEL_PRIVACY_LEVEL_THREE];
   optional PrivacyLevel privacy_level_4 = 3 [default = PRIVACY_LEVELPRIVACY_LEVEL_FOUR];
 }
+
+// danburkert/prost#310
+enum ERemoteClientBroadcastMsg {
+    k_ERemoteClientBroadcastMsgDiscovery = 0;
+}
+message CMsgRemoteClientBroadcastHeader {
+    optional ERemoteClientBroadcastMsg msg_type = 2 [default = k_ERemoteClientBroadcastMsgDiscovery];
+}
diff --git a/tests/src/unittest.rs b/tests/src/unittest.rs
index 77cab625c..d31f1a71e 100644
--- a/tests/src/unittest.rs
+++ b/tests/src/unittest.rs
@@ -1,3 +1,4 @@
+#![cfg(test)]
 #![allow(clippy::float_cmp)]
 
 use core::{f32, f64};