web_sys_ec/
wait_options.rs

1use std::time::Duration;
2
3/// Options for waiting.
4///
5/// You don't need to create this struct directly as it offers
6/// multiple `From` implementations to convert from different types
7/// and [`Wait`](crate::Wait()) will do it for you.
8#[derive(Debug)]
9pub struct WaitOptions {
10    duration: Duration,
11    poll_frecuency: Duration,
12}
13
14impl Default for WaitOptions {
15    fn default() -> Self {
16        Self {
17            duration: Duration::from_secs(10),
18            poll_frecuency: Duration::from_millis(20),
19        }
20    }
21}
22
23impl WaitOptions {
24    /// Create a new `WaitOptions` with the given duration and poll frequency.
25    pub fn new(duration: Duration, poll_frecuency: Duration) -> Self {
26        Self {
27            duration,
28            poll_frecuency,
29        }
30    }
31
32    pub fn duration(&self) -> Duration {
33        self.duration
34    }
35
36    pub fn poll_frecuency(&self) -> Duration {
37        self.poll_frecuency
38    }
39
40    pub fn with_duration(mut self, duration: Duration) -> Self {
41        self.duration = duration;
42        self
43    }
44
45    pub fn with_poll_frecuency(mut self, poll_frecuency: Duration) -> Self {
46        self.poll_frecuency = poll_frecuency;
47        self
48    }
49}
50
51impl From<u64> for WaitOptions {
52    fn from(seconds: u64) -> Self {
53        let millis = seconds * 1000;
54        Self {
55            duration: Duration::from_millis(millis),
56            poll_frecuency: Duration::from_millis(millis / 50),
57        }
58    }
59}
60
61impl From<(u64, u64)> for WaitOptions {
62    fn from((seconds, poll_frecuency): (u64, u64)) -> Self {
63        Self {
64            duration: Duration::from_secs(seconds),
65            poll_frecuency: Duration::from_secs(poll_frecuency),
66        }
67    }
68}
69
70impl From<f64> for WaitOptions {
71    fn from(seconds: f64) -> Self {
72        let millis = (seconds * 1000.0).round() as u64;
73        Self {
74            duration: Duration::from_millis(millis),
75            poll_frecuency: Duration::from_millis(millis / 50),
76        }
77    }
78}
79
80impl From<(f64, f64)> for WaitOptions {
81    fn from((seconds, poll_frecuency): (f64, f64)) -> Self {
82        let millis = (seconds * 1000.0).round() as u64;
83        let poll_millis = (poll_frecuency * 1000.0).round() as u64;
84        Self {
85            duration: Duration::from_millis(millis),
86            poll_frecuency: Duration::from_millis(poll_millis),
87        }
88    }
89}
90
91impl From<Duration> for WaitOptions {
92    fn from(duration: Duration) -> Self {
93        Self {
94            duration,
95            poll_frecuency: Duration::from_millis((duration.as_millis() / 20).try_into().unwrap()),
96        }
97    }
98}
99
100impl From<(Duration, Duration)> for WaitOptions {
101    fn from((duration, poll_frecuency): (Duration, std::time::Duration)) -> Self {
102        Self {
103            duration,
104            poll_frecuency,
105        }
106    }
107}
108
109impl From<(u64, f64)> for WaitOptions {
110    fn from((duration, poll_frecuency): (u64, f64)) -> Self {
111        Self {
112            duration: Duration::from_millis(duration),
113            poll_frecuency: Duration::from_millis((poll_frecuency * 1000.0).round() as u64),
114        }
115    }
116}
117
118impl From<(f64, u64)> for WaitOptions {
119    fn from((duration, poll_frecuency): (f64, u64)) -> Self {
120        let millis = (duration * 1000.0).round() as u64;
121        Self {
122            duration: Duration::from_millis(millis),
123            poll_frecuency: Duration::from_millis(poll_frecuency),
124        }
125    }
126}
127
128impl From<(Duration, u64)> for WaitOptions {
129    fn from((duration, poll_frecuency): (Duration, u64)) -> Self {
130        Self {
131            duration,
132            poll_frecuency: Duration::from_millis(poll_frecuency),
133        }
134    }
135}
136
137impl From<(u64, Duration)> for WaitOptions {
138    fn from((duration, poll_frecuency): (u64, Duration)) -> Self {
139        Self {
140            duration: Duration::from_millis(duration),
141            poll_frecuency,
142        }
143    }
144}
145
146impl From<(f64, Duration)> for WaitOptions {
147    fn from((duration, poll_frecuency): (f64, Duration)) -> Self {
148        let millis = (duration * 1000.0).round() as u64;
149        Self {
150            duration: Duration::from_millis(millis),
151            poll_frecuency,
152        }
153    }
154}
155
156impl From<(Duration, f64)> for WaitOptions {
157    fn from((duration, poll_frecuency): (Duration, f64)) -> Self {
158        Self {
159            duration,
160            poll_frecuency: Duration::from_millis((poll_frecuency * 1000.0).round() as u64),
161        }
162    }
163}