use std::fmt;
use crate::{
suppliers::supplier_once::SupplierOnce,
tasks::callable::BoxCallable,
};
pub trait Runnable<E> {
fn run(self) -> Result<(), E>;
fn into_box(self) -> BoxRunnable<E>
where
Self: Sized + 'static,
{
BoxRunnable::new(move || self.run())
}
fn into_fn(self) -> impl FnOnce() -> Result<(), E>
where
Self: Sized + 'static,
{
move || self.run()
}
fn to_box(&self) -> BoxRunnable<E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_box()
}
fn to_fn(&self) -> impl FnOnce() -> Result<(), E>
where
Self: Clone + Sized + 'static,
{
self.clone().into_fn()
}
fn into_callable(self) -> BoxCallable<(), E>
where
Self: Sized + 'static,
{
BoxCallable::new(move || self.run())
}
}
pub struct BoxRunnable<E> {
function: Box<dyn FnOnce() -> Result<(), E>>,
name: Option<String>,
}
impl<E> BoxRunnable<E> {
#[inline]
pub fn new<F>(function: F) -> Self
where
F: FnOnce() -> Result<(), E> + 'static,
{
Self {
function: Box::new(function),
name: None,
}
}
#[inline]
pub fn new_with_name<F>(name: &str, function: F) -> Self
where
F: FnOnce() -> Result<(), E> + 'static,
{
Self {
function: Box::new(function),
name: Some(name.to_string()),
}
}
#[inline]
pub fn new_with_optional_name<F>(function: F, name: Option<String>) -> Self
where
F: FnOnce() -> Result<(), E> + 'static,
{
Self {
function: Box::new(function),
name,
}
}
#[inline]
pub fn from_supplier<S>(supplier: S) -> Self
where
S: SupplierOnce<Result<(), E>> + 'static,
{
Self::new(move || supplier.get())
}
#[inline]
pub fn name(&self) -> Option<&str> {
self.name.as_deref()
}
#[inline]
pub fn set_name(&mut self, name: &str) {
if self.name.as_deref() != Some(name) {
self.name = Some(name.to_owned());
}
}
#[inline]
pub fn clear_name(&mut self) {
self.name = None;
}
#[inline]
pub fn and_then<N>(self, next: N) -> BoxRunnable<E>
where
N: Runnable<E> + 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
BoxRunnable::new_with_optional_name(
move || {
function()?;
next.run()
},
name,
)
}
#[inline]
pub fn then_callable<R, C>(self, callable: C) -> BoxCallable<R, E>
where
C: crate::tasks::callable::Callable<R, E> + 'static,
R: 'static,
E: 'static,
{
let name = self.name;
let function = self.function;
BoxCallable::new_with_optional_name(
move || {
function()?;
callable.call()
},
name,
)
}
}
impl<E> Runnable<E> for BoxRunnable<E> {
#[inline]
fn run(self) -> Result<(), E> {
(self.function)()
}
#[inline]
fn into_box(self) -> BoxRunnable<E> {
self
}
#[inline]
fn into_fn(self) -> impl FnOnce() -> Result<(), E> {
self.function
}
#[inline]
fn into_callable(self) -> BoxCallable<(), E>
where
Self: Sized + 'static,
{
let name = self.name;
let function = self.function;
BoxCallable::new_with_optional_name(function, name)
}
}
impl<E> SupplierOnce<Result<(), E>> for BoxRunnable<E> {
#[inline]
fn get(self) -> Result<(), E> {
self.run()
}
}
impl<F, E> Runnable<E> for F
where
F: FnOnce() -> Result<(), E>,
{
#[inline]
fn run(self) -> Result<(), E> {
self()
}
#[inline]
fn into_box(self) -> BoxRunnable<E>
where
Self: Sized + 'static,
{
BoxRunnable::new(self)
}
#[inline]
fn into_fn(self) -> impl FnOnce() -> Result<(), E>
where
Self: Sized + 'static,
{
self
}
}
impl<E> fmt::Debug for BoxRunnable<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("BoxRunnable")
.field("name", &self.name)
.field("function", &"<function>")
.finish()
}
}
impl<E> fmt::Display for BoxRunnable<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match &self.name {
Some(name) => write!(f, "BoxRunnable({name})"),
None => write!(f, "BoxRunnable"),
}
}
}