pub fn stack(a: char, b: char) -> Option<char> {
let look_for_a = char_to_bits(a);
let look_for_b = char_to_bits(b);
if look_for_b.is_none() || look_for_b.is_none() {
None
} else {
Some(LINE_DRAWING_CHARS[look_for_a.unwrap() | look_for_b.unwrap()])
}
}
#[inline]
pub fn char_to_bits(c: char) -> Option<usize> {
LINE_DRAWING_CHARS
.iter()
.enumerate()
.find(|&(_, &c2)| c == c2)
.map(|tup| tup.0)
}
#[inline]
pub fn bits_to_char(bits: u32) -> char {
if bits >= 16 {
panic!(
"Bit set must be between 0 and 15 inclusive but got {}",
bits
);
}
LINE_DRAWING_CHARS[bits as usize]
}
const LINE_DRAWING_CHARS: [char; 16] = [
' ', '\u{2575}', '\u{2576}', '\u{2514}', '\u{2577}', '\u{2502}', '\u{250c}', '\u{251c}', '\u{2574}', '\u{2518}', '\u{2500}', '\u{2534}', '\u{2510}', '\u{2524}', '\u{252c}', '\u{253c}', ];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn stack_with_empty() {
let base_char = '\u{2514}';
let result = stack(' ', base_char);
assert_eq!(Some(base_char), result);
}
#[test]
#[should_panic(expected = "but got 16")]
fn bits_to_char_panics_on_input_16() {
bits_to_char(16);
}
}