use super::error::Error as BehaviorError;
use crate::{ConstString, FAILURE_IF, ON_FAILURE, ON_HALTED, ON_SUCCESS, POST, SKIP_IF, SUCCESS_IF, WHILE};
use core::ops::{Deref, DerefMut};
#[derive(Default)]
pub struct Conditions {
pub(crate) pre: PreConditions,
pub(crate) post: PostConditions,
}
pub const PRE_CONDITIONS: [&str; 4] = [FAILURE_IF, SUCCESS_IF, SKIP_IF, WHILE];
#[derive(Default)]
pub struct PreConditions(pub(crate) Option<[Option<ConstString>; PRE_CONDITIONS.len()]>);
impl Deref for PreConditions {
type Target = Option<[Option<ConstString>; PRE_CONDITIONS.len()]>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for PreConditions {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl PreConditions {
#[must_use]
pub fn get(&self, name: &str) -> Option<&ConstString> {
if self.0.is_some() {
let op = (0..PRE_CONDITIONS.len()).find(|&i| PRE_CONDITIONS[i] == name);
op.and_then(|index| {
self.0
.as_ref()
.map_or_else(|| None, |array| array[index].as_ref())
})
} else {
None
}
}
pub fn set(&mut self, name: &str, script: &str) -> Result<(), BehaviorError> {
if self.0.is_none() {
self.0 = Some([None, None, None, None]);
}
let op = (0..PRE_CONDITIONS.len()).find(|&i| PRE_CONDITIONS[i] == name);
if let Some(index) = op {
self.0.as_mut().map_or_else(
|| Err(BehaviorError::UnableToSetCondition { value: name.into() }),
|array| {
array[index] = Some(script.into());
Ok(())
},
)
} else {
Err(BehaviorError::NoCondition { value: name.into() })
}
}
}
pub const POST_CONDITIONS: [&str; 4] = [ON_HALTED, ON_FAILURE, ON_SUCCESS, POST];
#[derive(Default)]
pub struct PostConditions(pub(crate) Option<[Option<ConstString>; POST_CONDITIONS.len()]>);
impl Deref for PostConditions {
type Target = Option<[Option<ConstString>; POST_CONDITIONS.len()]>;
fn deref(&self) -> &Self::Target {
&self.0
}
}
impl DerefMut for PostConditions {
fn deref_mut(&mut self) -> &mut Self::Target {
&mut self.0
}
}
impl PostConditions {
#[must_use]
pub fn get(&self, name: &str) -> Option<&ConstString> {
if self.0.is_some() {
let op = (0..POST_CONDITIONS.len()).find(|&i| POST_CONDITIONS[i] == name);
op.and_then(|index| {
self.0
.as_ref()
.map_or_else(|| None, |array| array[index].as_ref())
})
} else {
None
}
}
pub fn set(&mut self, name: &str, script: &str) -> Result<(), BehaviorError> {
if self.0.is_none() {
self.0 = Some([None, None, None, None]);
}
let op = (0..POST_CONDITIONS.len()).find(|&i| POST_CONDITIONS[i] == name);
if let Some(index) = op {
self.0.as_mut().map_or_else(
|| Err(BehaviorError::UnableToSetCondition { value: name.into() }),
|array| {
array[index] = Some(script.into());
Ok(())
},
)
} else {
Err(BehaviorError::NoCondition { value: name.into() })
}
}
}