use std::error::Error;
use std::time::Duration;
#[derive(Debug)]
pub struct RetryConfig<E> {
pub max_attempts: usize,
pub delay: Duration,
pub retry_condition: Option<fn(&E) -> bool>,
}
impl<E> Default for RetryConfig<E> {
fn default() -> Self {
RetryConfig {
max_attempts: 3,
delay: Duration::from_secs(2),
retry_condition: None,
}
}
}
impl<E> RetryConfig<E> {
pub fn new(max_attempts: usize, delay: Duration) -> Self {
RetryConfig {
max_attempts,
delay,
retry_condition: None,
}
}
pub fn with_retry_condition(mut self, retry_condition: fn(&E) -> bool) -> Self {
self.retry_condition = Some(retry_condition);
self
}
}
#[derive(Debug)]
pub struct ExecConfig<T> {
pub timeout_duration: Duration,
pub fallback: Option<fn() -> Result<T, Box<dyn Error>>>,
}
impl<T> ExecConfig<T>
where
T: Clone,
{
pub fn new(timeout_duration: Duration) -> Self {
ExecConfig {
timeout_duration,
fallback: None,
}
}
pub fn with_fallback(&mut self, fallback: fn() -> Result<T, Box<dyn Error>>) {
self.fallback = Some(fallback);
}
}