diff --git a/src/cp437_font.rs b/src/cp437_font.rs index 1ff34b9..dbb99d7 100644 --- a/src/cp437_font.rs +++ b/src/cp437_font.rs @@ -1,3 +1,4 @@ +use std::ops::Index; use servicepoint::{Bitmap, DataRef, TILE_SIZE}; const CHAR_COUNT: usize = u8::MAX as usize + 1; @@ -10,10 +11,6 @@ impl Cp437Font { pub fn new(bitmaps: [Bitmap; CHAR_COUNT]) -> Self { Self { bitmaps } } - - pub fn get_bitmap(&self, char_code: u8) -> &Bitmap { - &self.bitmaps[char_code as usize] - } } impl Default for Cp437Font { @@ -32,6 +29,14 @@ impl Default for Cp437Font { } } +impl Index for Cp437Font { + type Output = Bitmap; + + fn index(&self, char_code: u8) -> &Self::Output { + &self.bitmaps[char_code as usize] + } +} + /// Font from the display firmware `cape-cccb-apd/cp437font_linear.h` pub(crate) const CP437_FONT_LINEAR: [u64; 256] = [ 0x0000000000000000, // 0x00 diff --git a/src/execute_command.rs b/src/execute_command.rs index 36d4d0a..7a2bf45 100644 --- a/src/execute_command.rs +++ b/src/execute_command.rs @@ -154,11 +154,10 @@ impl<'t> CommandExecutor<'t> { let tile_x = char_x + x; let tile_y = char_y + y; - let bitmap = font.get_bitmap(char_code); match self.print_pixel_grid( tile_x * TILE_SIZE, tile_y * TILE_SIZE, - bitmap, + &font[char_code], ) { Success => {} Failure => { diff --git a/src/font_renderer.rs b/src/font_renderer.rs index 31f3537..64e966e 100644 --- a/src/font_renderer.rs +++ b/src/font_renderer.rs @@ -78,11 +78,8 @@ impl FontRenderer8x8 { .font .as_ref() .glyph_for_char(char) - .or(self.fallback_char); - let glyph_id = match glyph_id { - None => return Err(GlyphNotFound(char)), - Some(val) => val, - }; + .or(self.fallback_char) + .ok_or_else(|| GlyphNotFound(char))?; canvas.pixels.fill(0); self.font.as_ref().rasterize_glyph( @@ -97,8 +94,7 @@ impl FontRenderer8x8 { for y in 0..TILE_SIZE { for x in 0..TILE_SIZE { - let index = x + y * TILE_SIZE; - let canvas_val = canvas.pixels[index] != 0; + let canvas_val = canvas.pixels[x + y * TILE_SIZE] != 0; let bitmap_x = (offset.x + x) as isize; let bitmap_y = (offset.y + y) as isize; if !bitmap.set_optional(bitmap_x, bitmap_y, canvas_val) { diff --git a/src/main.rs b/src/main.rs index e6daaa5..7832f9e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -57,17 +57,9 @@ fn main() { scope.spawn(move || { let mut buf = [0; BUF_SIZE]; while stop_udp_rx.try_recv().is_err() { - let amount = match receive_into_buf(&socket, &mut buf) { - Some(value) => value, - None => continue, - }; - - let command = match command_from_slice(&buf[..amount]) { - Some(value) => value, - None => continue, - }; - - handle_command(&event_proxy, &command_executor, command); + receive_into_buf(&socket, &mut buf) + .and_then(move |amount| command_from_slice(&buf[..amount])) + .map(|cmd| handle_command(&event_proxy, &command_executor, cmd)); } }); event_loop @@ -107,22 +99,12 @@ fn init_logging(debug: bool) { } fn command_from_slice(slice: &[u8]) -> Option { - let package = match servicepoint::Packet::try_from(slice) { - Err(_) => { - warn!("could not load packet with length {}", slice.len()); - return None; - } - Ok(package) => package, - }; - - let command = match Command::try_from(package) { - Err(err) => { - warn!("could not read command for packet: {:?}", err); - return None; - } - Ok(val) => val, - }; - Some(command) + let packet = servicepoint::Packet::try_from(slice) + .inspect_err(|_| warn!("could not load packet with length {}", slice.len())) + .ok()?; + Command::try_from(packet) + .inspect_err(move |err| warn!("could not read command for packet: {:?}", err)) + .ok() } fn receive_into_buf(