chiark / gitweb /
shapelib: Split find_then from find_load
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 13 May 2022 21:31:55 +0000 (22:31 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 13 May 2022 23:07:48 +0000 (00:07 +0100)
This is preparatory to making the catalogue capable of holding magic
pieces.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/shapelib.rs

index 5182f85f9d6e348ca12d62138b0148d146647fe0..d02c8d76386dbb5d68a789c70d801ecc87e5c365 100644 (file)
@@ -413,14 +413,25 @@ impl From<ItemSpecLoaded> for SpecLoaded {
 
 impl ItemSpec {
   #[throws(SpecError)]
-  fn find_load(&self, ig: &Instance, depth: SpecDepth) -> ItemSpecLoaded {
+  fn find_then<F,T>(&self, ig: &Instance, then: F) -> T
+  where F: FnOnce(&Catalogue, &SvgBaseName<GoodItemName>, &ItemData)
+                  -> Result<T, SpecError>
+  {
     let regs = ig.all_shapelibs();
     let libs = regs.lib_name_lookup(&self.lib)?;
     let (lib, (item, idata)) = libs.iter().rev().find_map(
       |lib| Some((lib, lib.items.get_key_value(self.item.as_str())?))
     )
       .ok_or_else(|| SpE::LibraryItemNotFound(self.clone()))?;
-    lib.load1(idata, &self.lib, item.unnest::<str>(), ig, depth)?
+    then(lib, item, idata)?
+  }
+
+  #[throws(SpecError)]
+  fn find_load(&self, ig: &Instance,
+                   depth: SpecDepth) -> ItemSpecLoaded {
+    self.find_then(ig, |lib, item, idata| {
+      lib.load1(idata, &self.lib, item.unnest::<str>(), ig, depth)
+    })?
   }
 
   fn from_strs<L,I>(lib: &L, item: &I) -> Self