chiark / gitweb /
apitest: Provide initial_[vp]id_by_desc_glob
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 12 Jun 2022 19:29:08 +0000 (20:29 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sun, 12 Jun 2022 19:55:35 +0000 (20:55 +0100)
The plan is to replace literal pieceids with this.

Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
apitest/apitest.rs
src/imports.rs

index 8bad2194918f050196601136e1d1bebb7f81a40a..d7932bee904ed61b90ff8c7ebe7c6cf1a609499f 100644 (file)
@@ -89,6 +89,7 @@ pub struct SetupCore {
   pub server_child: Child,
   pub wanted_tests: TrackWantedTests,
   pub cln: cleanup_notify::Handle,
+  pub initial_pieces_cache: Option<Vec<MgmtGamePieceInfo>>,
 }
 
 #[derive(Clone,Debug)]
@@ -1053,6 +1054,30 @@ impl MgmtChannel {
   }
 }
 
+impl SetupCore {
+  #[throws(AE)]
+  pub fn initial_id_by_desc_glob(&mut self, desc_glob: &str) -> PieceId {
+    let pieces = self.initial_pieces_cache.get_or_insert_with(||{
+      self.mgmt_conn.borrow_mut().list_pieces().unwrap().0
+    });
+
+    let glob = glob::Pattern::new(desc_glob).unwrap();
+    pieces.iter().filter_map(|mgpi| {
+
+      let vis = mgpi.visible.as_ref()?;
+      glob.matches(vis.desc_html.as_html_str()).then(||())?;
+      Some(mgpi.piece)
+
+    }).exactly_one().map_err(|_| anyhow!("not exactly one {:?}", desc_glob))?
+  }
+
+  #[throws(AE)]
+  pub fn initial_vpid_by_desc_glob(&mut self, desc_glob: &str) -> String {
+    let id = self.initial_id_by_desc_glob(desc_glob)?;
+    VisiblePieceId::from(id.data()).to_string()
+  }
+}
+
 // ==================== core entrypoint, for wdriver too ====================
 
 #[throws(AE)]
@@ -1125,6 +1150,7 @@ pub fn setup_core<O>(module_paths: &[&str]) ->
      mgmt_conn: mgmt_conn.into(),
      server_child,
      wanted_tests,
+     initial_pieces_cache: None,
    })
 }
 
index 10baa4409474e812d7228b4df71efac13c648d2f..2db507e0e00849a8e833dbf270475cb380e5e219 100644 (file)
@@ -83,6 +83,7 @@ pub use crate::commands::{AccessTokenInfo, AccessTokenReport, MgmtError};
 pub use crate::commands::{MgmtCommand, MgmtResponse};
 pub use crate::commands::{MgmtGameInstruction, MgmtGameResponse};
 pub use crate::commands::{MgmtBundleList, MgmtGameUpdateMode};
+pub use crate::commands::{MgmtGamePieceInfo};
 pub use crate::commands::{ProgressUpdateMode};
 pub use crate::debugreader::DebugReader;
 pub use crate::error::*;