hex_matrix/
matrix.rs

1//! matrix module
2
3use std::{
4    default,
5    ops::{Add, Div},
6};
7
8#[derive(Default, Clone)]
9pub struct Line {
10    no: String,
11    hex: String,
12    info: String,
13}
14
15impl Line {
16    pub fn no(&self) -> &str {
17        &self.no
18    }
19    pub fn hex(&self) -> &str {
20        &self.hex
21    }
22    pub fn info(&self) -> &str {
23        &self.info
24    }
25    pub fn push_no(&mut self, no: String) {
26        self.no.push_str(&no);
27    }
28    pub fn push_hex(&mut self, hex: String) {
29        self.hex.push_str(&hex);
30    }
31    pub fn push_info(&mut self, info: String) {
32        self.info.push_str(&info);
33    }
34    pub fn set_no(&mut self, no: String) {
35        self.no = no;
36    }
37    pub fn set_hex(&mut self, hex: String) {
38        self.hex = hex;
39    }
40    pub fn set_info(&mut self, info: String) {
41        self.info = info;
42    }
43    pub fn to_string(&self) -> String {
44        format!("{} {} | {}", self.no, self.hex, self.info)
45    }
46}
47
48pub struct Matrix {
49    width: usize,
50    b_arr: Vec<u8>,
51    lines: Vec<Line>,
52}
53
54/// Hexadecimal matrix structure
55///
56/// # Examples
57///
58/// ```rust
59/// let arr = "".as_bytes();
60/// let mut m = Matrix::new(20, arr.to_vec());
61/// ```
62impl Matrix {
63    pub fn new(w: usize, b: Vec<u8>) -> Self {
64        Self {
65            width: w,
66            b_arr: b.clone(),
67            lines: Self::to_matrix(w, b.clone()),
68        }
69    }
70
71    /// print matrix
72    ///
73    /// # Examples
74    ///
75    /// ```rust
76    /// let arr = "".as_bytes();
77    /// let mut m = Matrix::new(20, arr.to_vec());
78    /// m.print_matrix();
79    /// ```
80    pub fn print_matrix(&mut self) {
81        for (i, v) in self.lines.iter().enumerate() {
82            println!("{}", v.to_string());
83        }
84    }
85
86    /// reset matrix
87    ///
88    /// # Examples
89    ///
90    /// ```rust
91    /// let arr = "".as_bytes();
92    /// let mut m = Matrix::new(20, arr.to_vec());
93    ///
94    /// let arr2 = "".as_bytes();
95    /// m.reset(arr2);
96    /// ```
97    pub fn reset(&mut self, b: &[u8]) {
98        self.b_arr = b.to_vec();
99        Self::to_matrix(self.width, b.to_vec());
100    }
101
102    /// reset matrix
103    ///
104    /// # Examples
105    ///
106    /// ```rust
107    /// let arr = "".as_bytes();
108    /// let mut m = Matrix::new(20, arr.to_vec());
109    ///
110    /// let line = m.get_line_by_index(index);
111    /// ```
112    pub fn get_line_by_index(&mut self, index: usize) -> &mut Line {
113        self.lines.get_mut(index).unwrap()
114    }
115
116    /// reset matrix
117    ///
118    /// # Examples
119    ///
120    /// ```rust
121    /// let arr = "".as_bytes();
122    /// let mut m = Matrix::new(20, arr.to_vec());
123    ///
124    /// println("{}", m.line_count());
125    /// ```
126    pub fn line_count(&self) -> usize {
127        self.lines.len()
128    }
129
130    /// get matrix line
131    ///
132    /// # Examples
133    ///
134    /// ```rust
135    /// let arr = "".as_bytes();
136    /// let mut m = Matrix::new(20, arr.to_vec());
137    ///
138    /// let lines : Vec<Line> = m.lines();
139    /// for (i,v) in lines.iter().enumerate()
140    /// {
141    ///   // ....
142    /// }
143    /// ```
144    pub fn lines(&self) -> Vec<Line> {
145        self.lines.clone()
146    }
147
148    fn to_matrix(width: usize, b_arr: Vec<u8>) -> Vec<Line> {
149        let len = b_arr.len();
150        let mut lines: Vec<Line> = vec![];
151        let mut line = Line::default();
152        // max line count
153        let mut max_line_count = len.div(width);
154        max_line_count = if max_line_count % width > 0 {
155            max_line_count.add(1)
156        } else {
157            max_line_count
158        };
159        // max line number
160        let mut max_line_number = String::from(format!("{}", (max_line_count * width))).len();
161        // current line number
162        let mut current_line_number: usize = 0;
163        for (i, b) in b_arr.iter().enumerate() {
164            // line number
165            let line_number: usize = current_line_number * width;
166            line.push_hex(format!("{:<02X} ", b));
167            if i % width == width - 1 || i == len - 1 {
168                for _j in 0..width - 1 - (i % (width)) {
169                    line.push_hex("00 ".to_string());
170                }
171                for j in i - i % width..i + 1 {
172                    if b_arr[j].is_ascii_alphabetic() {
173                        line.push_info(format!("{}", b_arr[j] as char));
174                    } else {
175                        line.push_info(".".to_string());
176                    }
177                }
178                let mut fill_zero = String::from("");
179                for i in 0..(max_line_number - line_number.to_string().len()) {
180                    fill_zero.push_str("0");
181                }
182                line.set_no(format!("{}{}", fill_zero, line_number.to_string()));
183                lines.push(line.clone());
184                line = Line::default();
185                current_line_number += 1;
186            }
187        }
188        lines
189    }
190}