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
/// Marks a data type as compatible with the `ocr()` function.
pub trait Scannable {
/// Converts the type into a String that can be read by `ocr()`.
/// '#' is considered part of a letter and all other chars are considered blank space.
///
/// To implement `normalize()` for your own data type, translate your data into this
/// format:
///
/// .##..###...##.
/// ##..#.#..#.#..#
/// ##..#.###..#...
/// #####.#..#.#...
/// ##..#.#..#.#..#
/// ##..#.###...##.
fn normalize(&self) -> String;
}
impl Scannable for &str {
fn normalize(&self) -> String {
self.to_string()
}
}
impl Scannable for (&Vec<bool>, usize) {
fn normalize(&self) -> String {
let (bools, width) = self;
let width = width.clone();
let mut output = String::new();
let height = bools.len() / width;
for y in 0..height {
for x in 0..width {
output.push(if bools[x + y * width] { '#' } else { '.' });
}
output.push('\n');
}
output
}
}
impl Scannable for (&Vec<char>, usize) {
fn normalize(&self) -> String {
let (chars, width) = self;
let width = width.clone();
let mut output = String::new();
let height = chars.len() / width;
for y in 0..height {
for x in 0..width {
output.push(chars[x + y * width]);
}
output.push('\n');
}
output
}
}
impl Scannable for &Vec<Vec<bool>> {
fn normalize(&self) -> String {
let mut output = String::new();
for row in self.iter() {
for &cell in row.iter() {
output.push(if cell { '#' } else { '.' });
}
output.push('\n');
}
output
}
}
impl Scannable for &Vec<Vec<char>> {
fn normalize(&self) -> String {
let mut output = String::new();
for row in self.iter() {
for &cell in row.iter() {
output.push(cell);
}
output.push('\n');
}
output
}
}