use crate::effect::writer::WriterEffect;
use crate::effect::Effect;
use crate::Semigroup;
pub struct TapTell<E, F> {
pub(crate) inner: E,
pub(crate) f: F,
}
impl<E, F> std::fmt::Debug for TapTell<E, F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("TapTell")
.field("inner", &"<effect>")
.field("f", &"<function>")
.finish()
}
}
impl<E, F, W2> Effect for TapTell<E, F>
where
E: WriterEffect,
E::Output: Clone + Send,
F: FnOnce(&E::Output) -> W2 + Send,
W2: Into<E::Writes>,
{
type Output = E::Output;
type Error = E::Error;
type Env = E::Env;
async fn run(self, env: &Self::Env) -> Result<Self::Output, Self::Error> {
self.inner.run(env).await
}
}
impl<E, F, W2> WriterEffect for TapTell<E, F>
where
E: WriterEffect,
E::Output: Clone + Send,
E::Writes: Semigroup,
F: FnOnce(&E::Output) -> W2 + Send,
W2: Into<E::Writes>,
{
type Writes = E::Writes;
async fn run_writer(
self,
env: &Self::Env,
) -> (Result<Self::Output, Self::Error>, Self::Writes) {
let (result, writes) = self.inner.run_writer(env).await;
match &result {
Ok(value) => {
let additional: E::Writes = (self.f)(value).into();
(result, writes.combine(additional))
}
Err(_) => (result, writes),
}
}
}