mod generated;
pub use generated::{GLYPH_COUNT, Glyph};
use generated::GLYPHS;
pub const SIDE: usize = 7;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Pixels {
rows: [u8; SIDE],
}
impl Pixels {
#[must_use]
pub const fn new(rows: [u8; SIDE]) -> Self {
Self { rows }
}
#[must_use]
pub fn is_lit(&self, x: u8, y: u8) -> bool {
if usize::from(x) >= SIDE {
return false;
}
self.row(y) & (1 << x) != 0
}
#[must_use]
pub fn row(&self, y: u8) -> u8 {
self.rows.get(usize::from(y)).copied().unwrap_or(0)
}
#[must_use]
pub const fn rows(&self) -> &[u8; SIDE] {
&self.rows
}
}
impl Glyph {
#[must_use]
pub fn pixels(self) -> &'static Pixels {
GLYPHS.get(self.index()).unwrap_or(&GLYPHS[0])
}
}
impl core::fmt::Display for Glyph {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
f.write_str(self.name())
}
}
#[cfg(test)]
mod tests {
use super::{GLYPH_COUNT, Glyph, Pixels, SIDE};
#[test]
fn every_glyph_is_a_drawing_of_its_own() {
assert_eq!(Glyph::ALL.len(), GLYPH_COUNT);
for (index, glyph) in Glyph::ALL.iter().enumerate() {
assert_eq!(glyph.index(), index, "{glyph}");
let pixels = glyph.pixels();
assert!(
pixels.rows().iter().any(|row| *row != 0),
"{glyph} is blank"
);
assert!(
pixels.rows().iter().any(|row| *row != 0x7f),
"{glyph} is solid"
);
for other in Glyph::ALL.iter().skip(index + 1) {
assert_ne!(
pixels,
other.pixels(),
"{glyph} and {other} are one drawing"
);
}
}
}
#[test]
fn a_pixel_is_a_bit_of_its_row() {
let mut rows = [0; SIDE];
rows[0] = 0b000_0001;
rows[6] = 0b100_0000;
let pixels = Pixels::new(rows);
assert!(pixels.is_lit(0, 0));
assert!(!pixels.is_lit(1, 0));
assert!(pixels.is_lit(6, 6));
assert!(!pixels.is_lit(7, 6));
assert!(!pixels.is_lit(0, 7));
assert_eq!(pixels.row(6), 0b100_0000);
assert_eq!(pixels.row(7), 0);
assert_eq!(pixels.rows(), &rows);
}
}