Compare commits
No commits in common. "7f5602e0eb033e5b0347d804b14de38756f6b9e7" and "ec7db304b83ff6a0db69dbded847aadd7ec73880" have entirely different histories.
7f5602e0eb
...
ec7db304b8
@ -34,7 +34,7 @@ impl Backplane {
|
||||
&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 {
|
||||
return Err(CardAddError::BackplaneFull.into());
|
||||
}
|
||||
|
@ -5,7 +5,7 @@ use std::fmt::{Debug, Display};
|
||||
|
||||
pub struct Type {
|
||||
pub name: &'static str,
|
||||
new: fn(data: Mapping) -> anyhow::Result<Box<dyn Card>>,
|
||||
new: fn(data: &Mapping) -> anyhow::Result<Box<dyn Card>>,
|
||||
}
|
||||
|
||||
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)
|
||||
}
|
||||
}
|
||||
@ -24,10 +24,10 @@ impl Type {
|
||||
inventory::collect!(Type);
|
||||
|
||||
pub trait Card: Debug + Display {
|
||||
fn new(data: Mapping) -> anyhow::Result<Self>
|
||||
fn new(data: &Mapping) -> anyhow::Result<Self>
|
||||
where
|
||||
Self: Sized;
|
||||
fn new_dyn(data: Mapping) -> anyhow::Result<Box<dyn Card>>
|
||||
fn new_dyn(data: &Mapping) -> anyhow::Result<Box<dyn Card>>
|
||||
where
|
||||
Self: Sized + 'static,
|
||||
{
|
||||
|
@ -63,7 +63,7 @@ fn main() -> Result<(), anyhow::Error> {
|
||||
.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)?;
|
||||
backplane.add_card(card.typ, &card.config)?;
|
||||
}
|
||||
let mut symbol_tables = SymbolTables::new();
|
||||
if let Some(initial_tables) = config.symbol_tables {
|
||||
|
15
src/ram.rs
15
src/ram.rs
@ -1,8 +1,8 @@
|
||||
use std::fmt::Display;
|
||||
|
||||
use anyhow::anyhow;
|
||||
use human_repr::HumanCount;
|
||||
use nullable_result::NullableResult;
|
||||
use serde::Deserialize;
|
||||
use serde_yaml::Mapping;
|
||||
|
||||
use crate::{
|
||||
@ -11,11 +11,6 @@ use crate::{
|
||||
register,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
size: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Ram {
|
||||
data: Vec<u8>,
|
||||
@ -26,8 +21,12 @@ pub struct Ram {
|
||||
impl Ram {}
|
||||
|
||||
impl Card for Ram {
|
||||
fn new(data: Mapping) -> anyhow::Result<Self> {
|
||||
let size = serde_yaml::from_value::<Config>(data.into())?.size;
|
||||
fn new(data: &Mapping) -> anyhow::Result<Self> {
|
||||
let size = data
|
||||
.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 {
|
||||
data: vec![0; size as usize],
|
||||
start: 0,
|
||||
|
14
src/rom.rs
14
src/rom.rs
@ -3,7 +3,6 @@ use std::{fmt::Display, fs::File, io::Read};
|
||||
use anyhow::anyhow;
|
||||
use human_repr::HumanCount;
|
||||
use nullable_result::NullableResult;
|
||||
use serde::Deserialize;
|
||||
use serde_yaml::Mapping;
|
||||
|
||||
use crate::{
|
||||
@ -12,11 +11,6 @@ use crate::{
|
||||
register,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
file_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Rom {
|
||||
data: Vec<u8>,
|
||||
@ -29,8 +23,12 @@ pub struct Rom {
|
||||
impl Rom {}
|
||||
|
||||
impl Card for Rom {
|
||||
fn new(data: Mapping) -> anyhow::Result<Self> {
|
||||
let file_name = serde_yaml::from_value::<Config>(data.into())?.file_name;
|
||||
fn new(data: &Mapping) -> anyhow::Result<Self> {
|
||||
let file_name = data
|
||||
.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();
|
||||
if let Some(file_name) = file_name.as_ref() {
|
||||
File::open(file_name)
|
||||
|
@ -3,7 +3,6 @@ use std::{fmt::Display, fs::File, io::Read};
|
||||
use anyhow::anyhow;
|
||||
use human_repr::HumanCount;
|
||||
use nullable_result::NullableResult;
|
||||
use serde::Deserialize;
|
||||
use serde_yaml::Mapping;
|
||||
|
||||
use crate::{
|
||||
@ -14,11 +13,6 @@ use crate::{
|
||||
|
||||
const SECTOR_SIZE: usize = 256;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
file_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Storage {
|
||||
data: Vec<u8>,
|
||||
@ -28,9 +22,12 @@ pub struct Storage {
|
||||
}
|
||||
|
||||
impl Card for Storage {
|
||||
fn new(data: Mapping) -> anyhow::Result<Self> {
|
||||
let file_name = serde_yaml::from_value::<Config>(data.into())?.file_name;
|
||||
|
||||
fn new(data: &Mapping) -> anyhow::Result<Self> {
|
||||
let file_name = data
|
||||
.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();
|
||||
if let Some(file_name) = file_name.as_ref() {
|
||||
File::open(file_name)
|
||||
|
@ -15,7 +15,7 @@ impl Display for Term {
|
||||
}
|
||||
|
||||
impl Card for Term {
|
||||
fn new(_data: Mapping) -> anyhow::Result<Self>
|
||||
fn new(_data: &Mapping) -> anyhow::Result<Self>
|
||||
where
|
||||
Self: Sized,
|
||||
{
|
||||
|
Loading…
Reference in New Issue
Block a user