From ecec7351f943a30cfc792f9a89d0fcc08b08d009 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Fri, 26 Feb 2021 12:51:34 +0000 Subject: [PATCH] apitest: Use Loop Signed-off-by: Ian Jackson --- Cargo.lock.example | 1 + apitest.rs | 3 ++- apitest/Cargo.toml | 1 + apitest/at-otter.rs | 31 ++++++++++++++++++++++--------- 4 files changed, 26 insertions(+), 10 deletions(-) diff --git a/Cargo.lock.example b/Cargo.lock.example index 5f554436..f69cba4e 100644 --- a/Cargo.lock.example +++ b/Cargo.lock.example @@ -1824,6 +1824,7 @@ dependencies = [ name = "otter-api-tests" version = "0.0.1" dependencies = [ + "ego-tree", "fehler", "humantime", "num-traits", diff --git a/apitest.rs b/apitest.rs index c245559d..4fbd060b 100644 --- a/apitest.rs +++ b/apitest.rs @@ -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; diff --git a/apitest/Cargo.toml b/apitest/Cargo.toml index 8e69c13d..46a99314 100644 --- a/apitest/Cargo.toml +++ b/apitest/Cargo.toml @@ -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" diff --git a/apitest/at-otter.rs b/apitest/at-otter.rs index c438f172..d5ce0ac2 100644 --- a/apitest/at-otter.rs +++ b/apitest/at-otter.rs @@ -127,16 +127,25 @@ impl Ctx { } } +#[derive(Debug,Clone)] +struct PieceInfo { + id: (), + info: I, +} + impl Session { #[throws(AE)] - fn pieces(&self) -> Vec<((/*will be piece id*/), serde_json::Value)> { + fn pieces(&self) -> Vec> { 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| { + 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>(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::>().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::>(); + dbg!(&llm); + let llm: [_;2] = llm.into_inner().unwrap(); + dbg!(&llm); // xxx find load markers ids // xxx find load markers' locations -- 2.30.2