11use crate :: errors:: TypeError ;
22use crate :: prelude:: * ;
3+ use core:: fmt:: { Debug , Formatter , Result as DebugResult } ;
34use core:: ops:: Deref ;
45use lcp_proto:: { google:: protobuf:: Any as ProtoAny , protobuf:: Protobuf } ;
56use serde:: { Deserialize , Serialize } ;
67
7- #[ derive( Default , Clone , Debug , PartialEq , Serialize , Deserialize ) ]
8+ const MAX_VALUE_LENGTH_FOR_DEBUG : usize = 4096 ;
9+
10+ #[ derive( Default , Clone , PartialEq , Serialize , Deserialize ) ]
811#[ serde( transparent) ]
912pub struct Any ( #[ serde( with = "ProtoAnyDef" ) ] ProtoAny ) ;
1013
@@ -18,6 +21,22 @@ impl Any {
1821 }
1922}
2023
24+ impl Debug for Any {
25+ fn fmt ( & self , f : & mut Formatter < ' _ > ) -> DebugResult {
26+ let mut debug = f. debug_struct ( "Any" ) ;
27+ debug. field ( "type_url" , & self . type_url ) ;
28+ if self . value . len ( ) > MAX_VALUE_LENGTH_FOR_DEBUG {
29+ debug. field ( "total_value_size" , & self . value . len ( ) ) . field (
30+ & format ! ( "first_{}_value" , MAX_VALUE_LENGTH_FOR_DEBUG ) ,
31+ & & self . value [ ..MAX_VALUE_LENGTH_FOR_DEBUG ] ,
32+ ) ;
33+ } else {
34+ debug. field ( "value" , & self . value ) ;
35+ }
36+ debug. finish ( )
37+ }
38+ }
39+
2140impl Deref for Any {
2241 type Target = ProtoAny ;
2342
@@ -111,4 +130,42 @@ mod tests {
111130 assert_eq!( any1, any3) ;
112131 }
113132 }
133+
134+ #[ test]
135+ fn test_debug_any ( ) {
136+ let type_url = "type_url" . to_string ( ) ;
137+ let base = Any :: new ( type_url. clone ( ) , [ 0u8 ; MAX_VALUE_LENGTH_FOR_DEBUG ] . to_vec ( ) ) ;
138+ let base_str = format ! ( "{:?}" , base) ;
139+ {
140+ let value = [ 0u8 ; MAX_VALUE_LENGTH_FOR_DEBUG + 1 ] . to_vec ( ) ;
141+ let value_str = format ! ( "{:?}" , Any :: new( type_url. clone( ) , value) ) ;
142+ let expected_str = base_str. replace (
143+ "value" ,
144+ & format ! (
145+ "total_value_size: {}, first_{}_value" ,
146+ MAX_VALUE_LENGTH_FOR_DEBUG + 1 ,
147+ MAX_VALUE_LENGTH_FOR_DEBUG
148+ ) ,
149+ ) ;
150+ assert_eq ! ( value_str, expected_str) ;
151+ }
152+ {
153+ let value = [ 0u8 ; MAX_VALUE_LENGTH_FOR_DEBUG ] . to_vec ( ) ;
154+ assert_eq ! ( base_str, format!( "{:?}" , Any :: new( type_url. clone( ) , value) ) ) ;
155+ }
156+ {
157+ let value = [ 0u8 ; MAX_VALUE_LENGTH_FOR_DEBUG - 1 ] . to_vec ( ) ;
158+ let value_str = format ! ( "{:?}" , Any :: new( type_url. clone( ) , value) ) ;
159+ let unexpected_str = base_str. replace (
160+ "value" ,
161+ & format ! (
162+ "total_value_size: {}, first_{}_value" ,
163+ MAX_VALUE_LENGTH_FOR_DEBUG - 1 ,
164+ MAX_VALUE_LENGTH_FOR_DEBUG
165+ ) ,
166+ ) ;
167+ assert_ne ! ( base_str, value_str) ;
168+ assert_ne ! ( value_str, unexpected_str) ;
169+ }
170+ }
114171}
0 commit comments