1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2026 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
use std::str::FromStr;
19

            
20
use crate::ast::Duration;
21
use crate::reader::Reader;
22
use crate::types::DurationUnit;
23

            
24
use super::ParseResult;
25
use super::error::{ParseError, ParseErrorKind};
26
use super::number::natural;
27

            
28
680
pub fn duration(reader: &mut Reader) -> ParseResult<Duration> {
29
680
    let value = natural(reader)?;
30
612
    let unit = duration_unit(reader)?;
31
608
    Ok(Duration::new(value, unit))
32
}
33

            
34
612
fn duration_unit(reader: &mut Reader) -> ParseResult<Option<DurationUnit>> {
35
612
    let pos = reader.cursor().pos;
36
1688
    let s = reader.read_while(|c| c.is_ascii_alphabetic());
37
612
    if s.is_empty() {
38
24
        Ok(None)
39
    } else {
40
588
        match DurationUnit::from_str(&s) {
41
584
            Ok(unit) => Ok(Some(unit)),
42
4
            Err(_) => Err(ParseError {
43
4
                pos,
44
4
                kind: ParseErrorKind::InvalidDurationUnit(s),
45
4
                recoverable: false,
46
4
            }),
47
        }
48
    }
49
}
50

            
51
#[cfg(test)]
52
mod tests {
53
    use super::*;
54
    use crate::ast::U64;
55
    use crate::reader::Pos;
56
    use crate::types::{DurationUnit, ToSource};
57

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

            
86
    #[test]
87
    fn test_duration_unit_error() {
88
        let mut reader = Reader::new("mms\n");
89
        let error = duration_unit(&mut reader).unwrap_err();
90
        assert_eq!(error.pos, Pos::new(1, 1));
91
        assert_eq!(
92
            error.kind,
93
            ParseErrorKind::InvalidDurationUnit("mms".to_string())
94
        );
95
    }
96

            
97
    #[test]
98
    fn test_duration() {
99
        let mut reader = Reader::new("10");
100
        assert_eq!(
101
            duration(&mut reader).unwrap(),
102
            Duration::new(U64::new(10, "10".to_source()), None)
103
        );
104
        let mut reader = Reader::new("10s");
105
        assert_eq!(
106
            duration(&mut reader).unwrap(),
107
            Duration::new(U64::new(10, "10".to_source()), Some(DurationUnit::Second))
108
        );
109
        let mut reader = Reader::new("10000ms");
110
        assert_eq!(
111
            duration(&mut reader).unwrap(),
112
            Duration::new(
113
                U64::new(10000, "10000".to_source()),
114
                Some(DurationUnit::MilliSecond)
115
            )
116
        );
117
        let mut reader = Reader::new("1m");
118
        assert_eq!(
119
            duration(&mut reader).unwrap(),
120
            Duration::new(U64::new(1, "1".to_source()), Some(DurationUnit::Minute))
121
        );
122
        let mut reader = Reader::new("1h");
123
        assert_eq!(
124
            duration(&mut reader).unwrap(),
125
            Duration::new(U64::new(1, "1".to_source()), Some(DurationUnit::Hour))
126
        );
127
    }
128

            
129
    #[test]
130
    fn test_duration_error() {
131
        let mut reader = Reader::new("10mms\n");
132
        let error = duration(&mut reader).unwrap_err();
133
        assert_eq!(error.pos, Pos::new(1, 3));
134
        assert_eq!(
135
            error.kind,
136
            ParseErrorKind::InvalidDurationUnit("mms".to_string())
137
        );
138
    }
139
}