Skip to content

Commit 24ad828

Browse files
committed
der: clarify: cleanup + clippy
1 parent 21d1b17 commit 24ad828

File tree

5 files changed

+27
-84
lines changed

5 files changed

+27
-84
lines changed

der/src/header.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,15 +73,15 @@ impl Encode for Header {
7373
fn clarify_start_tag(writer: &mut impl Writer, tag: &Tag) {
7474
#[cfg(feature = "clarify")]
7575
if let Some(clarifier) = writer.clarifier() {
76-
clarifier.clarify_header_start_tag(&tag);
76+
clarifier.clarify_header_start_tag(tag);
7777
}
7878
}
7979

8080
#[allow(unused_variables)]
8181
fn clarify_end_length(writer: &mut impl Writer, tag: &Tag, length: Length) {
8282
#[cfg(feature = "clarify")]
8383
if let Some(clarifier) = writer.clarifier() {
84-
clarifier.clarify_header_end_length(Some(&tag), length);
84+
clarifier.clarify_header_end_length(Some(tag), length);
8585
}
8686
}
8787

der/src/writer/clarify.rs

Lines changed: 19 additions & 78 deletions
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ pub trait EncodeClarifyExt: Encode {
3131
Err(err) => return ClarifyOutputs::from_err(err),
3232
};
3333

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];
3635

3736
let mut writer = ClarifySliceWriter::new(&mut buf, Vec::new(), flavor);
3837
let result = self.encode(&mut writer);
@@ -146,74 +145,17 @@ impl<'a> ClarifySliceWriter<'a> {
146145
}
147146
}
148147

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-
171148
/// Finish encoding to the buffer, returning a slice containing the data
172149
/// written to the buffer.
173150
pub fn finish(mut self) -> ClarifyOutputs<'a> {
174151
self.clarifier.flush_line();
175152

176153
ClarifyOutputs {
177-
raw: self.writer.finish().map(|raw| Cow::Borrowed(raw)),
154+
raw: self.writer.finish().map(Cow::Borrowed),
178155
clarify_buf: self.clarifier.clarify_buf,
179156
}
180157
}
181158

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-
217159
/// Reserve a portion of the internal buffer, updating the internal cursor
218160
/// position and returning a mutable slice.
219161
fn reserve(&mut self, len: impl TryInto<Length>) -> Result<&mut [u8]> {
@@ -224,7 +166,7 @@ impl<'a> ClarifySliceWriter<'a> {
224166
impl Clarifier {
225167
/// Returns indentation, for example "\n\t" for depth == 1
226168
pub fn indent_str(&self) -> &'static str {
227-
let ilen = self.depth.len() * 1;
169+
let ilen = self.depth.len();
228170
let ilen = ilen.min(INDENT_STR.len());
229171
&INDENT_STR[..ilen]
230172
}
@@ -256,7 +198,7 @@ impl Clarifier {
256198
self.flush_line();
257199

258200
let indent = self.indent_str();
259-
write!(&mut self.clarify_buf, "\n{}", indent).ok();
201+
write!(&mut self.clarify_buf, "\n{indent}").ok();
260202

261203
// write e.g. '"' before hex
262204
self.comment_writer.start_new_line(&mut self.clarify_buf);
@@ -279,13 +221,13 @@ impl Clarifier {
279221

280222
/// Writes string to debug output, for example a comment "// SEQUENCE"
281223
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();
283225
}
284226
/// Writes string to debug output, for example a comment: `// SEQUENCE: name`
285227
pub fn write_clarify_type_str(&mut self, start_end: &str, type_name: &str) {
286228
//let mut debugbuf = self.debug_ref.borrow_mut();
287229

288-
let comment = format!("{}: {} ", start_end, type_name);
230+
let comment = format!("{start_end}: {type_name} ");
289231
self.comment_writer.comment(&comment);
290232
}
291233

@@ -303,13 +245,15 @@ impl Clarifier {
303245

304246
/// Writes int to debug output, for example a comment: `// integer: 16dec`
305247
pub fn write_clarify_int(&mut self, value: i64) {
306-
if value >= 10 || value < 0 {
248+
if !(0..10).contains(&value) {
307249
let comment = format!("integer: {value}dec ");
308250
self.comment_writer.comment(&comment);
309251
}
310252
}
311253

312-
/// input: u32::from(self.writer.position())
254+
/// Writes e.g. `type: OctetString`
255+
///
256+
/// Expected `writer_pos` input: `u32::from(self.writer.position())`
313257
pub fn clarify_start_value_type_str(&mut self, writer_pos: Option<u32>, type_name: &str) {
314258
self.indent_enabled = true;
315259
self.depth.push(writer_pos);
@@ -321,15 +265,12 @@ impl Clarifier {
321265
fn clarify_end_value_type_str(&mut self, writer_pos: Option<u32>, type_name: &str) {
322266
let last_pos = self.depth.pop().unwrap_or(writer_pos);
323267

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;
331273
}
332-
_ => {}
333274
}
334275

335276
let type_name = strip_transparent_types(type_name);
@@ -382,10 +323,10 @@ impl Clarifier {
382323
pub fn clarify_header_end_length(&mut self, tag: Option<&Tag>, length: Length) {
383324
self.indent_enabled = true;
384325
if let Some(tag) = tag {
385-
self.write_clarify_type_str("tag", &format!("{}", tag));
326+
self.write_clarify_type_str("tag", &format!("{tag}"));
386327
}
387328
if u32::from(length) >= 10 {
388-
self.write_clarify_type_str("len", &format!("{}", length));
329+
self.write_clarify_type_str("len", &format!("{length}"));
389330
}
390331
}
391332

@@ -408,6 +349,7 @@ impl Clarifier {
408349
}
409350

410351
impl<'a> Writer for ClarifySliceWriter<'a> {
352+
#[allow(clippy::cast_possible_truncation)]
411353
fn write(&mut self, slice: &[u8]) -> Result<()> {
412354
self.reserve(slice.len())?.copy_from_slice(slice);
413355
self.clarifier.last_position += slice.len() as u32;
@@ -434,8 +376,7 @@ fn strip_transparent_types(mut type_name: &str) -> Cow<'_, str> {
434376

435377
for prefix in prefixes {
436378
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)
439380
} else {
440381
type_name
441382
};

der/src/writer/clarify/commentwriter.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ impl CommentWriter for JavaCommentWriter {
2323
}
2424

2525
fn before_new_line(&mut self, w: &mut dyn Write) {
26-
if self.buf.len() == 0 {
26+
if self.buf.is_empty() {
2727
return;
2828
}
2929
let _ = w.write_all(b" // ");
@@ -43,7 +43,7 @@ impl CommentWriter for XmlCommentWriter {
4343
}
4444

4545
fn before_new_line(&mut self, w: &mut dyn Write) {
46-
if self.buf.len() == 0 {
46+
if self.buf.is_empty() {
4747
return;
4848
}
4949
let _ = w.write_all(b" <!-- ");
@@ -68,7 +68,7 @@ impl CommentWriter for RustHexWriter {
6868
if self.started_newline {
6969
w.write_all(b"\"").ok();
7070
}
71-
if self.buf.len() == 0 {
71+
if self.buf.is_empty() {
7272
return;
7373
}
7474
w.write_all(b" // ").ok();

der/src/writer/clarify/hexdisplaylines.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ impl fmt::Display for HexDisplayLines<'_, '_> {
2121
first = false;
2222
}
2323
for byte in chunk {
24-
write!(f, "{:02X} ", byte)?;
24+
write!(f, "{byte:02X} ")?;
2525
}
2626
}
2727
Ok(())

der/tests/clarify.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
//! Tests for clarify pretty-printing support.
22
#![cfg(all(feature = "derive", feature = "alloc", feature = "clarify"))]
3+
// TODO: fix needless_question_mark in the derive crate
4+
#![allow(clippy::needless_question_mark)]
35

46
pub mod sequence {
57
use std::{println, str::FromStr};

0 commit comments

Comments
 (0)