chiark / gitweb /
wip list_glob
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Sep 2020 10:38:17 +0000 (11:38 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 27 Sep 2020 10:38:17 +0000 (11:38 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
src/cmdlistener.rs
src/commands.rs
src/shapelib.rs

index 3eb53d16d0fa3334ef298452454ef61d96ad07e0..6f9757f620e1d07f6e1c1e5e7cdca407a703644e 100644 (file)
@@ -108,6 +108,10 @@ fn execute(cs: &mut CommandStream, cmd: MgmtCommand) -> MgmtResponse {
       let mut g = gref.lock()?;
       execute_for_game(cs, &mut g, insns, how)?
     },
+
+    LibraryListByGlob { glob: _pat } => {
+      panic!() //xxx
+    },
   }
 }
 
@@ -166,7 +170,8 @@ fn execute_game_insn(cs: &CommandStream,
         let pinfo = ig.pieces.get(piece)?;
         let desc_html = pinfo.describe_html(None);
         let itemname = pinfo.itemname().to_string();
-        Some(MgmtGamePieceInfo { piece, pos, face, desc_html, itemname })
+        let bbox = pinfo.bbox_approx();
+        Some(MgmtGamePieceInfo { piece, pos, face, desc_html, bbox, itemname })
       }).flatten().collect();
       Resp::Pieces(pieces)
     }),
index aa0d932fcfb50e1ddbbe8113ed1ab23bc51bfb8b..72820013503ed8b052eb0f051317a137199d0899 100644 (file)
@@ -15,6 +15,9 @@ pub enum MgmtCommand {
     name: String, insns: Vec<MgmtGameInstruction>,
     how: MgmtGameUpdateMode,
   },
+  LibraryListByGlob {
+    glob: shapelib::ItemSpec,
+  },
 }
 
 #[derive(Debug,Serialize,Deserialize)]
@@ -66,6 +69,7 @@ pub struct MgmtGamePieceInfo {
   pub pos: Pos,
   pub face: FaceId,
   pub desc_html: Html,
+  pub bbox: [Pos;2],
 }
 
 #[derive(Debug,Copy,Clone,Serialize,Deserialize)]
@@ -87,6 +91,7 @@ pub enum MgmtError {
   PieceNotFound,
   LimitExceeded,
   ServerFailure(String),
+  BadGlob { pat: String, msg: String },
   BadSpec(#[from] SpecError),
 }
 impl Display for MgmtError {
index eeec5316bf62f95a8394e5ccc0472903918777c6..0d9dd1640f2d2fa30fbce237497bfa258633eae6 100644 (file)
@@ -142,8 +142,8 @@ struct Item {
 #[derive(Debug,Clone,Serialize,Deserialize)]
 struct ItemEnquiryResult {
   pub itemname: String,
-  pub bbbox: [Pos; 2],
   pub f0desc: Html,
+  pub f0bbox: [Pos; 2],
 }
 
 #[typetag::serde(name="Lib")]
@@ -189,14 +189,14 @@ impl ItemSpec {
   pub fn load(&self) -> Box<dyn Piece> {
     let libs = GLOBAL.shapelibs.read().unwrap(); 
     let lib = libs.get(&self.lib).ok_or(SE::LibraryNotFound)?;
-    lib.load1(&self.item)?
+    let idata = lib.items.get(&self.item).ok_or(SE::LibraryItemNotFound)?;
+    lib.load1(idata, &self.item)?
   }
 }
 
 impl Contents {
-  fn load1(&self, name: &str) -> Result<Box<dyn Piece>,SpecError> {
-    let idata = self.items.get(name).ok_or(SE::LibraryItemNotFound)?;
-
+  fn load1(&self, idata: &ItemData, name: &str)
+           -> Result<Box<dyn Piece>,SpecError> {
     let svg_path = format!("{}/{}.usvg", self.dirname, &name);
     let svg_data = fs::read_to_string(&svg_path)
       .map_err(|e| if e.kind() == ErrorKind::NotFound {
@@ -226,6 +226,29 @@ impl Contents {
                     itemname: name.to_string() };
     Ok(Box::new(it))
   }
+
+  #[throws(MgmtError)]
+  fn list_glob(&self, pat: &str) -> Vec<ItemEnquiryResult> {
+    let pat = glob::Pattern::new(pat).map_err(|pe| MgmtError::BadGlob {
+      pat: pat.to_string(), msg: pe.msg.to_string() })?;
+    let mut out = vec![];
+    for (k,v) in &self.items {
+      if !pat.matches(&k) { continue }
+      let loaded = match self.load1(v, &k) {
+        Err(SpecError::LibraryItemNotFound) => continue,
+        e@ Err(_) => e?,
+        Ok(r) => r,
+      };
+      let f0bbox = loaded.bbox_approx();
+      let ier = ItemEnquiryResult {
+        itemname: k.clone(),
+        f0bbox,
+        f0desc: loaded.describe_html(Some(Default::default())),
+      };
+      out.push(ier);
+    }
+    out
+  }
 }
 
 #[typetag::serde(name="Lib")]