From 7dbb80ac815229ffc00cc10c07b3123361624c8c Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Thu, 20 May 2021 01:00:37 +0100 Subject: [PATCH] specs: Change error handling for load_spec_to_read (again) Signed-off-by: Ian Jackson --- daemon/cmdlistener.rs | 10 ++-------- src/bundles.rs | 30 ++++++++++++++++++++---------- 2 files changed, 22 insertions(+), 18 deletions(-) diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 02d76f4a..363ab057 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -693,14 +693,8 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>( MGI::ResetFromNamedSpec { spec } => { reset_game_from_spec(cs,ag,ig,who, Box::new(move |ig| { - let (mut spec_f, e_f) = bundles::load_spec_to_read(ig,&spec)?; - let mut buf = String::new(); - spec_f.read_to_string(&mut buf).map_err(|e| match e.kind() { - ErrorKind::InvalidData => ME::GameSpecInvalidData, - ErrorKind::UnexpectedEof => ME::BadBundle(e.to_string()), - _ => e_f(e), - })?; - Ok::<_,ME>(buf) + let data = bundles::load_spec_to_read(ig,&spec)?; + Ok::<_,ME>(data) }))? } diff --git a/src/bundles.rs b/src/bundles.rs index 726720e6..0bca4eab 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -711,10 +711,19 @@ fn make_usvg(za: &mut IndexedZip, progress_count: &mut usize, //---------- specs ---------- #[throws(MgmtError)] -pub fn load_spec_to_read(ig: &Instance, spec_name: &str) -> ( - Box, - Box MgmtError> -) { +pub fn load_spec_to_read(ig: &Instance, spec_name: &str) -> String { + #[throws(MgmtError)] + fn read_from_read(spec_f: &mut dyn Read, + e_f: &mut dyn FnMut(io::Error) -> MgmtError) -> String { + let mut buf = String::new(); + spec_f.read_to_string(&mut buf).map_err(|e| match e.kind() { + ErrorKind::InvalidData => ME::GameSpecInvalidData, + ErrorKind::UnexpectedEof => ME::BadBundle(e.to_string()), + _ => e_f(e), + })?; + buf + } + let spec_leaf = format!("{}.game.toml", spec_name); // todo: game specs from bundles @@ -726,12 +735,13 @@ pub fn load_spec_to_read(ig: &Instance, spec_name: &str) -> ( debug!("{}: trying to loading builtin spec from {}", &ig.name, &path); match File::open(&path) { - Ok(f) => return ( - Box::new(f) as _, - Box::new(move |e| IE::from( - AE::from(e).context(path.clone()).context("read spec") - ).into()) as _, - ), + Ok(mut f) => { + return read_from_read(&mut f, &mut |e| { + IE::from( + AE::from(e).context(path.clone()).context("read spec") + ).into() + })?; + }, Err(e) if e.kind() == ErrorKind::NotFound => { }, Err(e) => throw!(IE::from( AE::from(e).context(path).context("try open game spec") -- 2.30.2