use crate::double_checked::{
execution_result::ExecutionResult,
executor_error::ExecutorError,
};
pub struct ExecutionContext<T, E>
where
E: std::fmt::Display,
{
result: ExecutionResult<T, E>,
}
impl<T, E> ExecutionContext<T, E>
where
E: std::fmt::Display,
{
#[inline]
pub(super) fn new(result: ExecutionResult<T, E>) -> Self {
Self { result }
}
#[inline]
pub fn get_result(self) -> ExecutionResult<T, E> {
self.result
}
#[inline]
pub fn peek_result(&self) -> &ExecutionResult<T, E> {
&self.result
}
#[inline]
pub fn is_success(&self) -> bool {
self.result.is_success()
}
}
impl<E> ExecutionContext<(), E>
where
E: std::fmt::Display,
{
#[inline]
pub fn finish(self) -> bool {
let result = self.get_result();
result.is_success()
}
#[inline]
pub fn try_finish(self) -> Result<bool, ExecutorError<E>> {
match self.get_result() {
ExecutionResult::Success(()) => Ok(true),
ExecutionResult::ConditionNotMet => Ok(false),
ExecutionResult::Failed(error) => Err(error),
}
}
}