chiark / gitweb /
specs: Break out reset_game_from_spec
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 18 May 2021 20:18:45 +0000 (21:18 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Tue, 18 May 2021 20:18:45 +0000 (21:18 +0100)
Signed-off-by: Ian Jackson <ijackson@chiark.greenend.org.uk>
daemon/cmdlistener.rs

index 3bdb1358e3cceebbfe9709c2b12a80e1fe9b4fd1..5e47e87fa1c92a9b231a5a3dff4ea79400e38e78 100644 (file)
@@ -553,6 +553,38 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>(
      None, insns, ig)
   }
 
+  #[throws(MgmtError)]
+  fn reset_game_from_spec<'igr, 'ig: 'igr, 'cs>(
+    cs: &'cs CommandStreamData,
+    ag: &'_ mut AccountsGuard,
+    ig: &'igr mut Unauthorised<InstanceGuard<'ig>, InstanceName>,
+    who: &'_ Html,
+    spec_toml: String,
+  ) -> ExecuteGameInsnResults<'igr, 'ig>
+  {
+    let spec: toml::Value = spec_toml.parse()
+      .map_err(|e: toml::de::Error| ME::TomlSyntaxError(e.to_string()))?;
+    let GameSpec {
+      pieces, table_size, table_colour, pcaliases,
+    } = toml_de::from_value(&spec)
+      .map_err(|e: toml_de::Error| ME::TomlStructureError(e.to_string()))?;
+
+    reset_game(cs,ag,ig, Box::new(|_ig, insns|{
+      // Define new stuff:
+      for (alias, target) in pcaliases.into_iter() {
+        insns.push(MGI::DefinePieceAlias{ alias, target });
+      }
+      insns.push(MGI::ClearLog);
+      insns.push(MGI::SetTableSize(table_size));
+      insns.push(MGI::SetTableColour(table_colour));
+      for pspec in pieces.into_iter() {
+        insns.push(MGI::AddPieces(pspec));
+      }
+      let html = hformat!("{} reset the game", &who);
+      Ok(LogEntry { html })
+    }))?
+  }
+
   impl<'cs> CommandStreamData<'cs> {
     #[throws(MgmtError)]
     fn check_acl_manip_player_access<'igr, 'ig: 'igr>(
@@ -657,27 +689,7 @@ fn execute_game_insn<'cs, 'igr, 'ig: 'igr>(
       }))?
     }
     MGI::ResetFromGameSpec { spec_toml: spec } => {
-      let spec: toml::Value = spec.parse()
-        .map_err(|e: toml::de::Error| ME::TomlSyntaxError(e.to_string()))?;
-      let GameSpec {
-        pieces, table_size, table_colour, pcaliases,
-      } = toml_de::from_value(&spec)
-        .map_err(|e: toml_de::Error| ME::TomlStructureError(e.to_string()))?;
-
-      reset_game(cs,ag,ig, Box::new(|_ig, insns|{
-        // Define new stuff:
-        for (alias, target) in pcaliases.into_iter() {
-          insns.push(MGI::DefinePieceAlias{ alias, target });
-        }
-        insns.push(MGI::ClearLog);
-        insns.push(MGI::SetTableSize(table_size));
-        insns.push(MGI::SetTableColour(table_colour));
-        for pspec in pieces.into_iter() {
-          insns.push(MGI::AddPieces(pspec));
-        }
-        let html = hformat!("{} reset the game", &who);
-        Ok(LogEntry { html })
-      }))?
+      reset_game_from_spec(cs,ag,ig,who, spec)?
     }
 
     MGI::InsnMark(token) => {