From: Ian Jackson Date: Fri, 26 Feb 2021 11:09:20 +0000 (+0000) Subject: apitest: break out Session::pieces X-Git-Tag: otter-0.4.0~355 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=f71252cd3050f4bb9f98c5ec86b1c64dae0974ea;p=otter.git apitest: break out Session::pieces Signed-off-by: Ian Jackson --- diff --git a/apitest/at-otter.rs b/apitest/at-otter.rs index f10550bc..fda71406 100644 --- a/apitest/at-otter.rs +++ b/apitest/at-otter.rs @@ -127,6 +127,20 @@ impl Ctx { } } +impl Session { + #[throws(AE)] + fn pieces(&self) -> Vec<((/*will be piece id*/), 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())) + .collect() + } +} + impl Ctx { #[throws(AE)] pub fn otter>(&mut self, args: &[S]) { @@ -150,10 +164,7 @@ impl Ctx { let session = self.connect_player(&self.alice)?; - for pu in session.dom.element("#pieces_marker").unwrap().next_siblings() { - let pu = match pu.value().as_element() { Some(pu) => pu, _ => continue }; - let info = match pu.attr("data-info") { Some(info) => info, _ => break }; - let info: serde_json::Value = serde_json::from_str(&info)?; + for (_, info) in session.pieces()? { let desc = &info["desc"]; dbg!(&desc); }