From: Ian Jackson Date: Sun, 27 Sep 2020 10:38:17 +0000 (+0100) Subject: wip list_glob X-Git-Tag: otter-0.2.0~864 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=7182f8e4234e61b2df3c39b585a13e8c3327b76b;p=otter.git wip list_glob Signed-off-by: Ian Jackson --- diff --git a/src/cmdlistener.rs b/src/cmdlistener.rs index 3eb53d16..6f9757f6 100644 --- a/src/cmdlistener.rs +++ b/src/cmdlistener.rs @@ -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) }), diff --git a/src/commands.rs b/src/commands.rs index aa0d932f..72820013 100644 --- a/src/commands.rs +++ b/src/commands.rs @@ -15,6 +15,9 @@ pub enum MgmtCommand { name: String, insns: Vec, 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 { diff --git a/src/shapelib.rs b/src/shapelib.rs index eeec5316..0d9dd164 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -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 { 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,SpecError> { - let idata = self.items.get(name).ok_or(SE::LibraryItemNotFound)?; - + fn load1(&self, idata: &ItemData, name: &str) + -> Result,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 { + 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")]