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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
//! Extension traits for `std::Result`.

/// Extension trait with useful methods for [`std::result::Result`].
///
/// [`std::result::Result`]: https://doc.rust-lang.org/std/result/enum.Result.html
pub trait ResultExt<T, E> {
    /// Combines `self` and another `Result`.
    ///
    /// If `self` is `Ok(s)` and `other` is `Ok(o)`, this method returns `Ok((s, o))`.
    /// Otherwise, if the `self` is `Ok(s)` and `other` is `Err(e)`, this method returns `Err(e)`.
    /// Otherwise, `self` is `Err(e)` and this method returns `Err(e)` (`other` is not taken into
    /// account, as in short circuit calculations).
    ///
    /// # Examples
    ///
    /// ```
    /// use stdext::prelude::*;
    ///
    /// let x = Ok(1);
    /// let y = Ok("hi");
    /// let z: Result<i32, &str> = Err("error");
    /// let z2: Result<i32, &str> = Err("other_error");
    ///
    /// assert_eq!(x.combine(y), Ok((1, "hi")));
    /// assert_eq!(x.combine(z), Err("error"));
    /// assert_eq!(z.combine(z2), Err("error"));
    /// ```
    fn combine<U>(self, other: Result<U, E>) -> Result<(T, U), E>;

    /// Combines `self` and another `Result` with function `f`.
    ///
    /// If `self` is `Ok(s)` and `other` is `Ok(o)`, this method returns `Ok(f(s, o))`.
    /// Otherwise, if the `self` is `Ok(s)` and `other` is `Err(e)`, this method returns `Err(e)`.
    /// Otherwise, `self` is `Err(e)` and this method returns `Err(e)` (`other` is not taken into
    /// account, as in short circuit calculations).
    ///
    /// # Examples
    ///
    /// ```
    /// use stdext::prelude::*;
    ///
    /// let x = Ok(1);
    /// let y = Ok(2);
    /// let z: Result<i32, &str> = Err("error");
    /// let z2: Result<i32, &str> = Err("other_error");
    ///
    /// assert_eq!(x.combine_with(y, |l, r| l + r), Ok(3));
    /// assert_eq!(x.combine_with(z, |l, r| l + r), Err("error"));
    /// assert_eq!(z.combine_with(z2, |l, r| l + r), Err("error"));
    /// ```
    ///
    /// [`zip_with`]: https://doc.rust-lang.org/std/Result/enum.Result.html#method.zip_with
    fn combine_with<U, F, R>(self, other: Result<U, E>, f: F) -> Result<R, E>
    where
        F: FnOnce(T, U) -> R;
}

impl<T, E> ResultExt<T, E> for Result<T, E> {
    fn combine<U>(self, other: Result<U, E>) -> Result<(T, U), E> {
        match (self, other) {
            (Ok(left), Ok(right)) => Ok((left, right)),
            (Ok(_), Err(err)) => Err(err),
            (Err(err), _) => Err(err),
        }
    }

    fn combine_with<U, F, R>(self, other: Result<U, E>, f: F) -> Result<R, E>
    where
        F: FnOnce(T, U) -> R,
    {
        self.combine(other).map(|(l, r)| f(l, r))
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn combine() {
        // Test vector of (left, right, expected) values.
        let test_vector = vec![
            (Ok(1), Ok(2), Ok((1, 2))),
            (Ok(1), Err("right"), Err("right")),
            (Err("left"), Ok(2), Err("left")),
            (Err("left"), Err("right"), Err("left")),
        ];

        for (left, right, expected) in test_vector {
            assert_eq!(left.combine(right), expected);
        }
    }

    #[test]
    fn combine_with() {
        fn f(l: i32, r: i32) -> i32 {
            l + r
        }

        // Test vector of (left, right, expected) values.
        let test_vector = vec![
            (Ok(1), Ok(2), Ok(3)),
            (Ok(1), Err("right"), Err("right")),
            (Err("left"), Ok(2), Err("left")),
            (Err("left"), Err("right"), Err("left")),
        ];

        for (left, right, expected) in test_vector {
            assert_eq!(left.combine_with(right, f), expected);
        }
    }
}