pub struct Scanner {
bytes: Vec<u8>,
cols: usize,
rows: usize,
}
impl Scanner {
fn new(bytes: Vec<u8>, cols: usize, rows: usize) -> Self {
Scanner { bytes, cols, rows }
}
fn index(&self, x: usize, y: usize) -> usize {
x + self.rows * y
}
fn invert_index(&self, index: usize) -> (usize, usize) {
(index % self.rows, index / self.rows)
}
pub fn get(&self, x: usize, y: usize) -> Option<u8> {
self.bytes.get(self.index(x, y)).copied()
}
pub fn bytes<'a>(&'a self) -> impl Iterator<Item = u8> + 'a {
self.bytes.iter().copied()
}
pub fn bytes_indices<'a>(&'a self) -> impl Iterator<Item = ((usize, usize), u8)> + 'a {
self.bytes()
.enumerate()
.map(move |(i, b)| (self.invert_index(i), b))
}
}
pub struct ScannerBuilder {
bytes: Option<Vec<u8>>,
cols: Option<usize>,
rows: Option<usize>,
}
impl ScannerBuilder {
pub fn with_cols(mut self, cols: usize) -> Self {
self.cols = Some(cols);
self
}
pub fn with_rows(mut self, rows: usize) -> Self {
self.rows = Some(rows);
self
}
}