chiark / gitweb /
hidden: recalculate: further generalise - return type
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Feb 2021 00:24:22 +0000 (00:24 +0000)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Wed, 17 Feb 2021 00:26:59 +0000 (00:26 +0000)
NFC

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

index 4c78c59a00b38e7aa6da4a4cad8c010e083c437d..c0b2c22df3d06fbaacfec84d439fa5627e7d1277 100644 (file)
@@ -211,16 +211,21 @@ pub fn massage_prep_piecestate(
 
 #[throws(InternalError)]
 fn recalculate_occultation_general<
+  R,
+  V: FnOnce(Vec<LogEntry>) -> R,
   L: FnOnce(&Html, Html, Html, Option<&Html>) -> Vec<LogEntry>,
+  F: FnOnce(PieceUpdateOps_PerPlayer, Vec<LogEntry>) -> R,
 >(
   gs: &mut GameState,
   who_by: Html,
   ipieces: &PiecesLoaded,
   piece: PieceId,
-  vanilla: PUFOS,
+  log_visible: Vec<LogEntry>,
+  ret_vanilla: V,
   log_callback: L,
+  ret_callback: F,
 )
-  -> PieceUpdate
+  -> R
 {
   // fallible part
   let (puos, log, occids): (_, _, OldNew<Option<OccId>>) = {
@@ -251,7 +256,7 @@ fn recalculate_occultation_general<
     ].into();
 
     let occids = occulteds.map(|h| h.as_ref().map(|occ| occ.occid));
-    if occids.old() == occids.new() { return vanilla.into(); }
+    if occids.old() == occids.new() { return ret_vanilla(log_visible); }
 
   /*
     #[throws(IE)]
@@ -329,7 +334,7 @@ fn recalculate_occultation_general<
 
     let log = match most_obscure {
       OccK::Visible => {
-        vanilla.2
+        log_visible
       }
       OccK::Scrambled | OccK::Displaced{..} => {
         let face = ipc.nfaces() - 1;
@@ -362,11 +367,7 @@ fn recalculate_occultation_general<
     gs.pieces.byid_mut(piece).unwrap().occult.passive = *occids.new();
   })(); // <- no ?, infallible commitment
 
-  PieceUpdate {
-    wrc: WRC::Unpredictable,
-    ops: PieceUpdateOps::PerPlayer(puos),
-    log,
-  }
+  ret_callback(puos, log)
 }
 
 #[throws(InternalError)]
@@ -375,7 +376,7 @@ pub fn recalculate_occultation_piece(
   who_by: Html,
   ipieces: &PiecesLoaded,
   piece: PieceId,
-  vanilla: PUFOS,
+  (vanilla_wrc, vanilla_op, vanilla_log): PUFOS,
 )
   -> PieceUpdate
 {
@@ -384,12 +385,18 @@ pub fn recalculate_occultation_piece(
     who_by,
     ipieces,
     piece,
-    vanilla,
+    vanilla_log,
+    |log| (vanilla_wrc, vanilla_op, log).into(),
     |who_by, old, new, show| vec![ LogEntry { html: Html(format!(
       "{} moved {} from {} to {}",
       &who_by.0,
       if let Some(show) = show { &show.0 } else { "something" },
       &old.0, &new.0,
-    ))}]
+    ))}],
+    |puos, log| PieceUpdate {
+      wrc: WRC::Unpredictable,
+      ops: PieceUpdateOps::PerPlayer(puos),
+      log
+    }
   )?
 }
index 9bf07a7f37a2f1541ab7aace5ec437df0f7d88c4..dbc434378bb52a98ae400a89c091f3c15adff1ee 100644 (file)
@@ -119,9 +119,13 @@ pub struct PieceUpdate {
 #[derive(Debug)]
 pub enum PieceUpdateOps {
   Simple(PieceUpdateOp<(),()>),
-  PerPlayer(SecondarySlotMap<PlayerId, PieceUpdateOp<(),()>>),
+  PerPlayer(PieceUpdateOps_PerPlayer),
 }
 
+#[allow(non_camel_case_types)]
+pub type PieceUpdateOps_PerPlayer =
+  SecondarySlotMap<PlayerId, PieceUpdateOp<(),()>>;
+
 impl From<PieceUpdateOp<(),()>> for PieceUpdateOps {
   fn from(op: PieceUpdateOp<(),()>) -> Self { PUOs::Simple(op) }
 }