-
Notifications
You must be signed in to change notification settings - Fork 508
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Make Debug impl optional for types (#797)
* Make Debug impl optional on derive(Message) * Config to optionally skip debug for messages * Inline things again * Put more things behind if * Optionally skip debug in oneofs too * Optionally skip debug for all types * fixup! Optionally skip debug for all types * Fix merge * Remove superfluous push_indent * Add some tests for the macro property * Fix test * Test generated protobuf * Unconditional Debug * Import --------- Co-authored-by: Lucio Franco <[email protected]>
- Loading branch information
1 parent
6180f9f
commit f9a3cff
Showing
7 changed files
with
216 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
syntax = "proto3"; | ||
|
||
package custom_debug; | ||
|
||
message Msg { | ||
int32 a = 1; | ||
string b = 2; | ||
oneof c { | ||
AnEnum d = 3; | ||
string e = 4; | ||
}; | ||
} | ||
|
||
enum AnEnum { | ||
A = 0; | ||
B = 1; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
//! Tests for skipping the default Debug implementation. | ||
|
||
use crate::custom_debug::{msg, AnEnum, Msg}; | ||
use crate::message_encoding::BasicEnumeration; | ||
use prost::alloc::{format, string::String}; | ||
use std::fmt; | ||
|
||
/// A special case with a tuple struct | ||
#[test] | ||
fn tuple_struct_custom_debug() { | ||
#[derive(Clone, PartialEq, prost::Message)] | ||
#[prost(skip_debug)] | ||
struct NewType(#[prost(enumeration = "BasicEnumeration", tag = "5")] i32); | ||
impl fmt::Debug for NewType { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str("NewType(custom_debug)") | ||
} | ||
} | ||
assert_eq!( | ||
format!("{:?}", NewType(BasicEnumeration::TWO as i32)), | ||
"NewType(custom_debug)" | ||
); | ||
assert_eq!(format!("{:?}", NewType(42)), "NewType(custom_debug)"); | ||
} | ||
|
||
#[allow(clippy::derive_partial_eq_without_eq)] | ||
#[derive(Clone, PartialEq, prost::Oneof)] | ||
#[prost(skip_debug)] | ||
pub enum OneofWithEnumCustomDebug { | ||
#[prost(int32, tag = "8")] | ||
Int(i32), | ||
#[prost(string, tag = "9")] | ||
String(String), | ||
#[prost(enumeration = "BasicEnumeration", tag = "10")] | ||
Enumeration(i32), | ||
} | ||
|
||
#[derive(Clone, PartialEq, prost::Message)] | ||
#[prost(skip_debug)] | ||
struct MessageWithOneofCustomDebug { | ||
#[prost(oneof = "OneofWithEnumCustomDebug", tags = "8, 9, 10")] | ||
of: Option<OneofWithEnumCustomDebug>, | ||
} | ||
|
||
impl fmt::Debug for MessageWithOneofCustomDebug { | ||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { | ||
f.write_str("MessageWithOneofCustomDebug {..}") | ||
} | ||
} | ||
|
||
/// Enumerations inside oneofs | ||
#[test] | ||
fn oneof_with_enum_custom_debug() { | ||
let msg = MessageWithOneofCustomDebug { | ||
of: Some(OneofWithEnumCustomDebug::Enumeration( | ||
BasicEnumeration::TWO as i32, | ||
)), | ||
}; | ||
assert_eq!(format!("{:?}", msg), "MessageWithOneofCustomDebug {..}"); | ||
} | ||
|
||
/// Generated protobufs | ||
#[test] | ||
fn test_proto_msg_custom_debug() { | ||
let msg = Msg { | ||
a: 0, | ||
b: "".to_string(), | ||
c: Some(msg::C::D(AnEnum::A as i32)), | ||
}; | ||
assert_eq!(format!("{:?}", msg), "Msg {..}"); | ||
} |