Loads of work to replace panics with results.
Some checks failed
continuous-integration/drone/push Build is failing

This commit is contained in:
2023-04-15 19:33:29 +02:00
parent 2849cad57b
commit 6f1880c768
25 changed files with 547 additions and 309 deletions

View File

@@ -1,3 +1,4 @@
use anyhow::{bail, Result};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
@@ -110,23 +111,23 @@ impl TurnChoice {
/// Helper function to get the move choice data from a turn. Note that this will panic if not
/// used on a move choice.
pub(crate) fn get_move_turn_data(&self) -> &MoveChoice {
pub(crate) fn get_move_turn_data(&self) -> Result<&MoveChoice> {
if let TurnChoice::Move(data) = self {
return data;
return Ok(data);
}
panic!("Invalid turn choice");
bail!("Invalid turn choice");
}
}
impl ScriptSource for TurnChoice {
fn get_script_count(&self) -> usize {
match self {
TurnChoice::Move(data) => data.get_script_count(),
TurnChoice::Item(data) => data.get_script_count(),
TurnChoice::Switch(data) => data.get_script_count(),
TurnChoice::Flee(data) => data.get_script_count(),
TurnChoice::Pass(data) => data.get_script_count(),
}
fn get_script_count(&self) -> Result<usize> {
Ok(match self {
TurnChoice::Move(data) => data.get_script_count()?,
TurnChoice::Item(data) => data.get_script_count()?,
TurnChoice::Switch(data) => data.get_script_count()?,
TurnChoice::Flee(data) => data.get_script_count()?,
TurnChoice::Pass(data) => data.get_script_count()?,
})
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
@@ -149,14 +150,15 @@ impl ScriptSource for TurnChoice {
}
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
match self {
TurnChoice::Move(data) => data.collect_scripts(scripts),
TurnChoice::Item(data) => data.collect_scripts(scripts),
TurnChoice::Switch(data) => data.collect_scripts(scripts),
TurnChoice::Flee(data) => data.collect_scripts(scripts),
TurnChoice::Pass(data) => data.collect_scripts(scripts),
}
TurnChoice::Move(data) => data.collect_scripts(scripts)?,
TurnChoice::Item(data) => data.collect_scripts(scripts)?,
TurnChoice::Switch(data) => data.collect_scripts(scripts)?,
TurnChoice::Flee(data) => data.collect_scripts(scripts)?,
TurnChoice::Pass(data) => data.collect_scripts(scripts)?,
};
Ok(())
}
}
@@ -230,8 +232,8 @@ impl MoveChoice {
}
impl ScriptSource for MoveChoice {
fn get_script_count(&self) -> usize {
1 + self.choice_data.user.get_script_count()
fn get_script_count(&self) -> Result<usize> {
Ok(self.choice_data.user.get_script_count()? + 1)
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
@@ -242,9 +244,9 @@ impl ScriptSource for MoveChoice {
scripts.push((&self.script).into());
}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
self.get_own_scripts(scripts);
self.choice_data.user.collect_scripts(scripts);
self.choice_data.user.collect_scripts(scripts)
}
}
@@ -273,8 +275,8 @@ impl ItemChoice {
}
impl ScriptSource for ItemChoice {
fn get_script_count(&self) -> usize {
0
fn get_script_count(&self) -> Result<usize> {
Ok(0)
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
@@ -283,8 +285,8 @@ impl ScriptSource for ItemChoice {
fn get_own_scripts(&self, _scripts: &mut Vec<ScriptWrapper>) {}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.choice_data.user.collect_scripts(scripts);
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
self.choice_data.user.collect_scripts(scripts)
}
}
@@ -313,8 +315,8 @@ impl SwitchChoice {
}
impl ScriptSource for SwitchChoice {
fn get_script_count(&self) -> usize {
0
fn get_script_count(&self) -> Result<usize> {
Ok(0)
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
@@ -323,8 +325,8 @@ impl ScriptSource for SwitchChoice {
fn get_own_scripts(&self, _scripts: &mut Vec<ScriptWrapper>) {}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.choice_data.user.collect_scripts(scripts);
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
self.choice_data.user.collect_scripts(scripts)
}
}
@@ -352,8 +354,8 @@ impl FleeChoice {
}
impl ScriptSource for FleeChoice {
fn get_script_count(&self) -> usize {
0
fn get_script_count(&self) -> Result<usize> {
Ok(0)
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
@@ -362,8 +364,8 @@ impl ScriptSource for FleeChoice {
fn get_own_scripts(&self, _scripts: &mut Vec<ScriptWrapper>) {}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.choice_data.user.collect_scripts(scripts);
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
self.choice_data.user.collect_scripts(scripts)
}
}
@@ -392,8 +394,8 @@ impl PassChoice {
}
impl ScriptSource for PassChoice {
fn get_script_count(&self) -> usize {
0
fn get_script_count(&self) -> Result<usize> {
Ok(0)
}
fn get_script_source_data(&self) -> &RwLock<ScriptSourceData> {
@@ -402,8 +404,8 @@ impl ScriptSource for PassChoice {
fn get_own_scripts(&self, _scripts: &mut Vec<ScriptWrapper>) {}
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) {
self.choice_data.user.collect_scripts(scripts);
fn collect_scripts(&self, scripts: &mut Vec<ScriptWrapper>) -> Result<()> {
self.choice_data.user.collect_scripts(scripts)
}
}