chiark / gitweb /
shapelib: Break out Catalouge::add_item
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 13 May 2022 23:36:25 +0000 (00:36 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 13 May 2022 23:36:25 +0000 (00:36 +0100)
We're going to want to call this again in add1, I think.

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

index c0e5630e243a3372ef91c878bb650cc7af9f5ae9..dfcb269ed6ba63e693e3b1acb49e8e9fe90d04a0 100644 (file)
@@ -1086,8 +1086,7 @@ pub fn load_catalogue(libname: &str, src: &mut dyn LibrarySource)
 
     for fe in gdefn.files.0 {
       process_files_entry(
-        libname, src, &mut l,
-        groupname,
+        src, &mut l,
         &gdefn.item_prefix, &gdefn.item_suffix, &gdefn.sort,
         &group, shape_calculable, fe
       )?;
@@ -1133,8 +1132,7 @@ fn subst(before: &str, needle: &'static str, replacement: &str)
 
 #[throws(LibraryLoadError)]
 fn process_files_entry(
-  libname: &str, src: &mut dyn LibrarySource, l: &mut Catalogue,
-  groupname: &str,
+  src: &mut dyn LibrarySource, l: &mut Catalogue,
   item_prefix: &str, item_suffix: &str, sort: &str,
   group: &Arc<GroupData>, shape_calculable: ShapeCalculable,
   fe: FileData
@@ -1195,21 +1193,7 @@ fn process_files_entry(
       shape_calculable,
       d: Arc::new(ItemDetails { desc }),
     };
-    type H<'e,X,Y> = hash_map::Entry<'e,X,Y>;
-    let new_item = SvgBaseName::note(
-      src, item_name.clone(), src_name.clone()
-    )?;
-    match l.items.entry(new_item) {
-      H::Occupied(oe) => throw!(LLE::DuplicateItem {
-        item: item_name.as_str().to_owned(),
-        group1: oe.get().group().groupname.clone(),
-        group2: groupname.to_owned(),
-      }),
-      H::Vacant(ve) => {
-        debug!("loaded shape {} {}", libname, item_name.as_str());
-        ve.insert(CatEnt::Item(idata));
-      }
-    };
+    l.add_item(src, src_name, item_name, CatEnt::Item(idata))?;
     Ok::<_,LLE>(())
   };
 
@@ -1231,6 +1215,31 @@ fn process_files_entry(
   }
 }
 
+impl Catalogue {
+  #[throws(LLE)]
+  fn add_item(&mut self,
+              src: &mut dyn LibrarySource, src_name: Result<&str,&SubstError>,
+              item_name: &GoodItemName, catent: CatalogueEntry) {
+    type H<'e,X,Y> = hash_map::Entry<'e,X,Y>;
+
+    let new_item = SvgBaseName::note(
+      src, item_name.clone(), src_name.clone()
+    )?;
+
+    match self.items.entry(new_item) {
+      H::Occupied(oe) => throw!(LLE::DuplicateItem {
+        item: item_name.as_str().to_owned(),
+        group1: oe.get().group().groupname.clone(),
+        group2: catent.group().groupname.clone(),
+      }),
+      H::Vacant(ve) => {
+        debug!("loaded shape {} {}", &self.libname, item_name.as_str());
+        ve.insert(catent);
+      }
+    };
+  }
+}
+
 //---------- reading, support functions ----------
 
 #[throws(LibraryLoadError)]