Skip to main content

ras_types/domain/
timing.rs

1use std::time::Duration;
2
3use serde::{Deserialize, Serialize};
4use thiserror::Error;
5
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(transparent)]
8pub struct ActionTimeout(Duration);
9
10#[derive(Debug, Error)]
11pub enum ActionTimeoutError {
12    #[error("timeout must be > 0 and finite")]
13    Invalid,
14}
15
16impl ActionTimeout {
17    pub fn new(d: Duration) -> Result<Self, ActionTimeoutError> {
18        if d.is_zero() {
19            return Err(ActionTimeoutError::Invalid);
20        }
21        Ok(Self(d))
22    }
23
24    #[must_use]
25    pub fn from_secs(s: u64) -> Self {
26        Self(Duration::from_secs(s.max(1)))
27    }
28
29    #[must_use]
30    pub fn as_duration(self) -> Duration {
31        self.0
32    }
33}
34
35impl Default for ActionTimeout {
36    fn default() -> Self {
37        Self(Duration::from_secs(180))
38    }
39}