From c738eb0518e064fddd5f5e9fbb49382785463b12 Mon Sep 17 00:00:00 2001 From: pjht Date: Mon, 24 Oct 2022 08:50:23 -0500 Subject: [PATCH] Fix some pedantic and nursery lints --- src/backplane.rs | 4 ++-- src/card.rs | 8 ++++---- src/location.rs | 8 ++++---- src/main.rs | 13 ++++++------- src/peek.rs | 43 +++++++++++++++++++++---------------------- src/rom.rs | 2 +- src/storage.rs | 2 +- 7 files changed, 39 insertions(+), 41 deletions(-) diff --git a/src/backplane.rs b/src/backplane.rs index 3a1db40..cf0cde2 100644 --- a/src/backplane.rs +++ b/src/backplane.rs @@ -3,7 +3,7 @@ use serde_yaml::Mapping; use thiserror::Error; use crate::{ - card::{Card, CardType}, + card::{self, Card}, m68k::BusError, }; @@ -39,7 +39,7 @@ impl Backplane { return Err(CardAddError::BackplaneFull.into()); } self.cards.push( - inventory::iter::() + inventory::iter::() .find(|card_type| card_type.name == type_name) .ok_or(CardAddError::InvalidType)? .new_card(config)?, diff --git a/src/card.rs b/src/card.rs index 937a840..f2016fb 100644 --- a/src/card.rs +++ b/src/card.rs @@ -3,12 +3,12 @@ use nullable_result::NullableResult; use serde_yaml::Mapping; use std::fmt::{Debug, Display}; -pub struct CardType { +pub struct Type { pub name: &'static str, new: fn(data: &Mapping) -> anyhow::Result>, } -impl CardType { +impl Type { pub const fn new(name: &'static str) -> Self { Self { name, @@ -21,7 +21,7 @@ impl CardType { } } -inventory::collect!(CardType); +inventory::collect!(Type); pub trait Card: Debug + Display { fn new(data: &Mapping) -> anyhow::Result @@ -126,7 +126,7 @@ pub const fn u16_get_be_byte(val: u16, idx: u8) -> u8 { macro_rules! register { ($typ: ty, $name: literal) => { paste::paste! { - inventory::submit!($crate::card::CardType::new::<$typ>($name)); + inventory::submit!($crate::card::Type::new::<$typ>($name)); } }; } diff --git a/src/location.rs b/src/location.rs index a072dd2..05f4c94 100644 --- a/src/location.rs +++ b/src/location.rs @@ -16,20 +16,20 @@ impl Location { } } - pub fn displayer<'a>(&'a self, symbol_tables: &'a SymbolTables) -> LocationDisplayer<'a> { - LocationDisplayer { + pub fn displayer<'a>(&'a self, symbol_tables: &'a SymbolTables) -> Displayer<'a> { + Displayer { location: self, symbol_tables, } } } -pub struct LocationDisplayer<'a> { +pub struct Displayer<'a> { location: &'a Location, symbol_tables: &'a SymbolTables, } -impl<'a> Display for LocationDisplayer<'a> { +impl<'a> Display for Displayer<'a> { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { match self.location { Location::Symbol((table, sym)) => f.write_fmt(format_args!( diff --git a/src/main.rs b/src/main.rs index 6bdd31e..32e6a46 100644 --- a/src/main.rs +++ b/src/main.rs @@ -25,7 +25,6 @@ use disas::DisassemblyError; use indexmap::IndexSet; use itertools::Itertools; use parse_int::parse; -use peek::{PeekFormat, PeekSize}; use reedline_repl_rs::{ clap::{builder::BoolishValueParser, Arg, ArgAction, Command}, Repl, @@ -201,7 +200,7 @@ fn main() -> Result<(), anyhow::Error> { .get_one::("stop_addr") .map(|s| state.symbol_tables.parse_location_address(s)) .transpose()?; - if stop_addr.map(|a| state.cpu.pc() == a).unwrap_or(false) + if stop_addr.map_or(false, |a| state.cpu.pc() == a) || state.symbol_tables.breakpoint_set_at(state.cpu.pc()) || state.address_breakpoints.contains(&state.cpu.pc()) { @@ -252,8 +251,8 @@ fn main() -> Result<(), anyhow::Error> { if fmt_str.len() != 2 { return Err(anyhow!("Peek format length must be 2")); } - let fmt = PeekFormat::try_from(fmt_str.chars().next().unwrap())?; - let size = PeekSize::try_from(fmt_str.chars().nth(1).unwrap())?; + let fmt = peek::Format::try_from(fmt_str.chars().next().unwrap())?; + let size = peek::Size::try_from(fmt_str.chars().nth(1).unwrap())?; let count = parse::(args.get_one::("count").map_or("1", String::as_str))?; let addr = state .symbol_tables @@ -262,9 +261,9 @@ fn main() -> Result<(), anyhow::Error> { let bus = state.cpu.bus_mut(); for i in 0..count { match size { - PeekSize::Byte => data.push(bus.read_byte(addr + i)? as u32), - PeekSize::Word => data.push(bus.read_word(addr + (i * 2))? as u32), - PeekSize::LongWord => data.push( + peek::Size::Byte => data.push(bus.read_byte(addr + i)? as u32), + peek::Size::Word => data.push(bus.read_word(addr + (i * 2))? as u32), + peek::Size::LongWord => data.push( (bus.read_word(addr + (i * 4))? as u32) << 16 | (bus.read_word(addr + (i * 4) + 2)? as u32), ), diff --git a/src/peek.rs b/src/peek.rs index f01bfe8..b132ff5 100644 --- a/src/peek.rs +++ b/src/peek.rs @@ -1,7 +1,7 @@ use thiserror::Error; #[derive(Copy, Clone, Debug)] -pub enum PeekFormat { +pub enum Format { Octal, Hex, Decimal, @@ -9,16 +9,16 @@ pub enum PeekFormat { Binary, } -impl PeekFormat { - pub fn format(self, num: u32, size: PeekSize) -> String { +impl Format { + pub fn format(self, num: u32, size: Size) -> String { match self { Self::Octal => format!("Oo{:0>width$o}", num, width = size.byte_count()), Self::Hex => format!("0x{:0>width$x}", num, width = size.byte_count() * 2), Self::Decimal => { let num = match size { - PeekSize::Byte => num as u8 as i8 as i32, - PeekSize::Word => num as u16 as i16 as i32, - PeekSize::LongWord => num as i32, + Size::Byte => num as u8 as i8 as i32, + Size::Word => num as u16 as i16 as i32, + Size::LongWord => num as i32, }; format!("{}", num) } @@ -30,10 +30,10 @@ impl PeekFormat { #[derive(Debug, Copy, Clone, Error)] #[error("Invalid peek format")] -pub struct InvalidPeekFormat; +pub struct InvalidFormat; -impl TryFrom for PeekFormat { - type Error = InvalidPeekFormat; +impl TryFrom for Format { + type Error = InvalidFormat; fn try_from(value: char) -> Result { match value { @@ -42,49 +42,48 @@ impl TryFrom for PeekFormat { 'd' => Ok(Self::Decimal), 'u' => Ok(Self::UnsignedDecimal), 'b' => Ok(Self::Binary), - _ => Err(InvalidPeekFormat), + _ => Err(InvalidFormat), } } } #[derive(Copy, Clone, Debug)] -pub enum PeekSize { +pub enum Size { Byte, Word, LongWord, } -impl PeekSize { +impl Size { pub fn byte_count(self) -> usize { match self { - PeekSize::Byte => 1, - PeekSize::Word => 2, - PeekSize::LongWord => 4, + Self::Byte => 1, + Self::Word => 2, + Self::LongWord => 4, } } pub fn chunk_size(self) -> usize { match self { - PeekSize::Byte => 8, - PeekSize::Word => 8, - PeekSize::LongWord => 4, + Self::Byte | Self::Word => 8, + Self::LongWord => 4, } } } #[derive(Debug, Copy, Clone, Error)] #[error("Invalid peek size")] -pub struct InvalidPeekSize; +pub struct InvalidSize; -impl TryFrom for PeekSize { - type Error = InvalidPeekSize; +impl TryFrom for Size { + type Error = InvalidSize; fn try_from(value: char) -> Result { match value { 'b' => Ok(Self::Byte), 'w' => Ok(Self::Word), 'l' => Ok(Self::LongWord), - _ => Err(InvalidPeekSize), + _ => Err(InvalidSize), } } } diff --git a/src/rom.rs b/src/rom.rs index 78a1381..8d2d811 100644 --- a/src/rom.rs +++ b/src/rom.rs @@ -28,7 +28,7 @@ impl Card for Rom { .get("image") .map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string"))) .transpose()? - .map(|name| name.to_string()); + .map(ToString::to_string); let mut data = Vec::new(); if let Some(file_name) = file_name.as_ref() { File::open(file_name) diff --git a/src/storage.rs b/src/storage.rs index 60ad634..b8a0893 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -27,7 +27,7 @@ impl Card for Storage { .get("image") .map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string"))) .transpose()? - .map(|name| name.to_string()); + .map(ToString::to_string); let mut data = Vec::new(); if let Some(file_name) = file_name.as_ref() { File::open(file_name)