chiark / gitweb /
apitest: Break out filter_by_desc_glob etc.
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 24 Apr 2022 18:34:14 +0000 (19:34 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 24 Apr 2022 18:47:06 +0000 (19:47 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
apitest/at-bundles.rs
apitest/at-otter.rs
apitest/atmain.rs

index 9e2e86c90be035e79098fccd6b0b503acd9c5669..ede198d5bc4c93d1cdb2832bda620fa4079437b5 100644 (file)
@@ -26,9 +26,7 @@ impl Ctx {
       let pieces = alice.pieces::<PIA>()?;
       dbgc!(&pieces);
       for expect in &["a purple knight", "a yellow bishop"] {
-        pieces.iter().find(
-          |p| p.info["desc"].as_str() == Some(expect)
-        ).expect(expect);
+        let _: PIA = pieces.find_by_desc_glob(expect);
       }
 
       Ok(())
index 808da01fb51e8cc13d78d2139bbcfe5e06edc00b..4214301b3ededa2b43b37180c37fdf6dfd6487de 100644 (file)
@@ -37,14 +37,7 @@ impl Ctx {
 
     // ----- alice: claim alices' hand -----
 
-    let [hand] = a_pieces.iter_enumerated()
-      .filter(|(_i,p)| {
-        p.info["desc"] == otter::hand::UNCLAIMED_HAND_DESC
-      })
-      .map(|(i,_)| i)
-      .collect::<ArrayVec<_,1>>()
-      .into_inner().unwrap();
-    dbgc!(&hand);
+    let hand = a_pieces.find_by_desc_glob(otter::hand::UNCLAIMED_HAND_DESC);
 
     alice.api_piece(GH::With, PuSynch((&mut a_pieces, hand)), ("k", json!({
       "opname": "claim",
@@ -54,11 +47,9 @@ impl Ctx {
     // ----- find the pawns -----
 
     fn find_pawns<PI:Idx>(pieces: &PiecesSlice<PI>) -> [PI; 2] {
-      let mut pawns = pieces.iter_enumerated()
-        .filter(|(_i,p)| p.info["desc"].as_str().unwrap().ends_with(" pawn"))
-        .map(|(i,_)| i)
-        .take(2)
-        .collect::<ArrayVec<_,2>>()
+      let mut pawns = pieces
+        .ids_by_desc_glob("* pawn")
+        .take(2).collect::<ArrayVec<_,2>>()
         .into_inner().unwrap();
 
       pawns.sort_by_key(|&p| -pieces[p].pos.x());
index 085547f51cec1a507621f0177d39ed212b752b69..6deb69b238cb53b29d8436735d40c81343d84e66 100644 (file)
@@ -380,6 +380,31 @@ impl Session {
   }
 }
 
+#[ext(pub)]
+impl<PI> IndexSlice<PI, [PieceInfo<JsV>]> where PI: index_vec::Idx {
+  fn filter_by_desc_glob(&self, desc_glob: &str)
+                       -> Box<dyn Iterator<Item=(PI, &PieceInfo<JsV>)> + '_> {
+    let glob = glob::Pattern::new(desc_glob).unwrap();
+    let iter = self.iter_enumerated()
+      .filter(move |(_i,p)| glob.matches(p.info["desc"].as_str().unwrap()));
+    Box::new(iter) as _
+  }
+  fn ids_by_desc_glob(&self, desc_glob: &str)
+                      -> Box<dyn Iterator<Item=PI> + '_> {
+    let iter = self.filter_by_desc_glob(desc_glob)
+      .map(|(id,_p)| id);
+    Box::new(iter) as _
+  }
+  fn find_by_desc_glob(&self, desc_glob: &str) -> PI {
+    let [pc] = self.filter_by_desc_glob(desc_glob)
+      .map(|(i,_p)| i)
+      .collect::<ArrayVec<_,1>>()
+      .into_inner().expect("multiple pieces matched, unexpectedly");
+    dbgc!(desc_glob, pc);
+    pc
+  }
+}
+
 pub fn update_update_pieces<PI:Idx>(
   nick: &str,
   pieces: &mut Pieces<PI>,
@@ -521,10 +546,11 @@ impl UsualCtx {
 
       let mut session = self.connect_player(&self.alice)?;
       let pieces = session.pieces::<PIA>()?;
-      let llm = pieces.into_iter()
-        .filter(|pi| pi.info["desc"] == "a library load area marker")
-        .collect::<ArrayVec<_,2>>();
-      let llm: [_;2] = llm.into_inner().unwrap();
+      let llm: [_;2] = pieces
+        .filter_by_desc_glob("a library load area marker")
+        .map(|(_id,p)| p)
+        .collect::<ArrayVec<_,2>>()
+        .into_inner().unwrap();
       dbgc!(&llm);
 
       for (llm, &pos) in izip!(&llm, [PosC::new(5,5), PosC::new(50,25)].iter())