From 7f5602e0eb033e5b0347d804b14de38756f6b9e7 Mon Sep 17 00:00:00 2001 From: pjht Date: Fri, 11 Nov 2022 20:37:50 -0600 Subject: [PATCH] Change cards to use a struct for card config --- src/ram.rs | 13 +++++++------ src/rom.rs | 12 +++++++----- src/storage.rs | 13 ++++++++----- 3 files changed, 22 insertions(+), 16 deletions(-) diff --git a/src/ram.rs b/src/ram.rs index aa49484..e739b70 100644 --- a/src/ram.rs +++ b/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, @@ -22,11 +27,7 @@ impl Ram {} impl Card for Ram { fn new(data: Mapping) -> anyhow::Result { - 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::(data.into())?.size; Ok(Self { data: vec![0; size as usize], start: 0, diff --git a/src/rom.rs b/src/rom.rs index 0833312..08a621d 100644 --- a/src/rom.rs +++ b/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, +} + #[derive(Debug)] pub struct Rom { data: Vec, @@ -24,11 +30,7 @@ impl Rom {} impl Card for Rom { fn new(data: Mapping) -> anyhow::Result { - 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::(data.into())?.file_name; let mut data = Vec::new(); if let Some(file_name) = file_name.as_ref() { File::open(file_name) diff --git a/src/storage.rs b/src/storage.rs index 44d1583..afa0f0d 100644 --- a/src/storage.rs +++ b/src/storage.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::{ @@ -13,6 +14,11 @@ use crate::{ const SECTOR_SIZE: usize = 256; +#[derive(Deserialize)] +struct Config { + file_name: Option, +} + #[derive(Debug)] pub struct Storage { data: Vec, @@ -23,11 +29,8 @@ pub struct Storage { impl Card for Storage { fn new(data: Mapping) -> anyhow::Result { - 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::(data.into())?.file_name; + let mut data = Vec::new(); if let Some(file_name) = file_name.as_ref() { File::open(file_name)