From 656f1bc3958c2fa9b434c5563633b85f67993936 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 29 Apr 2021 01:17:23 +0100 Subject: [PATCH] otter: Introduce SpecParse trait for specs we may want to send raw Signed-off-by: Ian Jackson --- src/bin/otter.rs | 33 +++++++++++++++++++++++++-------- 1 file changed, 25 insertions(+), 8 deletions(-) diff --git a/src/bin/otter.rs b/src/bin/otter.rs index 2bfdfc6a..7f7522d2 100644 --- a/src/bin/otter.rs +++ b/src/bin/otter.rs @@ -557,23 +557,40 @@ impl SomeSpec for TableSpec { const FNCOMP : &'static str = "table"; } +trait SpecParse { + type T; + type S: SomeSpec; + fn parse(s: String) -> Result; +} +#[derive(Debug,Copy,Clone)] +struct SpecParseToml(pub PhantomData); +impl SpecParse for SpecParseToml { + type T = T; + type S = T; + #[throws(AE)] + fn parse(buf: String) -> T { + let tv: toml::Value = buf.parse().context("parse TOML")?; + let spec: T = toml_de::from_value(&tv).context("parse value")?; + spec + } +} +impl SpecParseToml { pub fn new() -> Self { Self(default()) } } + #[throws(AE)] -fn read_spec - (ma: &MainOpts, specname: &str) -> T +fn read_spec(ma: &MainOpts, specname: &str, _: P) -> P::T { let filename = if specname.contains('/') { specname.to_string() } else { - format!("{}/{}.{}.toml", &ma.spec_dir, specname, T::FNCOMP) + format!("{}/{}.{}.toml", &ma.spec_dir, specname, P::S::FNCOMP) }; (||{ let mut f = File::open(&filename).context("open")?; let mut buf = String::new(); f.read_to_string(&mut buf).context("read")?; - let tv: toml::Value = buf.parse().context("parse TOML")?; - let spec: T = toml_de::from_value(&tv).context("parse value")?; + let spec = P::parse(buf)?; Ok::<_,AE>(spec) - })().with_context(|| format!("read {} {:?}", T::WHAT, &filename))? + })().with_context(|| format!("read {} {:?}", P::S::WHAT, &filename))? } #[throws(AE)] @@ -674,12 +691,12 @@ mod reset_game { pieces, table_colour, pcaliases, - } = read_spec(&ma, &args.game_file)?; + } = read_spec(&ma, &args.game_file, SpecParseToml::new())?; let mut insns = vec![]; if let Some(table_file) = args.table_file { - let table_spec = read_spec(&ma, &table_file)?; + let table_spec = read_spec(&ma, &table_file, SpecParseToml::new())?; let game = chan.game.clone(); chan.cmd(&MgmtCommand::CreateGame { game, -- 2.30.2