Compare commits
2 Commits
ec7db304b8
...
7f5602e0eb
Author | SHA1 | Date | |
---|---|---|---|
7f5602e0eb | |||
9ee6045d6f |
@ -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,6 +11,11 @@ use crate::{
|
||||
register,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
size: u32,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Ram {
|
||||
data: Vec<u8>,
|
||||
@ -21,12 +26,8 @@ pub struct Ram {
|
||||
impl Ram {}
|
||||
|
||||
impl Card for Ram {
|
||||
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"))?;
|
||||
fn new(data: Mapping) -> anyhow::Result<Self> {
|
||||
let size = serde_yaml::from_value::<Config>(data.into())?.size;
|
||||
Ok(Self {
|
||||
data: vec![0; size as usize],
|
||||
start: 0,
|
||||
|
14
src/rom.rs
14
src/rom.rs
@ -3,6 +3,7 @@ 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::{
|
||||
@ -11,6 +12,11 @@ use crate::{
|
||||
register,
|
||||
};
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
file_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Rom {
|
||||
data: Vec<u8>,
|
||||
@ -23,12 +29,8 @@ pub struct Rom {
|
||||
impl Rom {}
|
||||
|
||||
impl Card for Rom {
|
||||
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);
|
||||
fn new(data: Mapping) -> anyhow::Result<Self> {
|
||||
let file_name = serde_yaml::from_value::<Config>(data.into())?.file_name;
|
||||
let mut data = Vec::new();
|
||||
if let Some(file_name) = file_name.as_ref() {
|
||||
File::open(file_name)
|
||||
|
@ -3,6 +3,7 @@ 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::{
|
||||
@ -13,6 +14,11 @@ use crate::{
|
||||
|
||||
const SECTOR_SIZE: usize = 256;
|
||||
|
||||
#[derive(Deserialize)]
|
||||
struct Config {
|
||||
file_name: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct Storage {
|
||||
data: Vec<u8>,
|
||||
@ -22,12 +28,9 @@ pub struct Storage {
|
||||
}
|
||||
|
||||
impl Card for Storage {
|
||||
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);
|
||||
fn new(data: Mapping) -> anyhow::Result<Self> {
|
||||
let file_name = serde_yaml::from_value::<Config>(data.into())?.file_name;
|
||||
|
||||
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