Switch config format to TOML

This commit is contained in:
pjht 2022-11-11 22:11:06 -06:00
parent daf6bdc477
commit cb96ee0dd7
11 changed files with 48 additions and 65 deletions

42
Cargo.lock generated
View File

@ -378,12 +378,6 @@ dependencies = [
"either",
]
[[package]]
name = "itoa"
version = "1.0.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4217ad341ebadf8d8e724e264f13e593e0648f5b3e94b3896a5df283be015ecc"
[[package]]
name = "js-sys"
version = "0.3.60"
@ -450,8 +444,8 @@ dependencies = [
"paste",
"reedline-repl-rs",
"serde",
"serde_yaml",
"thiserror",
"toml",
]
[[package]]
@ -664,12 +658,6 @@ version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "97477e48b4cf8603ad5f7aaf897467cf42ab4218a38ef76fb14c2d6773a6d6a8"
[[package]]
name = "ryu"
version = "1.0.11"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "4501abdff3ae82a1c1b477a17252eb69cee9e66eb915c1abaa4f44d873df9f09"
[[package]]
name = "scopeguard"
version = "1.1.0"
@ -702,19 +690,6 @@ dependencies = [
"syn",
]
[[package]]
name = "serde_yaml"
version = "0.9.14"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6d232d893b10de3eb7258ff01974d6ee20663d8e833263c99409d4b13a0209da"
dependencies = [
"indexmap",
"itoa",
"ryu",
"serde",
"unsafe-libyaml",
]
[[package]]
name = "signal-hook"
version = "0.3.14"
@ -848,6 +823,15 @@ dependencies = [
"winapi",
]
[[package]]
name = "toml"
version = "0.5.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8d82e1a7758622a465f8cee077614c73484dac5b836c02ff6a40d5d1010324d7"
dependencies = [
"serde",
]
[[package]]
name = "unicode-ident"
version = "1.0.5"
@ -866,12 +850,6 @@ version = "0.1.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c0edd1e5b14653f783770bce4a4dabb4a5108a5370a5f5d8cfe8710c361f6c8b"
[[package]]
name = "unsafe-libyaml"
version = "0.2.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c1e5fa573d8ac5f1a856f8d7be41d390ee973daf97c806b2c1a465e4e1406e68"
[[package]]
name = "utf8parse"
version = "0.2.0"

View File

@ -19,5 +19,5 @@ parse_int = "0.6.0"
paste = "1.0.9"
reedline-repl-rs = { path = "reedline-repl-rs" }
serde = { version = "1.0.144", features = ["derive"] }
serde_yaml = "0.9.13"
thiserror = "1.0.37"
toml = "0.5.9"

13
config.toml Normal file
View File

@ -0,0 +1,13 @@
symbol_tables = ["rom/rom.elf"]
[[cards]]
type = "rom"
image = "rom/rom.bin"
[[cards]]
type = "ram"
size = 4096
[[cards]]
type = "storage"

View File

@ -1,9 +0,0 @@
---
cards:
- type: rom
image: rom/rom.bin
- type: ram
size: 4096
- type: storage
symbol_tables:
- rom/rom.elf

View File

@ -1,6 +1,6 @@
use nullable_result::{GeneralIterExt, NullableResult};
use serde_yaml::Mapping;
use thiserror::Error;
use toml::Value;
use crate::{
card::{self, Card},
@ -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: Value) -> anyhow::Result<usize> {
if self.cards.len() >= 255 {
return Err(CardAddError::BackplaneFull.into());
}

View File

@ -1,11 +1,11 @@
use crate::m68k::BusError;
use nullable_result::NullableResult;
use serde_yaml::Mapping;
use std::fmt::{Debug, Display};
use toml::Value;
pub struct Type {
pub name: &'static str,
new: fn(data: Mapping) -> anyhow::Result<Box<dyn Card>>,
new: fn(data: Value) -> 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: Value) -> 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: Value) -> anyhow::Result<Self>
where
Self: Sized;
fn new_dyn(data: Mapping) -> anyhow::Result<Box<dyn Card>>
fn new_dyn(data: Value) -> anyhow::Result<Box<dyn Card>>
where
Self: Sized + 'static,
{

View File

@ -30,16 +30,16 @@ use reedline_repl_rs::{
Repl,
};
use serde::Deserialize;
use serde_yaml::Mapping;
use std::{fs, path::Path, process};
use symbol_tables::SymbolTables;
use toml::Value;
#[derive(Deserialize, Debug)]
struct CardConfig<'a> {
#[serde(rename = "type")]
typ: &'a str,
#[serde(flatten)]
config: Mapping,
config: Value,
}
#[derive(Deserialize, Debug)]
@ -57,10 +57,11 @@ struct EmuState {
}
fn main() -> Result<(), anyhow::Error> {
let config_str = fs::read_to_string("config.yaml")
let config_str = fs::read_to_string("config.toml")
.map_err(|e| anyhow!("Could not read config file ({})", e))?;
let config: EmuConfig = serde_yaml::from_str(&config_str)
.map_err(|e| anyhow!("Could not parse config file ({})", e))?;
let config: EmuConfig =
toml::from_str(&config_str).map_err(|e| anyhow!("Could not parse config file ({})", e))?;
dbg!(&config);
let mut backplane = Backplane::new();
for card in config.cards {
backplane.add_card(card.typ, card.config)?;

View File

@ -3,7 +3,7 @@ use std::fmt::Display;
use human_repr::HumanCount;
use nullable_result::NullableResult;
use serde::Deserialize;
use serde_yaml::Mapping;
use toml::Value;
use crate::{
card::{u32_get_be_byte, Card},
@ -26,8 +26,8 @@ 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: Value) -> anyhow::Result<Self> {
let size = data.try_into::<Config>()?.size;
Ok(Self {
data: vec![0; size as usize],
start: 0,

View File

@ -4,7 +4,7 @@ use anyhow::anyhow;
use human_repr::HumanCount;
use nullable_result::NullableResult;
use serde::Deserialize;
use serde_yaml::Mapping;
use toml::Value;
use crate::{
card::{u16_get_be_byte, u16_set_be_byte, Card},
@ -29,8 +29,8 @@ 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: Value) -> anyhow::Result<Self> {
let file_name = data.try_into::<Config>()?.image;
let mut data = Vec::new();
if let Some(file_name) = file_name.as_ref() {
File::open(file_name)

View File

@ -4,7 +4,7 @@ use anyhow::anyhow;
use human_repr::HumanCount;
use nullable_result::NullableResult;
use serde::Deserialize;
use serde_yaml::Mapping;
use toml::Value;
use crate::{
card::{u32_get_be_byte, u32_set_be_byte, Card},
@ -28,8 +28,8 @@ 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: Value) -> anyhow::Result<Self> {
let file_name = data.try_into::<Config>()?.image;
let mut data = Vec::new();
if let Some(file_name) = file_name.as_ref() {
File::open(file_name)

View File

@ -1,7 +1,7 @@
use std::fmt::Display;
use nullable_result::NullableResult;
use serde_yaml::Mapping;
use toml::Value;
use crate::{card::Card, m68k::BusError, register};
@ -15,7 +15,7 @@ impl Display for Term {
}
impl Card for Term {
fn new(_data: Mapping) -> anyhow::Result<Self>
fn new(_data: Value) -> anyhow::Result<Self>
where
Self: Sized,
{