use std::time::Duration;
use serde::{
Deserialize,
Serialize,
};
use super::attempt_timeout_policy::AttemptTimeoutPolicy;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttemptTimeoutOption {
#[serde(with = "qubit_serde::serde::duration_millis")]
timeout: Duration,
policy: AttemptTimeoutPolicy,
}
impl AttemptTimeoutOption {
#[inline]
pub fn new(timeout: Duration, policy: AttemptTimeoutPolicy) -> Self {
Self { timeout, policy }
}
#[inline]
pub fn retry(timeout: Duration) -> Self {
Self::new(timeout, AttemptTimeoutPolicy::Retry)
}
#[inline]
pub fn abort(timeout: Duration) -> Self {
Self::new(timeout, AttemptTimeoutPolicy::Abort)
}
#[inline]
pub fn timeout(&self) -> Duration {
self.timeout
}
#[inline]
pub fn policy(&self) -> AttemptTimeoutPolicy {
self.policy
}
#[inline]
pub fn with_policy(self, policy: AttemptTimeoutPolicy) -> Self {
Self { policy, ..self }
}
pub fn validate(&self) -> Result<(), String> {
if self.timeout.is_zero() {
Err("attempt timeout must be greater than zero".to_string())
} else {
Ok(())
}
}
}