Skip to content

Commit f40a338

Browse files
committed
der: add ClarifyOptions
1 parent 6619211 commit f40a338

File tree

2 files changed

+43
-11
lines changed

2 files changed

+43
-11
lines changed

der/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,9 @@ pub use {
396396
};
397397

398398
#[cfg(feature = "clarify")]
399-
pub use writer::clarify::{Clarifier, ClarifyFlavor, ClarifySliceWriter, EncodeClarifyExt};
399+
pub use writer::clarify::{
400+
Clarifier, ClarifyFlavor, ClarifyOptions, ClarifySliceWriter, EncodeClarifyExt,
401+
};
400402

401403
#[cfg(feature = "time")]
402404
pub use time;

der/src/writer/clarify.rs

Lines changed: 40 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -17,23 +17,26 @@ use super::Writer;
1717
pub trait EncodeClarifyExt: Encode {
1818
/// Encode this type as pretty-printed hex DER, with comments.
1919
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+
});
2124
// Propagate encode and finish errors
2225
outputs.raw?;
2326
Ok(String::from_utf8(outputs.clarify_buf).expect("clarified output to be utf-8"))
2427
}
2528

2629
/// Encode this type as pretty-printed hex DER, with comments.
2730
/// 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> {
2932
let len = match self.encoded_len() {
3033
Ok(len) => len,
3134
Err(err) => return ClarifyOutputs::from_err(err),
3235
};
3336

3437
let mut buf = vec![0u8; u32::from(len) as usize];
3538

36-
let mut writer = ClarifySliceWriter::new(&mut buf, Vec::new(), flavor);
39+
let mut writer = ClarifySliceWriter::new(&mut buf, Vec::new(), options);
3740
let result = self.encode(&mut writer);
3841

3942
let outputs = writer.finish();
@@ -50,6 +53,25 @@ pub trait EncodeClarifyExt: Encode {
5053

5154
impl<T> EncodeClarifyExt for T where T: Encode {}
5255

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+
5375
static INDENT_STR: &str =
5476
"\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";
5577

@@ -76,6 +98,8 @@ pub struct Clarifier {
7698
/// Determines if newlines and indent are currently enabled
7799
indent_enabled: bool,
78100

101+
print_types: bool,
102+
79103
/// Sans-io buffer for comments
80104
comment_writer: Box<dyn CommentWriter>,
81105
}
@@ -106,27 +130,31 @@ impl<'a> ClarifyOutputs<'a> {
106130
}
107131

108132
/// Determines how comments will look like
109-
#[derive(Copy, Clone, Debug)]
133+
#[derive(Copy, Clone, Debug, Default)]
110134
pub enum ClarifyFlavor {
111135
/// `01 02 <!-- comment -->`
112136
XmlComments,
113137
/// `01 02 // comment`
138+
#[default]
114139
JavaComments,
115140
/// `"01 02" // comment`
116141
RustHex,
117142
}
118143

119144
impl Clarifier {
120145
/// 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 {
122147
Self {
123148
clarify_buf,
124149

125150
last_position: 0,
126151
depth: Vec::new(),
127152

128153
indent_enabled: true,
129-
comment_writer: match flavor {
154+
155+
print_types: options.print_types,
156+
157+
comment_writer: match options.flavor {
130158
ClarifyFlavor::XmlComments => Box::new(XmlCommentWriter::default()),
131159
ClarifyFlavor::JavaComments => Box::new(JavaCommentWriter::default()),
132160
ClarifyFlavor::RustHex => Box::new(RustHexWriter::default()),
@@ -137,10 +165,10 @@ impl Clarifier {
137165

138166
impl<'a> ClarifySliceWriter<'a> {
139167
/// 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 {
141169
Self {
142170
writer: SliceWriter::new(bytes),
143-
clarifier: Clarifier::new(clarify_buf, flavor),
171+
clarifier: Clarifier::new(clarify_buf, options),
144172
}
145173
}
146174

@@ -254,8 +282,10 @@ impl Clarifier {
254282
self.indent_enabled = true;
255283
self.depth.push(writer_pos);
256284

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+
}
259289
}
260290

261291
fn clarify_end_value_type_str(&mut self, writer_pos: Option<u32>, type_name: &str) {

0 commit comments

Comments
 (0)