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

            
19
use crate::parser::error::*;
20
use crate::parser::number::natural;
21
use crate::parser::ParseResult;
22
use crate::reader::Reader;
23
use crate::typing::{Duration, DurationUnit};
24
use std::str::FromStr;
25

            
26
205
pub fn duration(reader: &mut Reader) -> ParseResult<Duration> {
27
205
    let value = natural(reader)?;
28
170
    let unit = duration_unit(reader)?;
29
165
    Ok(Duration::new(value, unit))
30
}
31

            
32
170
fn duration_unit(reader: &mut Reader) -> ParseResult<Option<DurationUnit>> {
33
170
    let pos = reader.cursor().pos;
34
459
    let s = reader.read_while(|c| c.is_ascii_alphabetic());
35
170
    if s.is_empty() {
36
30
        Ok(None)
37
    } else {
38
140
        match DurationUnit::from_str(&s) {
39
135
            Ok(unit) => Ok(Some(unit)),
40
5
            Err(_) => Err(ParseError {
41
5
                pos,
42
5
                kind: ParseErrorKind::InvalidDurationUnit(s),
43
5
                recoverable: false,
44
5
            }),
45
        }
46
    }
47
}
48

            
49
#[cfg(test)]
50
mod tests {
51
    use super::*;
52
    use crate::reader::Pos;
53

            
54
    use crate::typing::DurationUnit;
55

            
56
    #[test]
57
    fn test_duration_unit() {
58
        let mut reader = Reader::new("");
59
        assert!(duration_unit(&mut reader).unwrap().is_none());
60
        let mut reader = Reader::new("\n");
61
        assert!(duration_unit(&mut reader).unwrap().is_none());
62
        let mut reader = Reader::new("s\n");
63
        assert_eq!(
64
            duration_unit(&mut reader).unwrap().unwrap(),
65
            DurationUnit::Second
66
        );
67
        let mut reader = Reader::new("ms\n");
68
        assert_eq!(
69
            duration_unit(&mut reader).unwrap().unwrap(),
70
            DurationUnit::MilliSecond
71
        );
72
        let mut reader = Reader::new("m\n");
73
        assert_eq!(
74
            duration_unit(&mut reader).unwrap().unwrap(),
75
            DurationUnit::Minute
76
        );
77
    }
78

            
79
    #[test]
80
    fn test_duration_unit_error() {
81
        let mut reader = Reader::new("mms\n");
82
        let error = duration_unit(&mut reader).unwrap_err();
83
        assert_eq!(error.pos, Pos::new(1, 1));
84
        assert_eq!(
85
            error.kind,
86
            ParseErrorKind::InvalidDurationUnit("mms".to_string())
87
        );
88
    }
89

            
90
    #[test]
91
    fn test_duration() {
92
        let mut reader = Reader::new("10");
93
        assert_eq!(duration(&mut reader).unwrap(), Duration::new(10, None));
94
        let mut reader = Reader::new("10s");
95
        assert_eq!(
96
            duration(&mut reader).unwrap(),
97
            Duration::new(10, Some(DurationUnit::Second))
98
        );
99
        let mut reader = Reader::new("10000ms");
100
        assert_eq!(
101
            duration(&mut reader).unwrap(),
102
            Duration::new(10000, Some(DurationUnit::MilliSecond))
103
        );
104
    }
105

            
106
    #[test]
107
    fn test_duration_error() {
108
        let mut reader = Reader::new("10mms\n");
109
        let error = duration(&mut reader).unwrap_err();
110
        assert_eq!(error.pos, Pos::new(1, 3));
111
        assert_eq!(
112
            error.kind,
113
            ParseErrorKind::InvalidDurationUnit("mms".to_string())
114
        );
115
    }
116
}