@@ -17,23 +17,26 @@ use super::Writer;
17
17
pub trait EncodeClarifyExt : Encode {
18
18
/// Encode this type as pretty-printed hex DER, with comments.
19
19
fn to_der_clarify ( & self , flavor : ClarifyFlavor ) -> Result < String > {
20
- let outputs = self . to_der_clarify_err_ignorant ( flavor) ;
20
+ let outputs = self . to_der_clarify_err_ignorant ( ClarifyOptions {
21
+ flavor,
22
+ ..Default :: default ( )
23
+ } ) ;
21
24
// Propagate encode and finish errors
22
25
outputs. raw ?;
23
26
Ok ( String :: from_utf8 ( outputs. clarify_buf ) . expect ( "clarified output to be utf-8" ) )
24
27
}
25
28
26
29
/// Encode this type as pretty-printed hex DER, with comments.
27
30
/// Ignores any errors that occur during [`Encode::encode`].
28
- fn to_der_clarify_err_ignorant ( & self , flavor : ClarifyFlavor ) -> ClarifyOutputs < ' static > {
31
+ fn to_der_clarify_err_ignorant ( & self , options : ClarifyOptions ) -> ClarifyOutputs < ' static > {
29
32
let len = match self . encoded_len ( ) {
30
33
Ok ( len) => len,
31
34
Err ( err) => return ClarifyOutputs :: from_err ( err) ,
32
35
} ;
33
36
34
37
let mut buf = vec ! [ 0u8 ; u32 :: from( len) as usize ] ;
35
38
36
- let mut writer = ClarifySliceWriter :: new ( & mut buf, Vec :: new ( ) , flavor ) ;
39
+ let mut writer = ClarifySliceWriter :: new ( & mut buf, Vec :: new ( ) , options ) ;
37
40
let result = self . encode ( & mut writer) ;
38
41
39
42
let outputs = writer. finish ( ) ;
@@ -50,6 +53,25 @@ pub trait EncodeClarifyExt: Encode {
50
53
51
54
impl < T > EncodeClarifyExt for T where T : Encode { }
52
55
56
+ /// Options to customize pretty-printing.
57
+ #[ derive( Clone ) ]
58
+ pub struct ClarifyOptions {
59
+ /// How should comments look like?
60
+ pub flavor : ClarifyFlavor ,
61
+
62
+ /// Write types? E.g `type: OctetStringRef`
63
+ pub print_types : bool ,
64
+ }
65
+
66
+ impl Default for ClarifyOptions {
67
+ fn default ( ) -> Self {
68
+ Self {
69
+ flavor : Default :: default ( ) ,
70
+ print_types : true ,
71
+ }
72
+ }
73
+ }
74
+
53
75
static INDENT_STR : & str =
54
76
"\t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t \t " ;
55
77
@@ -76,6 +98,8 @@ pub struct Clarifier {
76
98
/// Determines if newlines and indent are currently enabled
77
99
indent_enabled : bool ,
78
100
101
+ print_types : bool ,
102
+
79
103
/// Sans-io buffer for comments
80
104
comment_writer : Box < dyn CommentWriter > ,
81
105
}
@@ -106,27 +130,31 @@ impl<'a> ClarifyOutputs<'a> {
106
130
}
107
131
108
132
/// Determines how comments will look like
109
- #[ derive( Copy , Clone , Debug ) ]
133
+ #[ derive( Copy , Clone , Debug , Default ) ]
110
134
pub enum ClarifyFlavor {
111
135
/// `01 02 <!-- comment -->`
112
136
XmlComments ,
113
137
/// `01 02 // comment`
138
+ #[ default]
114
139
JavaComments ,
115
140
/// `"01 02" // comment`
116
141
RustHex ,
117
142
}
118
143
119
144
impl Clarifier {
120
145
/// Creates new Clarifier with buffer, that accumulates comments and hex bytes.
121
- pub fn new ( clarify_buf : Vec < u8 > , flavor : ClarifyFlavor ) -> Self {
146
+ pub fn new ( clarify_buf : Vec < u8 > , options : ClarifyOptions ) -> Self {
122
147
Self {
123
148
clarify_buf,
124
149
125
150
last_position : 0 ,
126
151
depth : Vec :: new ( ) ,
127
152
128
153
indent_enabled : true ,
129
- comment_writer : match flavor {
154
+
155
+ print_types : options. print_types ,
156
+
157
+ comment_writer : match options. flavor {
130
158
ClarifyFlavor :: XmlComments => Box :: new ( XmlCommentWriter :: default ( ) ) ,
131
159
ClarifyFlavor :: JavaComments => Box :: new ( JavaCommentWriter :: default ( ) ) ,
132
160
ClarifyFlavor :: RustHex => Box :: new ( RustHexWriter :: default ( ) ) ,
@@ -137,10 +165,10 @@ impl Clarifier {
137
165
138
166
impl < ' a > ClarifySliceWriter < ' a > {
139
167
/// Create a new encoder with the given byte slice as a backing buffer.
140
- pub fn new ( bytes : & ' a mut [ u8 ] , clarify_buf : Vec < u8 > , flavor : ClarifyFlavor ) -> Self {
168
+ pub fn new ( bytes : & ' a mut [ u8 ] , clarify_buf : Vec < u8 > , options : ClarifyOptions ) -> Self {
141
169
Self {
142
170
writer : SliceWriter :: new ( bytes) ,
143
- clarifier : Clarifier :: new ( clarify_buf, flavor ) ,
171
+ clarifier : Clarifier :: new ( clarify_buf, options ) ,
144
172
}
145
173
}
146
174
@@ -254,8 +282,10 @@ impl Clarifier {
254
282
self . indent_enabled = true ;
255
283
self . depth . push ( writer_pos) ;
256
284
257
- let type_name = strip_transparent_types ( type_name) ;
258
- self . write_clarify_type_str ( "type" , & type_name) ;
285
+ if self . print_types {
286
+ let type_name = strip_transparent_types ( type_name) ;
287
+ self . write_clarify_type_str ( "type" , & type_name) ;
288
+ }
259
289
}
260
290
261
291
fn clarify_end_value_type_str ( & mut self , writer_pos : Option < u32 > , type_name : & str ) {
0 commit comments