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 crate::ast::U64;
20
use core::fmt;
21
use std::str::FromStr;
22

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

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

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

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

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

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

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

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

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

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

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