From b76c4c7bf5df0383417ce58753fb8aa6ffe3275e Mon Sep 17 00:00:00 2001 From: pjht Date: Sat, 12 Nov 2022 08:59:46 -0600 Subject: [PATCH] Refactor card config parsing out of main.rs --- src/backplane.rs | 36 +++++++++++------------------------- src/card.rs | 21 ++++++++++++++++++++- src/main.rs | 17 +++-------------- 3 files changed, 34 insertions(+), 40 deletions(-) diff --git a/src/backplane.rs b/src/backplane.rs index 1090f4b..c4a4b90 100644 --- a/src/backplane.rs +++ b/src/backplane.rs @@ -1,6 +1,5 @@ +use anyhow::anyhow; use nullable_result::{GeneralIterExt, NullableResult}; -use thiserror::Error; -use toml::Value; use crate::{ card::{self, Card}, @@ -12,17 +11,17 @@ pub struct Backplane { cards: Vec>, } -#[derive(Debug, Copy, Clone, Error)] -pub enum CardAddError { - #[error["Backplane full, could not add card"]] - BackplaneFull, - #[error("Invalid card type")] - InvalidType, -} - impl Backplane { - pub fn new() -> Self { - Self { cards: Vec::new() } + pub fn new(cards: Vec>) -> anyhow::Result { + if cards.len() > 255 { + return Err(anyhow!("A maximum of 255 cards are allowed")); + } + Ok(Self { + cards: cards + .into_iter() + .map(|cfg| cfg.into_card()) + .collect::, _>>()?, + }) } #[allow(dead_code)] @@ -34,19 +33,6 @@ impl Backplane { &mut self.cards } - pub fn add_card(&mut self, type_name: &str, config: Value) -> anyhow::Result { - if self.cards.len() >= 255 { - return Err(CardAddError::BackplaneFull.into()); - } - self.cards.push( - inventory::iter::() - .find(|card_type| card_type.name == type_name) - .ok_or(CardAddError::InvalidType)? - .new_card(config)?, - ); - Ok(self.cards.len() - 1) - } - pub fn read_word(&mut self, address: u32) -> Result { self.mem_helper( address, diff --git a/src/card.rs b/src/card.rs index 911fa13..3a05680 100644 --- a/src/card.rs +++ b/src/card.rs @@ -1,8 +1,27 @@ use crate::m68k::BusError; +use anyhow::anyhow; use nullable_result::NullableResult; +use serde::Deserialize; use std::fmt::{Debug, Display}; use toml::Value; +#[derive(Deserialize, Debug)] +pub struct Config<'a> { + #[serde(rename = "type")] + typ: &'a str, + #[serde(flatten)] + config: Value, +} + +impl Config<'_> { + pub fn into_card(self) -> anyhow::Result> { + inventory::iter::() + .find(|card_type| card_type.name == self.typ) + .ok_or_else(|| anyhow!("Invalid card type {}", self.typ))? + .new_card(self.config) + } +} + pub struct Type { pub name: &'static str, new: fn(data: Value) -> anyhow::Result>, @@ -16,7 +35,7 @@ impl Type { } } - pub fn new_card(&self, data: Value) -> anyhow::Result> { + fn new_card(&self, data: Value) -> anyhow::Result> { (self.new)(data) } } diff --git a/src/main.rs b/src/main.rs index f024cd2..c6ce220 100644 --- a/src/main.rs +++ b/src/main.rs @@ -32,20 +32,11 @@ use reedline_repl_rs::{ use serde::Deserialize; use std::{fs, path::Path, process}; use symbol_tables::SymbolTables; -use toml::Value; - -#[derive(Deserialize, Debug)] -struct CardConfig<'a> { - #[serde(rename = "type")] - typ: &'a str, - #[serde(flatten)] - config: Value, -} #[derive(Deserialize, Debug)] struct EmuConfig<'a> { #[serde(borrow)] - cards: Vec>, + cards: Vec>, #[serde(borrow)] symbol_tables: Option>, } @@ -61,10 +52,8 @@ fn main() -> Result<(), anyhow::Error> { .map_err(|e| anyhow!("Could not read config file ({})", e))?; let config: EmuConfig = toml::from_str(&config_str).map_err(|e| anyhow!("Could not parse config file ({})", e))?; - let mut backplane = Backplane::new(); - for card in config.cards { - backplane.add_card(card.typ, card.config)?; - } + dbg!(&config); + let backplane = Backplane::new(config.cards)?; let mut symbol_tables = SymbolTables::new(); if let Some(initial_tables) = config.symbol_tables { for path in initial_tables {