From 18c67a48a1e7f98f5206fc9c84630cf54335b220 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Wed, 12 May 2021 00:17:47 +0100 Subject: [PATCH] shapelib: Reorganise BundleParseErrorHandling Signed-off-by: Ian Jackson --- src/bundles.rs | 50 ++++++++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 20 deletions(-) diff --git a/src/bundles.rs b/src/bundles.rs index 63703f0a..cc07d59d 100644 --- a/src/bundles.rs +++ b/src/bundles.rs @@ -320,12 +320,13 @@ impl<'z,R> IntoIterator for &'z IndexedZip where R: Read + io::Seek { } } -trait BundleParseError: Sized { - fn required(bpath: &str, f:F) -> Result +trait BundleParseErrorHandling: Copy { + type Err; + fn required(self, f:F) -> Result where XE: Into, F: FnOnce() -> Result; - fn besteffort(bpath: &str, f:F, g:G) -> Result + fn besteffort(self, f:F, g:G) -> Result where XE: Into, F: FnOnce() -> Result, G: FnOnce() -> T; @@ -338,8 +339,11 @@ enum ReloadError { } display_as_debug!{ReloadError} -impl BundleParseError for ReloadError { - fn required(bpath: &str, f:F) -> Result +#[derive(Debug,Copy,Clone)] +struct BundleParseReload<'s>{ pub bpath: &'s str } +impl BundleParseErrorHandling for BundleParseReload<'_> { + type Err = ReloadError; + fn required(self, f:F) -> Result where XE: Into, F: FnOnce() -> Result { @@ -348,17 +352,17 @@ impl BundleParseError for ReloadError { let le: LE = xe.into(); match le { LE::BadBundle(why) => RLE::Unloadable( - format!("{}: {}", bpath, &why) + format!("{}: {}", self.bpath, &why) ), LE::IE(IE::Anyhow(ae)) => RLE::IE(IE::Anyhow( - ae.context(bpath.to_owned()) + ae.context(self.bpath.to_owned()) )), LE::IE(ie) => RLE::IE(ie), } }) } - fn besteffort(bpath: &str, f:F, g:G) -> Result + fn besteffort(self, f:F, g:G) -> Result where XE: Into, F: FnOnce() -> Result, G: FnOnce() -> T, @@ -366,10 +370,12 @@ impl BundleParseError for ReloadError { Ok(f().unwrap_or_else(|e| { match e.into() { LE::IE(ie) => { - error!("reloading, error, partially skipping {}: {}", bpath, ie); + error!("reloading, error, partially skipping {}: {}", + self.bpath, ie); }, LE::BadBundle(why) => { - warn!("reloading, partially skipping {}: {}", bpath, why); + warn!("reloading, partially skipping {}: {}", + self.bpath, why); }, } g() @@ -377,15 +383,18 @@ impl BundleParseError for ReloadError { } } -impl BundleParseError for LoadError { - fn required(_bpath: &str, f:F) -> Result +#[derive(Debug,Copy,Clone)] +struct BundleParseUpload; +impl BundleParseErrorHandling for BundleParseUpload { + type Err = LoadError; + fn required(self, f:F) -> Result where XE: Into, F: FnOnce() -> Result { f().map_err(Into::into) } - fn besteffort(_bpath: &str, f:F, _:G) -> Result + fn besteffort(self, f:F, _:G) -> Result where XE: Into, F: FnOnce() -> Result, G: FnOnce() -> T, @@ -394,17 +403,17 @@ impl BundleParseError for LoadError { } } -#[throws(EH)] -fn parse_bundle(id: Id, file: &mut F, bpath: &str) -> Parsed -where EH: BundleParseError, +#[throws(EH::Err)] +fn parse_bundle(id: Id, file: &mut F, eh: EH) -> Parsed +where EH: BundleParseErrorHandling, F: Read + io::Seek { match id.kind { Kind::Zip => () } - let mut za = EH::required(bpath, ||{ + let mut za = eh.required(||{ IndexedZip::new(file) })?; - let meta = EH::besteffort(bpath, ||{ + let meta = eh.besteffort(||{ const META: &str = "otter.toml"; let mut mf = za.by_name_caseless(META)? .ok_or_else(|| LE::BadBundle(format!("bundle missing {}", META)))?; @@ -522,7 +531,8 @@ impl InstanceBundles { ib.bundles.resize_with(iu+1, default); } - let parsed = match parse_bundle::(id, &mut file, fpath) { + let eh = BundleParseReload { bpath: fpath }; + let parsed = match parse_bundle(id, &mut file, eh) { Ok(y) => y, Err(e) => { debug!("bundle file {:?} reload failed {}", &fpath, e); @@ -598,7 +608,7 @@ impl Uploading { file.rewind().context("rewind"). map_err(IE::from)?; let mut file = BufReader::new(file); - let parsed = parse_bundle::(id, &mut file, &tmp)?; + let parsed = parse_bundle(id, &mut file, BundleParseUpload)?; process_bundle(id, &*instance, for_progress)?; -- 2.30.2