Fix some pedantic and nursery lints

This commit is contained in:
pjht 2022-10-24 08:50:23 -05:00
parent a3b1178fd3
commit c738eb0518
7 changed files with 39 additions and 41 deletions

View File

@ -3,7 +3,7 @@ use serde_yaml::Mapping;
use thiserror::Error; use thiserror::Error;
use crate::{ use crate::{
card::{Card, CardType}, card::{self, Card},
m68k::BusError, m68k::BusError,
}; };
@ -39,7 +39,7 @@ impl Backplane {
return Err(CardAddError::BackplaneFull.into()); return Err(CardAddError::BackplaneFull.into());
} }
self.cards.push( self.cards.push(
inventory::iter::<CardType>() inventory::iter::<card::Type>()
.find(|card_type| card_type.name == type_name) .find(|card_type| card_type.name == type_name)
.ok_or(CardAddError::InvalidType)? .ok_or(CardAddError::InvalidType)?
.new_card(config)?, .new_card(config)?,

View File

@ -3,12 +3,12 @@ use nullable_result::NullableResult;
use serde_yaml::Mapping; use serde_yaml::Mapping;
use std::fmt::{Debug, Display}; use std::fmt::{Debug, Display};
pub struct CardType { pub struct Type {
pub name: &'static str, pub name: &'static str,
new: fn(data: &Mapping) -> anyhow::Result<Box<dyn Card>>, new: fn(data: &Mapping) -> anyhow::Result<Box<dyn Card>>,
} }
impl CardType { impl Type {
pub const fn new<T: Card + 'static>(name: &'static str) -> Self { pub const fn new<T: Card + 'static>(name: &'static str) -> Self {
Self { Self {
name, name,
@ -21,7 +21,7 @@ impl CardType {
} }
} }
inventory::collect!(CardType); inventory::collect!(Type);
pub trait Card: Debug + Display { pub trait Card: Debug + Display {
fn new(data: &Mapping) -> anyhow::Result<Self> fn new(data: &Mapping) -> anyhow::Result<Self>
@ -126,7 +126,7 @@ pub const fn u16_get_be_byte(val: u16, idx: u8) -> u8 {
macro_rules! register { macro_rules! register {
($typ: ty, $name: literal) => { ($typ: ty, $name: literal) => {
paste::paste! { paste::paste! {
inventory::submit!($crate::card::CardType::new::<$typ>($name)); inventory::submit!($crate::card::Type::new::<$typ>($name));
} }
}; };
} }

View File

@ -16,20 +16,20 @@ impl Location {
} }
} }
pub fn displayer<'a>(&'a self, symbol_tables: &'a SymbolTables) -> LocationDisplayer<'a> { pub fn displayer<'a>(&'a self, symbol_tables: &'a SymbolTables) -> Displayer<'a> {
LocationDisplayer { Displayer {
location: self, location: self,
symbol_tables, symbol_tables,
} }
} }
} }
pub struct LocationDisplayer<'a> { pub struct Displayer<'a> {
location: &'a Location, location: &'a Location,
symbol_tables: &'a SymbolTables, 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 { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.location { match self.location {
Location::Symbol((table, sym)) => f.write_fmt(format_args!( Location::Symbol((table, sym)) => f.write_fmt(format_args!(

View File

@ -25,7 +25,6 @@ use disas::DisassemblyError;
use indexmap::IndexSet; use indexmap::IndexSet;
use itertools::Itertools; use itertools::Itertools;
use parse_int::parse; use parse_int::parse;
use peek::{PeekFormat, PeekSize};
use reedline_repl_rs::{ use reedline_repl_rs::{
clap::{builder::BoolishValueParser, Arg, ArgAction, Command}, clap::{builder::BoolishValueParser, Arg, ArgAction, Command},
Repl, Repl,
@ -201,7 +200,7 @@ fn main() -> Result<(), anyhow::Error> {
.get_one::<String>("stop_addr") .get_one::<String>("stop_addr")
.map(|s| state.symbol_tables.parse_location_address(s)) .map(|s| state.symbol_tables.parse_location_address(s))
.transpose()?; .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.symbol_tables.breakpoint_set_at(state.cpu.pc())
|| state.address_breakpoints.contains(&state.cpu.pc()) || state.address_breakpoints.contains(&state.cpu.pc())
{ {
@ -252,8 +251,8 @@ fn main() -> Result<(), anyhow::Error> {
if fmt_str.len() != 2 { if fmt_str.len() != 2 {
return Err(anyhow!("Peek format length must be 2")); return Err(anyhow!("Peek format length must be 2"));
} }
let fmt = PeekFormat::try_from(fmt_str.chars().next().unwrap())?; let fmt = peek::Format::try_from(fmt_str.chars().next().unwrap())?;
let size = PeekSize::try_from(fmt_str.chars().nth(1).unwrap())?; let size = peek::Size::try_from(fmt_str.chars().nth(1).unwrap())?;
let count = parse::<u32>(args.get_one::<String>("count").map_or("1", String::as_str))?; let count = parse::<u32>(args.get_one::<String>("count").map_or("1", String::as_str))?;
let addr = state let addr = state
.symbol_tables .symbol_tables
@ -262,9 +261,9 @@ fn main() -> Result<(), anyhow::Error> {
let bus = state.cpu.bus_mut(); let bus = state.cpu.bus_mut();
for i in 0..count { for i in 0..count {
match size { match size {
PeekSize::Byte => data.push(bus.read_byte(addr + i)? as u32), peek::Size::Byte => data.push(bus.read_byte(addr + i)? as u32),
PeekSize::Word => data.push(bus.read_word(addr + (i * 2))? as u32), peek::Size::Word => data.push(bus.read_word(addr + (i * 2))? as u32),
PeekSize::LongWord => data.push( peek::Size::LongWord => data.push(
(bus.read_word(addr + (i * 4))? as u32) << 16 (bus.read_word(addr + (i * 4))? as u32) << 16
| (bus.read_word(addr + (i * 4) + 2)? as u32), | (bus.read_word(addr + (i * 4) + 2)? as u32),
), ),

View File

@ -1,7 +1,7 @@
use thiserror::Error; use thiserror::Error;
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum PeekFormat { pub enum Format {
Octal, Octal,
Hex, Hex,
Decimal, Decimal,
@ -9,16 +9,16 @@ pub enum PeekFormat {
Binary, Binary,
} }
impl PeekFormat { impl Format {
pub fn format(self, num: u32, size: PeekSize) -> String { pub fn format(self, num: u32, size: Size) -> String {
match self { match self {
Self::Octal => format!("Oo{:0>width$o}", num, width = size.byte_count()), 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::Hex => format!("0x{:0>width$x}", num, width = size.byte_count() * 2),
Self::Decimal => { Self::Decimal => {
let num = match size { let num = match size {
PeekSize::Byte => num as u8 as i8 as i32, Size::Byte => num as u8 as i8 as i32,
PeekSize::Word => num as u16 as i16 as i32, Size::Word => num as u16 as i16 as i32,
PeekSize::LongWord => num as i32, Size::LongWord => num as i32,
}; };
format!("{}", num) format!("{}", num)
} }
@ -30,10 +30,10 @@ impl PeekFormat {
#[derive(Debug, Copy, Clone, Error)] #[derive(Debug, Copy, Clone, Error)]
#[error("Invalid peek format")] #[error("Invalid peek format")]
pub struct InvalidPeekFormat; pub struct InvalidFormat;
impl TryFrom<char> for PeekFormat { impl TryFrom<char> for Format {
type Error = InvalidPeekFormat; type Error = InvalidFormat;
fn try_from(value: char) -> Result<Self, Self::Error> { fn try_from(value: char) -> Result<Self, Self::Error> {
match value { match value {
@ -42,49 +42,48 @@ impl TryFrom<char> for PeekFormat {
'd' => Ok(Self::Decimal), 'd' => Ok(Self::Decimal),
'u' => Ok(Self::UnsignedDecimal), 'u' => Ok(Self::UnsignedDecimal),
'b' => Ok(Self::Binary), 'b' => Ok(Self::Binary),
_ => Err(InvalidPeekFormat), _ => Err(InvalidFormat),
} }
} }
} }
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum PeekSize { pub enum Size {
Byte, Byte,
Word, Word,
LongWord, LongWord,
} }
impl PeekSize { impl Size {
pub fn byte_count(self) -> usize { pub fn byte_count(self) -> usize {
match self { match self {
PeekSize::Byte => 1, Self::Byte => 1,
PeekSize::Word => 2, Self::Word => 2,
PeekSize::LongWord => 4, Self::LongWord => 4,
} }
} }
pub fn chunk_size(self) -> usize { pub fn chunk_size(self) -> usize {
match self { match self {
PeekSize::Byte => 8, Self::Byte | Self::Word => 8,
PeekSize::Word => 8, Self::LongWord => 4,
PeekSize::LongWord => 4,
} }
} }
} }
#[derive(Debug, Copy, Clone, Error)] #[derive(Debug, Copy, Clone, Error)]
#[error("Invalid peek size")] #[error("Invalid peek size")]
pub struct InvalidPeekSize; pub struct InvalidSize;
impl TryFrom<char> for PeekSize { impl TryFrom<char> for Size {
type Error = InvalidPeekSize; type Error = InvalidSize;
fn try_from(value: char) -> Result<Self, Self::Error> { fn try_from(value: char) -> Result<Self, Self::Error> {
match value { match value {
'b' => Ok(Self::Byte), 'b' => Ok(Self::Byte),
'w' => Ok(Self::Word), 'w' => Ok(Self::Word),
'l' => Ok(Self::LongWord), 'l' => Ok(Self::LongWord),
_ => Err(InvalidPeekSize), _ => Err(InvalidSize),
} }
} }
} }

View File

@ -28,7 +28,7 @@ impl Card for Rom {
.get("image") .get("image")
.map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string"))) .map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string")))
.transpose()? .transpose()?
.map(|name| name.to_string()); .map(ToString::to_string);
let mut data = Vec::new(); let mut data = Vec::new();
if let Some(file_name) = file_name.as_ref() { if let Some(file_name) = file_name.as_ref() {
File::open(file_name) File::open(file_name)

View File

@ -27,7 +27,7 @@ impl Card for Storage {
.get("image") .get("image")
.map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string"))) .map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string")))
.transpose()? .transpose()?
.map(|name| name.to_string()); .map(ToString::to_string);
let mut data = Vec::new(); let mut data = Vec::new();
if let Some(file_name) = file_name.as_ref() { if let Some(file_name) = file_name.as_ref() {
File::open(file_name) File::open(file_name)