wrapbin 0.1.0

Simple binary newtype as wrapped Cow u8 array.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
//!
//! Hexadecimal dump of a file.
//!
//! ```ebnf
//! DumpRepresentation ::= [ HeaderLine ] { '\n' DataLine }
//!
//! HeaderLine ::= PrefixString ' '{4-7} (Column8   | Column16   | Column32  |
//!                          Column2C8 | Column2C16 | Column2C32)
//!
//! DataLine ::= LineIndex  (Column8   | Column16   | Column32  |
//!                          Column2C8 | Column2C16 | Column2C32)
//! LineIndex ::= Nybble{3-6} ': '
//!
//! Column8 ::= Byte ( ' ' Byte ){0-7}
//! Column2C8 ::= Column8 [ ' - ' Column8 ]
//! Column16 ::= Byte ( ' ' Byte ){0-15}
//! Column2C16 ::= Column16 [ ' - ' Column16 ]
//! Column32 ::= Byte ( ' ' Byte ){0-31}
//! Column2C32 ::= Column32 [ ' - ' Column32 ]
//!
//! Byte ::= Nybble Nybble Nybble?
//! Nybble ::= [0-9a-fA-F]
//! ```
//!
//! # Examples
//!
#![cfg_attr(
    any(
        not(feature = "repr-dump"),
        all(feature = "repr-dump", feature = "repr-color")
    ),
    doc = "```ignore"
)]
#![cfg_attr(
    all(feature = "repr-dump", not(feature = "repr-color")),
    doc = "```rust"
)]
//! use wrapbin::{
//!     Binary,
//!     repr::{BinaryFormatOptions, dump::DumpFormatOptions, format, has_color}
//! };
//!
//! let binary = Binary::from([
//!     0x7b_u8,0xe6_u8,0xd4_u8,0xf2_u8,0x25_u8,0x5c_u8,0x62_u8,0xd3_u8,
//!     0x21_u8,0x24_u8,0xab_u8,0x7e_u8,0x40_u8,0xf1_u8,0x7b_u8,0xce_u8,
//!     0x17_u8,0x3c_u8,0x08_u8,0xd2_u8,0xd1_u8,0xce_u8,0xcc_u8,0x17_u8,
//! ]);
//!
//! assert_eq!(
//!     format(
//!         &binary,
//!         DumpFormatOptions::classic_hex_dump()),
//!     vec![
//!         "0X       00 01 02 03 04 05 06 07 - 08 09 0A 0B 0C 0D 0E 0F ",
//!         "000000:  7B E6 D4 F2 25 5C 62 D3 - 21 24 AB 7E 40 F1 7B CE ",
//!         "000010:  17 3C 08 D2 D1 CE CC 17 - ",
//!     ].join("\n")
//! );
//! ```
//!

use crate::{
    Binary,
    error::Error,
    repr::{BinaryFormatOptions, ByteKind, RadixFormat, ReprComponentKind},
};
use alloc::{
    format,
    string::{String, ToString},
};
use core::{
    assert,
    clone::Clone,
    cmp::{Eq, PartialEq},
    default::Default,
    fmt::Debug,
    iter::Iterator,
    marker::Copy,
    option::Option::{self, None, Some},
    result::Result,
    todo, unreachable,
};

// ------------------------------------------------------------------------------------------------
// Public Types
// ------------------------------------------------------------------------------------------------

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct DumpFormatOptions {
    radix_format: RadixFormat,
    compact: bool,
    index_header_line: bool,
    index_line_numbers: bool,
    index_radix_format: RadixFormat,
    column_width: DumpColumnWidth,
    two_columns: bool,
    show_ascii: bool,
    show_extended_ascii: bool,
    line_index_spacing: String,
    value_spacing: String,
    column_separator: char,
    column_index_underline: Option<char>,
    colored: bool,
}

#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
#[repr(usize)]
pub enum DumpColumnWidth {
    #[default]
    Eight = 8,
    Sixteen = 16,
    ThirtyTwo = 32,
}

// ------------------------------------------------------------------------------------------------
// Public Functions
// ------------------------------------------------------------------------------------------------

pub fn dump_representation(value: &Binary<'_>, options: &DumpFormatOptions) -> String {
    // --------------------------------------------------------------------------------------------
    // This is not supported the line indexes get ridiculous.
    // --------------------------------------------------------------------------------------------
    assert!(options.index_radix_format != RadixFormat::Binary);

    let (mid, end) = options.byte_counts();
    let mut buffer = String::default();

    // --------------------------------------------------------------------------------------------
    // Header line(s).
    // --------------------------------------------------------------------------------------------
    if options.index_header_line {
        buffer.push_str(&format!(
            "{:1$}{2:3$}",
            options.radix_format.prefix_str(),
            options.line_index_width(),
            "",
            options.line_index_spacing.len(),
        ));
        for i in 0..end {
            buffer.push_str(&options.format_column_index(i));
            if (i + 1) % end != 0 && options.two_columns && (i + 1) % mid == 0 {
                buffer.push_str(&options.format_column_separator());
            }
        }
        buffer.push('\n');
        if let Some(underline) = options.format_header_underline() {
            buffer.push_str(&format!(
                "{:1$}",
                "",
                options.line_index_width() + options.line_index_spacing.len()
            ));
            buffer.push_str(&underline);
        }
    }

    // --------------------------------------------------------------------------------------------
    // Actual data formatting.
    // --------------------------------------------------------------------------------------------
    for (index, byte) in value.iter().enumerate() {
        let one_index = index + 1;

        if options.index_line_numbers && index == 0 || index % end == 0 {
            buffer.push_str(&options.format_line_index(index));
        }

        if options.show_ascii {
            buffer.push_str(&options.format_ascii_char(byte));
        } else {
            buffer.push_str(&options.format_data_value(*byte));
        }

        if one_index % end == 0 {
            buffer.push('\n');
        } else if options.two_columns && one_index > 0 && one_index % mid == 0 {
            buffer.push_str(&options.format_column_separator());
        }
    }
    buffer
}

pub fn parse_dump_representation(_s: &str) -> Result<Binary<'_>, Error> {
    todo!()
}

// ------------------------------------------------------------------------------------------------
// Implementations
// ------------------------------------------------------------------------------------------------

impl From<DumpFormatOptions> for BinaryFormatOptions {
    fn from(value: DumpFormatOptions) -> Self {
        Self::Dump(value)
    }
}

impl Default for DumpFormatOptions {
    fn default() -> Self {
        Self {
            radix_format: RadixFormat::default(),
            compact: false,
            index_header_line: true,
            index_line_numbers: true,
            index_radix_format: RadixFormat::default(),
            column_width: DumpColumnWidth::default(),
            two_columns: true,
            show_ascii: false,
            show_extended_ascii: false,
            line_index_spacing: ":  ".to_string(),
            value_spacing: " ".to_string(),
            column_separator: '',
            column_index_underline: Some(''),
            colored: cfg!(feature = "repr-color"),
        }
    }
}

impl DumpFormatOptions {
    pub fn classic_hex_dump() -> Self {
        Self::default()
            .with_upper_hex_bytes()
            .with_upper_hex_indices()
            .compact(false)
            .two_columns_of(DumpColumnWidth::Eight)
            .no_column_index_underline()
            .separate_columns_with('-')
            .show_ascii(false)
    }

    pub fn ascii_hex_dump() -> Self {
        Self::default()
            .with_upper_hex_bytes()
            .with_upper_hex_indices()
            .compact(false)
            .two_columns_of(DumpColumnWidth::Eight)
            .no_column_index_underline()
            .separate_columns_with('-')
            .show_extended_ascii(true)
    }

    pub fn hex_dump() -> Self {
        Self::default()
            .with_upper_hex_bytes()
            .with_upper_hex_indices()
            .compact(false)
            .two_columns_of(DumpColumnWidth::Eight)
            .show_ascii(false)
    }

    pub fn octal_dump() -> Self {
        Self::default()
            .with_octal_bytes()
            .with_octal_indices()
            .compact(false)
            .two_columns_of(DumpColumnWidth::Eight)
            .show_ascii(false)
    }

    pub fn binary_dump() -> Self {
        Self::default()
            .with_binary_bytes()
            .with_upper_hex_indices()
            .compact(false)
            .two_columns_of(DumpColumnWidth::Eight)
            .show_ascii(false)
    }

    pub fn decimal_dump() -> Self {
        Self::default()
            .with_decimal_bytes()
            .with_decimal_indices()
            .compact(false)
            .two_columns_of(DumpColumnWidth::Eight)
            .show_ascii(false)
    }

    pub fn lower_hex_dump() -> Self {
        Self::default()
            .with_lower_hex_bytes()
            .with_lower_hex_indices()
            .compact(false)
            .two_columns_of(DumpColumnWidth::Eight)
            .show_ascii(false)
    }

    /// Sets the radix format for each byte in the array to be one of the values of the enum
    /// [`RadixFormat`].
    pub fn with_byte_radix_format(mut self, radix_format: RadixFormat) -> Self {
        self.radix_format = radix_format;
        self
    }
    /// Sets the radix format for each byte in the array to [`RadixFormat::Binary`].
    pub fn with_binary_bytes(self) -> Self {
        Self::with_byte_radix_format(self, RadixFormat::Binary)
    }
    /// Sets the radix format for each byte in the array to [`RadixFormat::Decimal`].
    pub fn with_decimal_bytes(self) -> Self {
        Self::with_byte_radix_format(self, RadixFormat::Decimal)
    }
    /// Sets the radix format for each byte in the array to [`RadixFormat::LowerHex`].
    pub fn with_lower_hex_bytes(self) -> Self {
        Self::with_byte_radix_format(self, RadixFormat::LowerHex)
    }
    /// Sets the radix format for each byte in the array to [`RadixFormat::Octal`].
    pub fn with_octal_bytes(self) -> Self {
        Self::with_byte_radix_format(self, RadixFormat::Octal)
    }
    /// Sets the radix format for each byte in the array to [`RadixFormat::UpperHex`].
    pub fn with_upper_hex_bytes(self) -> Self {
        Self::with_byte_radix_format(self, RadixFormat::UpperHex)
    }

    pub fn has_index_header_line(mut self, header_line: bool) -> Self {
        self.index_header_line = header_line;
        self
    }

    pub fn underline_column_index_with(mut self, underline: char) -> Self {
        self.column_index_underline = Some(underline);
        self
    }

    pub fn no_column_index_underline(mut self) -> Self {
        self.column_index_underline = None;
        self
    }

    pub fn separate_columns_with(mut self, column_separator: char) -> Self {
        self.column_separator = column_separator;
        self
    }

    pub fn has_index_line_numbers(mut self, line_numbers: bool) -> Self {
        self.index_line_numbers = line_numbers;
        self
    }

    /// Sets the radix format for line and column indices to be one of the values
    /// of the enum [`RadixFormat`].
    pub fn with_index_radix_format(mut self, index_radix_format: RadixFormat) -> Self {
        self.index_radix_format = index_radix_format;
        self
    }
    /// Sets the radix format for line and column indices to [`RadixFormat::Decimal`].
    pub fn with_decimal_indices(self) -> Self {
        Self::with_index_radix_format(self, RadixFormat::Decimal)
    }
    /// Sets the radix format for line and column indices to [`RadixFormat::LowerHex`].
    pub fn with_lower_hex_indices(self) -> Self {
        Self::with_index_radix_format(self, RadixFormat::LowerHex)
    }
    /// Sets the radix format for line and column indices to [`RadixFormat::Octal`].
    pub fn with_octal_indices(self) -> Self {
        Self::with_index_radix_format(self, RadixFormat::Octal)
    }
    /// Sets the radix format for line and column indices to [`RadixFormat::UpperHex`].
    pub fn with_upper_hex_indices(self) -> Self {
        Self::with_index_radix_format(self, RadixFormat::UpperHex)
    }

    pub fn has_two_columns(mut self, two_columns: bool) -> Self {
        self.two_columns = two_columns;
        self
    }

    pub fn with_column_width(mut self, column_width: DumpColumnWidth) -> Self {
        self.column_width = column_width;
        self
    }

    pub fn one_column_of(mut self, column_width: DumpColumnWidth) -> Self {
        self.two_columns = false;
        self.column_width = column_width;
        self
    }

    pub fn two_columns_of(mut self, column_width: DumpColumnWidth) -> Self {
        self.two_columns = true;
        self.column_width = column_width;
        self
    }

    pub fn compact(mut self, compact: bool) -> Self {
        self.compact = compact;
        self
    }

    pub fn show_ascii(mut self, show_ascii: bool) -> Self {
        self = self.with_upper_hex_bytes();
        self.show_ascii = show_ascii;
        self
    }

    pub fn show_extended_ascii(mut self, show_extended_ascii: bool) -> Self {
        self = self.show_ascii(true);
        self.show_extended_ascii = show_extended_ascii;
        self
    }

    /// Use color to denote byte kind according the ASCII conventions denoted by the
    /// enums `ByteStyle` and `ReprStyle`.
    #[cfg(feature = "repr-color")]
    pub fn use_color(mut self, colored: bool) -> Self {
        self.colored = colored;
        self
    }

    const fn byte_counts(&self) -> (usize, usize) {
        match (self.two_columns, self.column_width) {
            (false, w @ DumpColumnWidth::Eight) => (0, w.byte_count()),
            (true, w @ DumpColumnWidth::Eight) => (w.byte_count(), w.two_column_byte_count()),
            (false, w @ DumpColumnWidth::Sixteen) => (0, w.byte_count()),
            (true, w @ DumpColumnWidth::Sixteen) => (w.byte_count(), w.two_column_byte_count()),
            (false, w @ DumpColumnWidth::ThirtyTwo) => (0, w.byte_count()),
            (true, w @ DumpColumnWidth::ThirtyTwo) => (w.byte_count(), w.two_column_byte_count()),
        }
    }

    fn format_column_index(&self, index: usize) -> String {
        let style = ReprComponentKind::Index.display_style(self.colored);
        match self.radix_format {
            RadixFormat::Binary => {
                format!(
                    "{style}{index:00$b}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
            RadixFormat::Decimal => {
                format!(
                    "{style}{index:00$}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
            RadixFormat::Octal => {
                format!(
                    "{style}{index:00$o}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
            RadixFormat::LowerHex => {
                format!(
                    "{style}{index:00$x}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
            RadixFormat::UpperHex => {
                format!(
                    "{style}{index:00$X}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
        }
    }

    fn format_header_underline(&self) -> Option<String> {
        if let Some(underline) = self.column_index_underline {
            let width =
                (self.data_value_width() + self.value_spacing.len()) * self.column_width as usize;
            let style = ReprComponentKind::Separator.display_style(self.colored);
            let underline = format!("{style}{}{style:#}", underline.to_string().repeat(width));
            let mut buffer = String::default();
            buffer.push_str(&underline);
            if self.two_columns {
                buffer.push_str(&self.format_column_separator());
                buffer.push_str(&underline);
            }
            buffer.push('\n');
            Some(buffer)
        } else {
            None
        }
    }

    fn format_column_separator(&self) -> String {
        let style = ReprComponentKind::Separator.display_style(self.colored);
        format!(
            "{style}{}{}{style:#}",
            self.column_separator, self.value_spacing
        )
    }

    const fn line_index_width(&self) -> usize {
        match self.index_radix_format {
            RadixFormat::Decimal | RadixFormat::Octal => 8,
            RadixFormat::LowerHex | RadixFormat::UpperHex => 6,
            _ => unreachable!(),
        }
    }

    fn format_line_index(&self, index: usize) -> String {
        let style = ReprComponentKind::Index.display_style(self.colored);
        match self.index_radix_format {
            RadixFormat::Decimal => format!(
                "{style}{index:0width$}{spacer}{style:#}",
                width = self.line_index_width(),
                spacer = self.line_index_spacing
            ),
            RadixFormat::Octal => format!(
                "{style}{index:0width$o}{spacer}{style:#}",
                width = self.line_index_width(),
                spacer = self.line_index_spacing
            ),
            RadixFormat::LowerHex => format!(
                "{style}{index:0width$x}{spacer}{style:#}",
                width = self.line_index_width(),
                spacer = self.line_index_spacing
            ),
            RadixFormat::UpperHex => format!(
                "{style}{index:0width$X}{spacer}{style:#}",
                width = self.line_index_width(),
                spacer = self.line_index_spacing
            ),
            _ => unreachable!(),
        }
    }

    const fn data_value_width(&self) -> usize {
        match self.radix_format {
            RadixFormat::Binary => 8,
            RadixFormat::Decimal | RadixFormat::Octal => 3,
            RadixFormat::LowerHex | RadixFormat::UpperHex => 2,
        }
    }

    fn format_data_value(&self, byte: u8) -> String {
        let style = ByteKind::ascii_char_display_style(&byte, self.colored);
        match self.radix_format {
            RadixFormat::Binary => {
                format!(
                    "{style}{byte:00$b}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
            RadixFormat::Decimal => {
                format!(
                    "{style}{byte:00$}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
            RadixFormat::Octal => {
                format!(
                    "{style}{byte:00$o}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
            RadixFormat::LowerHex => {
                format!(
                    "{style}{byte:00$x}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
            RadixFormat::UpperHex => {
                format!(
                    "{style}{byte:00$X}{style:#}{spacing}",
                    self.data_value_width(),
                    spacing = self.value_spacing
                )
            }
        }
    }

    fn format_ascii_char(&self, byte: &u8) -> String {
        // This follows ISO 8859-1.
        let decoded_char = match byte {
            // 7-bit ASCII control characters
            0x00 if self.show_extended_ascii => Some(''),
            0x01 if self.show_extended_ascii => Some(''),
            0x02 if self.show_extended_ascii => Some(''),
            0x03 if self.show_extended_ascii => Some(''),
            0x04 if self.show_extended_ascii => Some(''),
            0x05 if self.show_extended_ascii => Some(''),
            0x06 if self.show_extended_ascii => Some(''),
            0x07 if self.show_extended_ascii => Some(''),
            0x08 if self.show_extended_ascii => Some(''),
            0x09 if self.show_extended_ascii => Some(''),
            0x0A if self.show_extended_ascii => Some(''),
            0x0B if self.show_extended_ascii => Some(''),
            0x0C if self.show_extended_ascii => Some(''),
            0x0D if self.show_extended_ascii => Some(''),
            0x0E if self.show_extended_ascii => Some(''),
            0x0F if self.show_extended_ascii => Some(''),
            0x10 if self.show_extended_ascii => Some(''),
            0x11 if self.show_extended_ascii => Some(''),
            0x12 if self.show_extended_ascii => Some(''),
            0x13 if self.show_extended_ascii => Some(''),
            0x14 if self.show_extended_ascii => Some(''),
            0x15 if self.show_extended_ascii => Some(''),
            0x16 if self.show_extended_ascii => Some(''),
            0x17 if self.show_extended_ascii => Some(''),
            0x18 if self.show_extended_ascii => Some(''),
            0x19 if self.show_extended_ascii => Some(''),
            0x1A if self.show_extended_ascii => Some(''),
            0x1B if self.show_extended_ascii => Some(''),
            0x1C if self.show_extended_ascii => Some(''),
            0x1D if self.show_extended_ascii => Some(''),
            0x1E if self.show_extended_ascii => Some(''),
            0x1F if self.show_extended_ascii => Some(''),
            0x20 if self.show_extended_ascii => Some(''),
            // Printable 7-bit ASCII characters.
            0x21..=0x7E => Some(*byte as char),
            // 7-bit ASCII control character
            0x7F if self.show_extended_ascii => Some(''),
            // 8-bit Extended ASCII characters
            // 0x80..=0x9F: Not defined by ISO 8859-1.
            0xA0 if self.show_extended_ascii => Some(''), // NBSP
            // AD: SHY (soft hyphen)
            // Printable 8-bit ASCII characters.
            0xA1..=0xAC | 0xAE..=0xFF => Some(*byte as char),
            _ => None, // Non-printable characters
        };
        let style = ByteKind::ascii_char_display_style(byte, self.colored);
        if let Some(c) = decoded_char {
            format!(
                "{style}{c:0$}{style:#}{spacing}",
                self.data_value_width(),
                spacing = self.value_spacing
            )
        } else {
            format!(
                "{style}{byte:00$X}{style:#}{spacing}",
                self.data_value_width(),
                spacing = self.value_spacing
            )
        }
    }
}

// ------------------------------------------------------------------------------------------------
// Implementations > DumpColumnWidth
// ------------------------------------------------------------------------------------------------

impl DumpColumnWidth {
    #[inline(always)]
    pub const fn byte_count(&self) -> usize {
        *self as usize
    }

    #[inline(always)]
    pub const fn two_column_byte_count(&self) -> usize {
        self.byte_count() * 2
    }
}