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
use crate::ast::*;
19
use crate::combinator::choice;
20
use crate::parser::multiline::multiline_string;
21
use crate::parser::number::number;
22
use crate::parser::primitives::*;
23
use crate::parser::string::*;
24
use crate::parser::{ParseError, ParseErrorKind, ParseResult};
25
use crate::reader::Reader;
26

            
27
use super::placeholder;
28

            
29
15920
pub fn predicate_value(reader: &mut Reader) -> ParseResult<PredicateValue> {
30
15920
    choice(
31
15920
        &[
32
19104
            |p1| match null(p1) {
33
110
                Ok(()) => Ok(PredicateValue::Null),
34
15810
                Err(e) => Err(e),
35
19104
            },
36
19082
            |p1| match boolean(p1) {
37
240
                Ok(value) => Ok(PredicateValue::Bool(value)),
38
15570
                Err(e) => Err(e),
39
19082
            },
40
19034
            |p1| match number(p1) {
41
3880
                Ok(value) => Ok(PredicateValue::Number(value)),
42
11690
                Err(e) => Err(e),
43
19034
            },
44
18258
            |p1| match file(p1) {
45
60
                Ok(value) => Ok(PredicateValue::File(value)),
46
11630
                Err(e) => Err(e),
47
18258
            },
48
18246
            |p1| match hex(p1) {
49
925
                Ok(value) => Ok(PredicateValue::Hex(value)),
50
10705
                Err(e) => Err(e),
51
18246
            },
52
18061
            |p1| match base64(p1) {
53
35
                Ok(value) => Ok(PredicateValue::Base64(value)),
54
10670
                Err(e) => Err(e),
55
18061
            },
56
18054
            |p1| match placeholder::parse(p1) {
57
455
                Ok(value) => Ok(PredicateValue::Placeholder(value)),
58
10215
                Err(e) => Err(e),
59
18054
            },
60
17963
            |p1| match quoted_template(p1) {
61
9780
                Ok(value) => Ok(PredicateValue::String(value)),
62
435
                Err(e) => Err(e),
63
17963
            },
64
16005
            |p1| match multiline_string(p1) {
65
175
                Ok(value) => Ok(PredicateValue::MultilineString(value)),
66
250
                Err(e) => Err(e),
67
16005
            },
68
15970
            |p1| match backtick_template(p1) {
69
40
                Ok(value) => Ok(PredicateValue::String(value)),
70
210
                Err(e) => Err(e),
71
15970
            },
72
15962
            |p1| match regex(p1) {
73
200
                Ok(value) => Ok(PredicateValue::Regex(value)),
74
10
                Err(e) => Err(e),
75
15962
            },
76
15920
        ],
77
15920
        reader,
78
15920
    )
79
15924
    .map_err(|e| {
80
20
        let kind = if e.recoverable {
81
5
            ParseErrorKind::PredicateValue
82
        } else {
83
15
            e.kind
84
        };
85
20
        ParseError::new(e.pos, false, kind)
86
15924
    })
87
}
88

            
89
#[cfg(test)]
90
mod tests {
91

            
92
    use super::*;
93
    use crate::parser::ParseErrorKind;
94
    use crate::reader::Pos;
95

            
96
    #[test]
97
    fn test_predicate_value() {
98
        let mut reader = Reader::new("true");
99
        assert_eq!(
100
            predicate_value(&mut reader).unwrap(),
101
            PredicateValue::Bool(true)
102
        );
103

            
104
        let mut reader = Reader::new("1");
105
        assert_eq!(
106
            predicate_value(&mut reader).unwrap(),
107
            PredicateValue::Number(Number::Integer(1))
108
        );
109

            
110
        let mut reader = Reader::new("1.1");
111
        assert_eq!(
112
            predicate_value(&mut reader).unwrap(),
113
            PredicateValue::Number(Number::Float(Float {
114
                value: 1.1,
115
                encoded: "1.1".to_string(),
116
            }))
117
        );
118
    }
119

            
120
    #[test]
121
    fn test_predicate_value_error() {
122
        let mut reader = Reader::new("xx");
123
        let error = predicate_value(&mut reader).err().unwrap();
124
        assert_eq!(error.pos, Pos { line: 1, column: 1 });
125
        assert_eq!(error.kind, ParseErrorKind::PredicateValue);
126
        assert!(!error.recoverable);
127
    }
128

            
129
    #[test]
130
    fn test_predicate_value_error_missing_quote() {
131
        let mut reader = Reader::new("\"not_found");
132
        let error = predicate_value(&mut reader).err().unwrap();
133
        assert_eq!(
134
            error.pos,
135
            Pos {
136
                line: 1,
137
                column: 11
138
            }
139
        );
140
        assert_eq!(
141
            error.kind,
142
            ParseErrorKind::Expecting {
143
                value: "\"".to_string()
144
            }
145
        );
146
        assert!(!error.recoverable);
147
    }
148
}