1use super::{BasicGenerator, Generator};
2use crate::cbor_utils::cbor_map;
3use std::time::Duration;
4
5pub struct DurationGenerator {
10 min_nanos: u64,
11 max_nanos: u64,
12}
13
14impl DurationGenerator {
15 pub fn min_value(mut self, min: Duration) -> Self {
17 self.min_nanos = duration_to_nanos(min);
18 self
19 }
20
21 pub fn max_value(mut self, max: Duration) -> Self {
23 self.max_nanos = duration_to_nanos(max);
24 self
25 }
26
27 fn build_schema(&self) -> ciborium::Value {
28 assert!(
29 self.min_nanos <= self.max_nanos,
30 "Cannot have max_value < min_value"
31 );
32 cbor_map! {
33 "type" => "integer",
34 "min_value" => self.min_nanos,
35 "max_value" => self.max_nanos
36 }
37 }
38}
39
40impl Generator<Duration> for DurationGenerator {
41 fn as_basic(&self) -> Option<BasicGenerator<'_, Duration>> {
42 Some(BasicGenerator::new(self.build_schema(), |raw| {
43 let nanos: u64 = super::deserialize_value(raw);
44 Duration::from_nanos(nanos)
45 }))
46 }
47}
48
49pub fn durations() -> DurationGenerator {
67 DurationGenerator {
68 min_nanos: 0,
69 max_nanos: u64::MAX,
70 }
71}
72
73fn duration_to_nanos(d: Duration) -> u64 {
74 d.as_nanos().try_into().unwrap_or(u64::MAX)
75}