use crate::Moment;
use async_trait::async_trait;
#[derive(Clone)]
pub struct If<C: Moment<Value = bool>, T: Moment, F: Moment> {
pub(crate) condition: C,
pub(crate) then: T,
pub(crate) otherwise: F,
}
impl<C: Moment<Value = bool>, T: Moment, F: Moment> If<C, T, F> {
pub fn new(condition: C, then: T, otherwise: F) -> Self {
Self {
condition,
then,
otherwise,
}
}
pub async fn to_then(self) -> Option<T> {
if self.condition.resolve().await {
Some(self.then)
} else {
None
}
}
pub async fn to_otherwise(self) -> Option<F> {
if !self.condition.resolve().await {
Some(self.otherwise)
} else {
None
}
}
}
#[async_trait]
impl<C, T, F> Moment for If<C, T, F>
where
C: Moment<Value = bool>,
T: Moment,
F: Moment
{
type Value = If<C, T::Value, F::Value>;
async fn resolve(self) -> Self::Value {
If::new(
self.condition,
self.then.resolve().await,
self.otherwise.resolve().await,
)
}
}