1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2024 Orange
4
 *
5
 * Licensed under the Apache License, Version 2.0 (the "License");
6
 * you may not use this file except in compliance with the License.
7
 * You may obtain a copy of the License at
8
 *
9
 *          http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 *
17
 */
18
//! Hurl common types.
19
use core::fmt;
20
use std::str::FromStr;
21

            
22
/// Represents a count operation, either finite or infinite.
23
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
24
pub enum Count {
25
    Finite(usize),
26
    Infinite,
27
}
28

            
29
/// Represent a duration
30
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
31
pub struct Duration {
32
    pub value: u64,
33
    pub unit: Option<DurationUnit>,
34
}
35

            
36
impl Duration {
37
180
    pub fn new(value: u64, unit: Option<DurationUnit>) -> Duration {
38
180
        Duration { value, unit }
39
    }
40
}
41

            
42
/// Represents a duration unit
43
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
44
pub enum DurationUnit {
45
    MilliSecond,
46
    Second,
47
    Minute,
48
}
49

            
50
impl fmt::Display for Count {
51
160
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
52
160
        match self {
53
150
            Count::Finite(n) => {
54
150
                write!(f, "{n}")
55
            }
56
            Count::Infinite => {
57
10
                write!(f, "-1")
58
            }
59
        }
60
    }
61
}
62

            
63
impl fmt::Display for Duration {
64
65
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65
65
        let unit = if let Some(value) = self.unit {
66
65
            value.to_string()
67
        } else {
68
            String::new()
69
        };
70
65
        write!(f, "{}{unit}", self.value)
71
    }
72
}
73

            
74
impl fmt::Display for DurationUnit {
75
160
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
76
160
        match self {
77
115
            DurationUnit::MilliSecond => write!(f, "ms"),
78
45
            DurationUnit::Second => write!(f, "s"),
79
            DurationUnit::Minute => write!(f, "m"),
80
        }
81
    }
82
}
83

            
84
impl FromStr for DurationUnit {
85
    type Err = String;
86

            
87
170
    fn from_str(s: &str) -> Result<Self, Self::Err> {
88
170
        match s {
89
170
            "ms" => Ok(DurationUnit::MilliSecond),
90
60
            "s" => Ok(DurationUnit::Second),
91
5
            "m" => Ok(DurationUnit::Minute),
92
5
            x => Err(format!("Invalid duration unit {x}")),
93
        }
94
    }
95
}
96

            
97
/// Represents bit rate.
98
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
99
pub struct BytesPerSec(pub u64);
100

            
101
impl fmt::Display for BytesPerSec {
102
10
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
103
10
        write!(f, "{}", self.0)
104
    }
105
}