chiark / gitweb /
apitest: Use Loop
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 26 Feb 2021 12:51:34 +0000 (12:51 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Fri, 26 Feb 2021 12:51:34 +0000 (12:51 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
Cargo.lock.example
apitest.rs
apitest/Cargo.toml
apitest/at-otter.rs

index 5f554436abc5ce4cc1a534306bf9d7e796a5ecfa..f69cba4ed5e39339fea9b461aa622ff5e6777258 100644 (file)
@@ -1824,6 +1824,7 @@ dependencies = [
 name = "otter-api-tests"
 version = "0.0.1"
 dependencies = [
+ "ego-tree",
  "fehler",
  "humantime",
  "num-traits",
index c245559d0cd2bc7b3e7403539ffee1045a5a2b10..4fbd060ba9ed40b05c4f66724a4c3a2a865eb1c8 100644 (file)
@@ -37,7 +37,7 @@ pub use std::env;
 pub use std::fmt::{self, Debug};
 pub use std::collections::hash_map::HashMap;
 pub use std::collections::btree_set::BTreeSet;
-pub use std::convert::TryInto;
+pub use std::convert::{Infallible, TryInto};
 pub use std::fs;
 pub use std::io::{self, BufRead, BufReader, ErrorKind, Write};
 pub use std::iter;
@@ -69,6 +69,7 @@ pub use otter::spec::{Coord, GameSpec, Pos, PosC};
 pub use otter::toml_de;
 pub use otter::ui::{AbbrevPresentationLayout, PresentationLayout};
 pub use otter::ui::player_num_dasharray;
+pub use otter::utils::*;
 
 pub const MS: time::Duration = time::Duration::from_millis(1);
 pub type AE = anyhow::Error;
index 8e69c13d5ff39f39333d4705da6e1a586f39e49b..46a99314e154fd545bde50ad68a118eba52b8bda 100644 (file)
@@ -13,6 +13,7 @@ edition = "2018"
 [dependencies]
 otter = { path = ".." }
 
+ego-tree = "0.6"
 humantime = "2"
 reqwest = { version = "0.11", features = ["blocking", "stream"] }
 scraper = "0.12"
index c438f172552ac86edf87a4f09e1536b0d9f8086b..d5ce0ac280e0eeaa14dbda0f2a388486e13e1843 100644 (file)
@@ -127,16 +127,25 @@ impl Ctx {
   }
 }
 
+#[derive(Debug,Clone)]
+struct PieceInfo<I> {
+  id: (),
+  info: I,
+}
+
 impl Session {
   #[throws(AE)]
-  fn pieces(&self) -> Vec<((/*will be piece id*/), serde_json::Value)> {
+  fn pieces(&self) -> Vec<PieceInfo<serde_json::Value>> {
     self.dom
       .element("#pieces_marker")
       .unwrap().next_siblings()
-      .filter_map(|pu| pu.value().as_element())
-      .map(|pu| ((), pu.attr("data-info")))
-      .take_while(|(_,attr)| attr.is_some())
-      .map(|(id,attr)| (id, serde_json::from_str(attr.unwrap()).unwrap()))
+      .map_loop(|puse: ego_tree::NodeRef<scraper::Node>| {
+        dbg!(puse);
+        let puse = puse.value().as_element().ok_or(Loop::Continue)?;
+        let attr = puse.attr("data-info").ok_or(Loop::Break)?;
+        let info = serde_json::from_str(attr).unwrap();
+        Ok::<_,Loop<Infallible>>(PieceInfo { id: (), info })
+      })
       .collect()
   }
 }
@@ -163,10 +172,14 @@ impl Ctx {
                Some(EXIT_NOTFOUND));
 
     let session = self.connect_player(&self.alice)?;
-    let llm: [_;2] = session.pieces()?.into_iter()
-      .filter(|(_,info)| info["desc"] == "a library load area marker")
-      .collect::<ArrayVec<_>>().into_inner().unwrap();
-    dbg!(llm);
+    let pieces = session.pieces()?;
+    dbg!(&pieces);
+    let llm = pieces.into_iter()
+      .filter(|pi| pi.info["desc"] == "a library load area marker")
+      .collect::<ArrayVec<_>>();
+    dbg!(&llm);
+    let llm: [_;2] = llm.into_inner().unwrap();
+    dbg!(&llm);
     // xxx find load markers ids
 
     // xxx find load markers' locations