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
//! matrix module

use std::{
    default,
    ops::{Add, Div},
};

#[derive(Default, Clone)]
pub struct Line {
    no: String,
    hex: String,
    info: String,
}

impl Line {
    pub fn no(&self) -> &str {
        &self.no
    }
    pub fn hex(&self) -> &str {
        &self.hex
    }
    pub fn info(&self) -> &str {
        &self.info
    }
    pub fn push_no(&mut self, no: String) {
        self.no.push_str(&no);
    }
    pub fn push_hex(&mut self, hex: String) {
        self.hex.push_str(&hex);
    }
    pub fn push_info(&mut self, info: String) {
        self.info.push_str(&info);
    }
    pub fn set_no(&mut self, no: String) {
        self.no = no;
    }
    pub fn set_hex(&mut self, hex: String) {
        self.hex = hex;
    }
    pub fn set_info(&mut self, info: String) {
        self.info = info;
    }
    pub fn to_string(&self) -> String {
        format!("{} {} | {}", self.no, self.hex, self.info)
    }
}

pub struct Matrix {
    width: usize,
    b_arr: Vec<u8>,
    lines: Vec<Line>,
}

/// Hexadecimal matrix structure
///
/// # Examples
///
/// ```rust
/// let arr = "".as_bytes();
/// let mut m = Matrix::new(20, arr.to_vec());
/// ```
impl Matrix {
    pub fn new(w: usize, b: Vec<u8>) -> Self {
        Self {
            width: w,
            b_arr: b.clone(),
            lines: Self::to_matrix(w, b.clone()),
        }
    }

    /// print matrix
    ///
    /// # Examples
    ///
    /// ```rust
    /// let arr = "".as_bytes();
    /// let mut m = Matrix::new(20, arr.to_vec());
    /// m.print_matrix();
    /// ```
    pub fn print_matrix(&mut self) {
        for (i, v) in self.lines.iter().enumerate() {
            println!("{}", v.to_string());
        }
    }

    /// reset matrix
    ///
    /// # Examples
    ///
    /// ```rust
    /// let arr = "".as_bytes();
    /// let mut m = Matrix::new(20, arr.to_vec());
    ///
    /// let arr2 = "".as_bytes();
    /// m.reset(arr2);
    /// ```
    pub fn reset(&mut self, b: &[u8]) {
        self.b_arr = b.to_vec();
        Self::to_matrix(self.width, b.to_vec());
    }

    /// reset matrix
    ///
    /// # Examples
    ///
    /// ```rust
    /// let arr = "".as_bytes();
    /// let mut m = Matrix::new(20, arr.to_vec());
    ///
    /// let line = m.get_line_by_index(index);
    /// ```
    pub fn get_line_by_index(&mut self, index: usize) -> &mut Line {
        self.lines.get_mut(index).unwrap()
    }

    /// reset matrix
    ///
    /// # Examples
    ///
    /// ```rust
    /// let arr = "".as_bytes();
    /// let mut m = Matrix::new(20, arr.to_vec());
    ///
    /// println("{}", m.line_count());
    /// ```
    pub fn line_count(&self) -> usize {
        self.lines.len()
    }

    fn to_matrix(width: usize, b_arr: Vec<u8>) -> Vec<Line> {
        let len = b_arr.len();
        let mut lines: Vec<Line> = vec![];
        let mut line = Line::default();
        // max line count
        let mut max_line_count = len.div(width);
        max_line_count = if max_line_count % width > 0 {
            max_line_count.add(1)
        } else {
            max_line_count
        };
        // max line number
        let mut max_line_number = String::from(format!("{}", (max_line_count * width))).len();
        // current line number
        let mut current_line_number: usize = 0;
        for (i, b) in b_arr.iter().enumerate() {
            // line number
            let line_number: usize = current_line_number * width;
            line.push_hex(format!("{:<02X} ", b));
            if i % width == width - 1 || i == len - 1 {
                for _j in 0..width - 1 - (i % (width)) {
                    line.push_hex("00 ".to_string());
                }
                for j in i - i % width..i + 1 {
                    if b_arr[j].is_ascii_alphabetic() {
                        line.push_info(format!("{}", b_arr[j] as char));
                    } else {
                        line.push_info(".".to_string());
                    }
                }
                let mut fill_zero = String::from("");
                for i in 0..(max_line_number - line_number.to_string().len()) {
                    fill_zero.push_str("0");
                }
                line.set_no(format!("{}{}", fill_zero, line_number.to_string()));
                lines.push(line.clone());
                line = Line::default();
                current_line_number += 1;
            }
        }
        lines
    }
}