@@ -31,8 +31,7 @@ pub trait EncodeClarifyExt: Encode {
31
31
Err ( err) => return ClarifyOutputs :: from_err ( err) ,
32
32
} ;
33
33
34
- let mut buf: Vec < u8 > = Vec :: new ( ) ;
35
- buf. resize ( u32:: from ( len) as usize , 0u8 ) ;
34
+ let mut buf = vec ! [ 0u8 ; u32 :: from( len) as usize ] ;
36
35
37
36
let mut writer = ClarifySliceWriter :: new ( & mut buf, Vec :: new ( ) , flavor) ;
38
37
let result = self . encode ( & mut writer) ;
@@ -146,74 +145,17 @@ impl<'a> ClarifySliceWriter<'a> {
146
145
}
147
146
}
148
147
149
- // /// Encode a value which impls the [`Encode`] trait.
150
- // pub fn encode<T: Encode>(&mut self, encodable: &T) -> Result<()> {
151
- // self.writer.encode(encodable)
152
- // }
153
-
154
- // /// Return an error with the given [`ErrorKind`], annotating it with
155
- // /// context about where the error occurred.
156
- // pub fn error<T>(&mut self, kind: ErrorKind) -> Result<T> {
157
- // self.writer.error(kind)
158
- // }
159
-
160
- // /// Did the decoding operation fail due to an error?
161
- // pub fn is_failed(&self) -> bool {
162
- // self.writer.is_failed()
163
- // }
164
-
165
- // /// Finish encoding to the buffer, returning a slice containing the data
166
- // /// written to the buffer.
167
- // pub fn finish_internal(&self) -> Result<&'a [u8]> {
168
- // self.writer.finish()
169
- // }
170
-
171
148
/// Finish encoding to the buffer, returning a slice containing the data
172
149
/// written to the buffer.
173
150
pub fn finish ( mut self ) -> ClarifyOutputs < ' a > {
174
151
self . clarifier . flush_line ( ) ;
175
152
176
153
ClarifyOutputs {
177
- raw : self . writer . finish ( ) . map ( |raw| Cow :: Borrowed ( raw ) ) ,
154
+ raw : self . writer . finish ( ) . map ( Cow :: Borrowed ) ,
178
155
clarify_buf : self . clarifier . clarify_buf ,
179
156
}
180
157
}
181
158
182
- // /// Encode a `CONTEXT-SPECIFIC` field with the provided tag number and mode.
183
- // pub fn context_specific<T>(
184
- // &mut self,
185
- // tag_number: TagNumber,
186
- // tag_mode: TagMode,
187
- // value: &T,
188
- // ) -> Result<()>
189
- // where
190
- // T: EncodeValue + Tagged,
191
- // {
192
- // self.writer.context_specific(tag_number, tag_mode, value)
193
- // }
194
-
195
- // /// Encode an ASN.1 `SEQUENCE` of the given length.
196
- // ///
197
- // /// Spawns a nested slice writer which is expected to be exactly the
198
- // /// specified length upon completion.
199
- // pub fn sequence<F>(&mut self, length: Length, f: F) -> Result<()>
200
- // where
201
- // F: FnOnce(&mut DebugSliceWriter<'_>) -> Result<()>,
202
- // {
203
- // Header::new(Tag::Sequence, length).and_then(|header| header.encode(self))?;
204
-
205
- // let debug_ref = self.debug_ref.clone();
206
- // let mut nested_encoder = DebugSliceWriter::new(self.reserve(length)?, debug_ref, true);
207
- // f(&mut nested_encoder)?;
208
-
209
- // let nresult: FinishOutputs<'_> = nested_encoder.finish();
210
- // if nresult.raw?.len() == usize::try_from(length)? {
211
- // Ok(())
212
- // } else {
213
- // self.error(ErrorKind::Length { tag: Tag::Sequence })
214
- // }
215
- // }
216
-
217
159
/// Reserve a portion of the internal buffer, updating the internal cursor
218
160
/// position and returning a mutable slice.
219
161
fn reserve ( & mut self , len : impl TryInto < Length > ) -> Result < & mut [ u8 ] > {
@@ -224,7 +166,7 @@ impl<'a> ClarifySliceWriter<'a> {
224
166
impl Clarifier {
225
167
/// Returns indentation, for example "\n\t" for depth == 1
226
168
pub fn indent_str ( & self ) -> & ' static str {
227
- let ilen = self . depth . len ( ) * 1 ;
169
+ let ilen = self . depth . len ( ) ;
228
170
let ilen = ilen. min ( INDENT_STR . len ( ) ) ;
229
171
& INDENT_STR [ ..ilen]
230
172
}
@@ -256,7 +198,7 @@ impl Clarifier {
256
198
self . flush_line ( ) ;
257
199
258
200
let indent = self . indent_str ( ) ;
259
- write ! ( & mut self . clarify_buf, "\n {}" , indent ) . ok ( ) ;
201
+ write ! ( & mut self . clarify_buf, "\n {indent}" ) . ok ( ) ;
260
202
261
203
// write e.g. '"' before hex
262
204
self . comment_writer . start_new_line ( & mut self . clarify_buf ) ;
@@ -279,13 +221,13 @@ impl Clarifier {
279
221
280
222
/// Writes string to debug output, for example a comment "// SEQUENCE"
281
223
pub fn write_clarify_str ( & mut self , s : & str ) {
282
- write ! ( & mut self . clarify_buf, "{}" , s ) . unwrap ( ) ;
224
+ write ! ( & mut self . clarify_buf, "{s}" ) . ok ( ) ;
283
225
}
284
226
/// Writes string to debug output, for example a comment: `// SEQUENCE: name`
285
227
pub fn write_clarify_type_str ( & mut self , start_end : & str , type_name : & str ) {
286
228
//let mut debugbuf = self.debug_ref.borrow_mut();
287
229
288
- let comment = format ! ( "{}: {} " , start_end , type_name ) ;
230
+ let comment = format ! ( "{start_end }: {type_name } " ) ;
289
231
self . comment_writer . comment ( & comment) ;
290
232
}
291
233
@@ -303,13 +245,15 @@ impl Clarifier {
303
245
304
246
/// Writes int to debug output, for example a comment: `// integer: 16dec`
305
247
pub fn write_clarify_int ( & mut self , value : i64 ) {
306
- if value >= 10 || value < 0 {
248
+ if ! ( 0 .. 10 ) . contains ( & value) {
307
249
let comment = format ! ( "integer: {value}dec " ) ;
308
250
self . comment_writer . comment ( & comment) ;
309
251
}
310
252
}
311
253
312
- /// input: u32::from(self.writer.position())
254
+ /// Writes e.g. `type: OctetString`
255
+ ///
256
+ /// Expected `writer_pos` input: `u32::from(self.writer.position())`
313
257
pub fn clarify_start_value_type_str ( & mut self , writer_pos : Option < u32 > , type_name : & str ) {
314
258
self . indent_enabled = true ;
315
259
self . depth . push ( writer_pos) ;
@@ -321,15 +265,12 @@ impl Clarifier {
321
265
fn clarify_end_value_type_str ( & mut self , writer_pos : Option < u32 > , type_name : & str ) {
322
266
let last_pos = self . depth . pop ( ) . unwrap_or ( writer_pos) ;
323
267
324
- match ( writer_pos, last_pos) {
325
- ( Some ( writer_pos) , Some ( last_pos) ) => {
326
- let diff = writer_pos - last_pos;
327
- if diff < 15 {
328
- // ignore short runs
329
- return ;
330
- }
268
+ if let ( Some ( writer_pos) , Some ( last_pos) ) = ( writer_pos, last_pos) {
269
+ let diff = writer_pos - last_pos;
270
+ if diff < 16 {
271
+ // ignore short runs
272
+ return ;
331
273
}
332
- _ => { }
333
274
}
334
275
335
276
let type_name = strip_transparent_types ( type_name) ;
@@ -382,10 +323,10 @@ impl Clarifier {
382
323
pub fn clarify_header_end_length ( & mut self , tag : Option < & Tag > , length : Length ) {
383
324
self . indent_enabled = true ;
384
325
if let Some ( tag) = tag {
385
- self . write_clarify_type_str ( "tag" , & format ! ( "{}" , tag ) ) ;
326
+ self . write_clarify_type_str ( "tag" , & format ! ( "{tag}" ) ) ;
386
327
}
387
328
if u32:: from ( length) >= 10 {
388
- self . write_clarify_type_str ( "len" , & format ! ( "{}" , length ) ) ;
329
+ self . write_clarify_type_str ( "len" , & format ! ( "{length}" ) ) ;
389
330
}
390
331
}
391
332
@@ -408,6 +349,7 @@ impl Clarifier {
408
349
}
409
350
410
351
impl < ' a > Writer for ClarifySliceWriter < ' a > {
352
+ #[ allow( clippy:: cast_possible_truncation) ]
411
353
fn write ( & mut self , slice : & [ u8 ] ) -> Result < ( ) > {
412
354
self . reserve ( slice. len ( ) ) ?. copy_from_slice ( slice) ;
413
355
self . clarifier . last_position += slice. len ( ) as u32 ;
@@ -434,8 +376,7 @@ fn strip_transparent_types(mut type_name: &str) -> Cow<'_, str> {
434
376
435
377
for prefix in prefixes {
436
378
type_name = if let Some ( stripped) = type_name. strip_prefix ( prefix) {
437
- let stripped = stripped. strip_suffix ( ">" ) . unwrap_or ( stripped) ;
438
- stripped
379
+ stripped. strip_suffix ( ">" ) . unwrap_or ( stripped)
439
380
} else {
440
381
type_name
441
382
} ;
0 commit comments