chiark / gitweb /
apitest: break out await_update
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Feb 2021 00:26:04 +0000 (00:26 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 27 Feb 2021 00:26:04 +0000 (00:26 +0000)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
apitest/at-otter.rs

index 724a637cbb6c0bbd326bce24b814f7075ca0d231..98454cb0de19af29b803b0f148f22bf6fe36bf1e 100644 (file)
@@ -223,22 +223,37 @@ impl Session {
   }
 
   #[throws(AE)]
-  fn resynch_pieces(&mut self) {
+  fn await_update<
+    R,
+    G: FnMut(&mut Session, Generation) -> Option<R>,
+    F: FnMut(&mut Session, Generation, &str, &serde_json::Value) -> Option<R>,
+   > (&mut self, mut f: F, mut g: G) -> R {
     'overall: loop {
       let update = self.updates.recv()?;
       let update = update.as_array().unwrap();
-      let new_gen = &update[0];
+      let new_gen = Generation(
+        update[0]
+          .as_i64().unwrap()
+          .try_into().unwrap()
+      );
+      if let Some(y) = g(self, new_gen) { break 'overall y }
       for ue in update[1].as_array().unwrap() {
         let (k,v) = ue.as_object().unwrap().iter().next().unwrap();
-        let got_cseq: RawClientSequence = match k.as_str() {
-          "Recorded" => v["cseq"].as_i64().unwrap().try_into().unwrap(),
-          "Log" => continue,
-          _ => throw!(anyhow!("unknown update: {}", ue)),
-        };
-        if got_cseq == self.cseq { break 'overall }
+        if let Some(y) = f(self, new_gen, k, v) { break 'overall y }
       }
     }
   }
+
+  #[throws(AE)]
+  fn resynch_pieces(&mut self) {
+    self.await_update(|session, _gen, k, v| {
+      let got_cseq: RawClientSequence = match k {
+        "Recorded" => v["cseq"].as_i64().unwrap().try_into().unwrap(),
+        _ => return None,
+      };
+      if got_cseq == session.cseq { Some(()) } else { None }
+    }, |_,_| None)?;
+  }
 }
 
 impl Ctx {