Compare commits

...

2 Commits

7 changed files with 32 additions and 26 deletions

View File

@ -34,7 +34,7 @@ impl Backplane {
&mut self.cards &mut self.cards
} }
pub fn add_card(&mut self, type_name: &str, config: &Mapping) -> anyhow::Result<usize> { pub fn add_card(&mut self, type_name: &str, config: Mapping) -> anyhow::Result<usize> {
if self.cards.len() >= 255 { if self.cards.len() >= 255 {
return Err(CardAddError::BackplaneFull.into()); return Err(CardAddError::BackplaneFull.into());
} }

View File

@ -5,7 +5,7 @@ use std::fmt::{Debug, Display};
pub struct Type { 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 Type { impl Type {
@ -16,7 +16,7 @@ impl Type {
} }
} }
pub fn new_card(&self, data: &Mapping) -> anyhow::Result<Box<dyn Card>> { pub fn new_card(&self, data: Mapping) -> anyhow::Result<Box<dyn Card>> {
(self.new)(data) (self.new)(data)
} }
} }
@ -24,10 +24,10 @@ impl Type {
inventory::collect!(Type); 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>
where where
Self: Sized; Self: Sized;
fn new_dyn(data: &Mapping) -> anyhow::Result<Box<dyn Card>> fn new_dyn(data: Mapping) -> anyhow::Result<Box<dyn Card>>
where where
Self: Sized + 'static, Self: Sized + 'static,
{ {

View File

@ -63,7 +63,7 @@ fn main() -> Result<(), anyhow::Error> {
.map_err(|e| anyhow!("Could not parse config file ({})", e))?; .map_err(|e| anyhow!("Could not parse config file ({})", e))?;
let mut backplane = Backplane::new(); let mut backplane = Backplane::new();
for card in config.cards { for card in config.cards {
backplane.add_card(card.typ, &card.config)?; backplane.add_card(card.typ, card.config)?;
} }
let mut symbol_tables = SymbolTables::new(); let mut symbol_tables = SymbolTables::new();
if let Some(initial_tables) = config.symbol_tables { if let Some(initial_tables) = config.symbol_tables {

View File

@ -1,8 +1,8 @@
use std::fmt::Display; use std::fmt::Display;
use anyhow::anyhow;
use human_repr::HumanCount; use human_repr::HumanCount;
use nullable_result::NullableResult; use nullable_result::NullableResult;
use serde::Deserialize;
use serde_yaml::Mapping; use serde_yaml::Mapping;
use crate::{ use crate::{
@ -11,6 +11,11 @@ use crate::{
register, register,
}; };
#[derive(Deserialize)]
struct Config {
size: u32,
}
#[derive(Debug)] #[derive(Debug)]
pub struct Ram { pub struct Ram {
data: Vec<u8>, data: Vec<u8>,
@ -21,12 +26,8 @@ pub struct Ram {
impl Ram {} impl Ram {}
impl Card for Ram { impl Card for Ram {
fn new(data: &Mapping) -> anyhow::Result<Self> { fn new(data: Mapping) -> anyhow::Result<Self> {
let size = data let size = serde_yaml::from_value::<Config>(data.into())?.size;
.get("size")
.ok_or_else(|| anyhow!("No size value for RAM"))?
.as_u64()
.ok_or_else(|| anyhow!("Size value not a positive integer"))?;
Ok(Self { Ok(Self {
data: vec![0; size as usize], data: vec![0; size as usize],
start: 0, start: 0,

View File

@ -3,6 +3,7 @@ use std::{fmt::Display, fs::File, io::Read};
use anyhow::anyhow; use anyhow::anyhow;
use human_repr::HumanCount; use human_repr::HumanCount;
use nullable_result::NullableResult; use nullable_result::NullableResult;
use serde::Deserialize;
use serde_yaml::Mapping; use serde_yaml::Mapping;
use crate::{ use crate::{
@ -11,6 +12,11 @@ use crate::{
register, register,
}; };
#[derive(Deserialize)]
struct Config {
file_name: Option<String>,
}
#[derive(Debug)] #[derive(Debug)]
pub struct Rom { pub struct Rom {
data: Vec<u8>, data: Vec<u8>,
@ -23,12 +29,8 @@ pub struct Rom {
impl Rom {} impl Rom {}
impl Card for Rom { impl Card for Rom {
fn new(data: &Mapping) -> anyhow::Result<Self> { fn new(data: Mapping) -> anyhow::Result<Self> {
let file_name = data let file_name = serde_yaml::from_value::<Config>(data.into())?.file_name;
.get("image")
.map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string")))
.transpose()?
.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

@ -3,6 +3,7 @@ use std::{fmt::Display, fs::File, io::Read};
use anyhow::anyhow; use anyhow::anyhow;
use human_repr::HumanCount; use human_repr::HumanCount;
use nullable_result::NullableResult; use nullable_result::NullableResult;
use serde::Deserialize;
use serde_yaml::Mapping; use serde_yaml::Mapping;
use crate::{ use crate::{
@ -13,6 +14,11 @@ use crate::{
const SECTOR_SIZE: usize = 256; const SECTOR_SIZE: usize = 256;
#[derive(Deserialize)]
struct Config {
file_name: Option<String>,
}
#[derive(Debug)] #[derive(Debug)]
pub struct Storage { pub struct Storage {
data: Vec<u8>, data: Vec<u8>,
@ -22,12 +28,9 @@ pub struct Storage {
} }
impl Card for Storage { impl Card for Storage {
fn new(data: &Mapping) -> anyhow::Result<Self> { fn new(data: Mapping) -> anyhow::Result<Self> {
let file_name = data let file_name = serde_yaml::from_value::<Config>(data.into())?.file_name;
.get("image")
.map(|name| name.as_str().ok_or_else(|| anyhow!("File name not string")))
.transpose()?
.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

@ -15,7 +15,7 @@ impl Display for Term {
} }
impl Card for Term { impl Card for Term {
fn new(_data: &Mapping) -> anyhow::Result<Self> fn new(_data: Mapping) -> anyhow::Result<Self>
where where
Self: Sized, Self: Sized,
{ {