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 9145c71a34
3 changed files with 21 additions and 16 deletions

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>,
@ -22,11 +27,7 @@ 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>,
@ -24,11 +30,7 @@ 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>,
@ -23,11 +29,7 @@ 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)