Change cards to use a struct for card config

This commit is contained in:
pjht 2022-11-11 20:37:50 -06:00
parent 9ee6045d6f
commit 7f5602e0eb
3 changed files with 22 additions and 16 deletions

View File

@ -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>,
@ -22,11 +27,7 @@ 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"))?;
let size = serde_yaml::from_value::<Config>(data.into())?.size;
Ok(Self {
data: vec![0; size as usize],
start: 0,

View File

@ -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>,
@ -24,11 +30,7 @@ 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);
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)

View File

@ -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>,
@ -23,11 +29,8 @@ 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);
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)