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,26 @@ 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 (
30+ "value" ,
31+ & format_args ! (
32+ "{:?} … ({} bytes total)" ,
33+ & self . value[ ..MAX_VALUE_LENGTH_FOR_DEBUG ] ,
34+ self . value. len( )
35+ ) ,
36+ ) ;
37+ } else {
38+ debug. field ( "value" , & self . value ) ;
39+ }
40+ debug. finish ( )
41+ }
42+ }
43+
2144impl Deref for Any {
2245 type Target = ProtoAny ;
2346
@@ -111,4 +134,29 @@ mod tests {
111134 assert_eq!( any1, any3) ;
112135 }
113136 }
137+
138+ #[ test]
139+ fn test_debug_any ( ) {
140+ let type_url = "type_url" . to_string ( ) ;
141+ let base = Any :: new ( type_url. clone ( ) , [ 0u8 ; MAX_VALUE_LENGTH_FOR_DEBUG ] . to_vec ( ) ) ;
142+ let base_str = format ! ( "{:?}" , base) ;
143+ {
144+ let value = [ 0u8 ; MAX_VALUE_LENGTH_FOR_DEBUG + 1 ] . to_vec ( ) ;
145+ let value_str = format ! ( "{:?}" , Any :: new( type_url. clone( ) , value) ) ;
146+ assert_ne ! ( value_str, base_str) ;
147+ assert ! (
148+ value_str. contains( & format!( "({} bytes total)" , MAX_VALUE_LENGTH_FOR_DEBUG + 1 ) )
149+ ) ;
150+ }
151+ {
152+ let value = [ 0u8 ; MAX_VALUE_LENGTH_FOR_DEBUG ] . to_vec ( ) ;
153+ assert_eq ! ( base_str, format!( "{:?}" , Any :: new( type_url. clone( ) , value) ) ) ;
154+ }
155+ {
156+ let value = [ 0u8 ; MAX_VALUE_LENGTH_FOR_DEBUG - 1 ] . to_vec ( ) ;
157+ let value_str = format ! ( "{:?}" , Any :: new( type_url. clone( ) , value) ) ;
158+ assert_ne ! ( value_str, base_str) ;
159+ assert ! ( !value_str. contains( "bytes total" ) ) ;
160+ }
161+ }
114162}
0 commit comments