A bunch more work on replacing every potential panic with results
All checks were successful
continuous-integration/drone/push Build is passing

This commit is contained in:
2023-04-16 19:57:21 +02:00
parent 1b8403ecda
commit 00d596d656
37 changed files with 526 additions and 300 deletions

View File

@@ -1,7 +1,7 @@
use crate::dynamic_data::choices::TurnChoice;
use crate::dynamic_data::script_handling::ScriptSource;
use crate::dynamic_data::Pokemon;
use crate::{script_hook, PkmnError, ValueIdentifiable, ValueIdentifier};
use crate::{script_hook, PkmnError, ValueIdentifiable, ValueIdentifier, VecExt};
use anyhow::Result;
use anyhow_ext::anyhow;
use parking_lot::lock_api::MappedRwLockReadGuard;
@@ -81,9 +81,7 @@ impl ChoiceQueue {
let len = self.queue.read().len();
let mut write_lock = self.queue.write();
for index in self.current..len {
let choice = &mut write_lock
.get_mut(index)
.ok_or(PkmnError::IndexOutOfBounds { index, len })?;
let choice = &mut write_lock.get_mut_res(index)?;
if let Some(choice) = choice {
let mut speed = choice.user().boosted_stats().speed();
script_hook!(change_speed, (*choice), choice, &mut speed);
@@ -121,14 +119,9 @@ impl ChoiceQueue {
return Ok(true);
}
let len = queue_lock.len();
// Take the choice we want to move forward out of it's place.
let choice = queue_lock
.get_mut(desired_index)
.ok_or(PkmnError::IndexOutOfBounds {
index: self.current,
len,
})?
.get_mut_res(desired_index)?
.take()
.ok_or(anyhow!("Choice was already taken"))?;
// Iterate backwards from the spot before the choice we want to move up, push them all back
@@ -136,15 +129,8 @@ impl ChoiceQueue {
for index in (self.current..desired_index).rev() {
queue_lock.swap(index, index + 1);
}
let len = queue_lock.len();
// Place the choice that needs to be next in the next to be executed position.
let _ = queue_lock
.get_mut(self.current)
.ok_or(PkmnError::IndexOutOfBounds {
index: self.current,
len,
})?
.insert(choice);
let _ = queue_lock.get_mut_res(self.current)?.insert(choice);
true
}
None => false,
@@ -173,13 +159,13 @@ impl ValueIdentifiable for ChoiceQueue {
}
#[cfg(test)]
#[allow(clippy::unwrap_used)]
mod tests {
use super::*;
use crate::defines::LevelInt;
use crate::dynamic_data::{DynamicLibrary, PassChoice};
use crate::static_data::{AbilityIndex, Gender};
use std::sync::Arc;
#[test]
fn create_empty_queue() {
let queue = ChoiceQueue::new(Vec::new());