1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
//! FallbackTo combinator for alternative effects on error.
use crate::effect::Effect;
/// Tries an alternative effect on any error.
///
/// Zero-cost: no heap allocation. Stores only the primary and
/// alternative effects.
///
/// # Examples
///
/// ```rust,ignore
/// use stillwater::effect::prelude::*;
///
/// let data = fetch_primary()
/// .fallback_to(fetch_secondary());
/// ```
pub struct FallbackTo<E1, E2> {
pub(crate) primary: E1,
pub(crate) alternative: E2,
}
impl<E1, E2> std::fmt::Debug for FallbackTo<E1, E2> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("FallbackTo")
.field("primary", &"<effect>")
.field("alternative", &"<effect>")
.finish()
}
}
impl<E1, E2> FallbackTo<E1, E2> {
/// Creates a new `FallbackTo` combinator.
///
/// # Parameters
/// - `primary`: The primary effect to try first
/// - `alternative`: The alternative effect to try if the primary fails
pub fn new(primary: E1, alternative: E2) -> Self {
Self {
primary,
alternative,
}
}
}
impl<E1, E2> Effect for FallbackTo<E1, E2>
where
E1: Effect,
E2: Effect<Output = E1::Output, Error = E1::Error, Env = E1::Env>,
{
type Output = E1::Output;
type Error = E1::Error;
type Env = E1::Env;
async fn run(self, env: &Self::Env) -> Result<Self::Output, Self::Error> {
match self.primary.run(env).await {
Ok(value) => Ok(value),
Err(_) => self.alternative.run(env).await,
}
}
}