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
113
114
115
116
117
118
119
120
121
122
use crate::{Error, Result, StdError};
use std::any::TypeId;

/// Define the `wrap` function for Result types
pub trait Wrapper<T, E> {
    /// Wrap the error providing the ability to add more context
    fn wrap(self, msg: &str) -> Result<T>;

    /// Check if there is an error and the err is the given error type
    fn err_is<U>(&self) -> bool
    where
        U: StdError + 'static;

    /// Retry the given function when we have an error `max` number of times.
    fn retry<F>(self, max: usize, f: F) -> Result<T, E>
    where
        F: Fn(usize) -> Result<T, E>;

    /// Retry the given function when we have the concreate error `U` `max` number of times.
    fn retry_on<F>(self, max: usize, id: TypeId, f: F) -> Result<T, E>
    where
        F: Fn(usize) -> Result<T, E>;
}

impl<T, E> Wrapper<T, E> for Result<T, E>
where
    E: StdError + Send + Sync + 'static,
{
    fn wrap(self, msg: &str) -> Result<T> {
        match self {
            Err(err) => Error::wrap(err, msg),
            Ok(val) => Ok(val),
        }
    }

    fn err_is<U>(&self) -> bool
    where
        U: StdError + 'static,
    {
        match self {
            Ok(_) => false,
            Err(e) => (e as &(dyn StdError + 'static)).is::<U>(),
        }
    }

    fn retry<F>(self, max: usize, f: F) -> Result<T, E>
    where
        F: Fn(usize) -> Result<T, E>,
    {
        let mut retries = 0;
        let mut result = self;
        while retries < max && result.is_err() {
            retries += 1;
            result = f(retries);
        }
        result
    }

    fn retry_on<F>(self, max: usize, id: TypeId, f: F) -> Result<T, E>
    where
        F: Fn(usize) -> Result<T, E>,
    {
        let mut retries = 0;
        let mut result = self;
        while retries < max
            && match result {
                Ok(_) => false,
                Err(_) => TypeId::of::<E>() == id,
            }
        {
            retries += 1;
            result = f(retries);
        }
        result
    }
}

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

    use std::sync::Once;
    static INIT: Once = Once::new();
    pub fn initialize() {
        INIT.call_once(|| {
            std::env::set_var(crate::WITCHER_COLOR, "0");
            std::env::set_var("RUST_BACKTRACE", "0");
        });
    }

    fn retry() -> Result<()> {
        do_external_thing().retry(3, |_| do_external_thing()).wrap("Failed while attacking beast")
    }

    fn retry_on_concreate_error_type_using_err_is() -> Result<()> {
        let mut retries = 0;
        let mut result = do_external_thing();
        while retries < 3 && result.err_is::<std::io::Error>() {
            retries += 1;
            result = do_external_thing();
        }
        result.wrap(&format!("Failed while attacking beast: {}", retries))
    }

    fn retry_on_concreate_error_type() -> Result<()> {
        do_external_thing().retry_on(3, TypeId::of::<std::io::Error>(), |_| do_external_thing()).wrap("Failed while attacking beast")
    }

    fn do_external_thing() -> std::io::Result<()> {
        Err(std::io::Error::new(std::io::ErrorKind::Other, "Oh no, we missed!"))
    }

    #[test]
    fn test_retry_on() {
        initialize();
        assert_eq!("Failed while attacking beast", retry().unwrap_err().to_string());
        assert_eq!("Failed while attacking beast", retry_on_concreate_error_type().unwrap_err().to_string());
        assert_eq!("Failed while attacking beast: 3", retry_on_concreate_error_type_using_err_is().unwrap_err().to_string());
    }
}