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::PredicateValue;
19
use crate::combinator::choice;
20
use crate::parser::multiline::multiline_string;
21
use crate::parser::number::number;
22
use crate::parser::primitives::{base64, boolean, file, hex, null, regex};
23
use crate::parser::string::{backtick_template, quoted_template};
24
use crate::parser::{ParseError, ParseErrorKind, ParseResult};
25
use crate::reader::Reader;
26

            
27
use super::placeholder;
28

            
29
16010
pub fn predicate_value(reader: &mut Reader) -> ParseResult<PredicateValue> {
30
16010
    choice(
31
16010
        &[
32
19212
            |p1| match null(p1) {
33
110
                Ok(()) => Ok(PredicateValue::Null),
34
15900
                Err(e) => Err(e),
35
19212
            },
36
19190
            |p1| match boolean(p1) {
37
240
                Ok(value) => Ok(PredicateValue::Bool(value)),
38
15660
                Err(e) => Err(e),
39
19190
            },
40
19142
            |p1| match number(p1) {
41
3905
                Ok(value) => Ok(PredicateValue::Number(value)),
42
11755
                Err(e) => Err(e),
43
19142
            },
44
18361
            |p1| match file(p1) {
45
60
                Ok(value) => Ok(PredicateValue::File(value)),
46
11695
                Err(e) => Err(e),
47
18361
            },
48
18349
            |p1| match hex(p1) {
49
925
                Ok(value) => Ok(PredicateValue::Hex(value)),
50
10770
                Err(e) => Err(e),
51
18349
            },
52
18164
            |p1| match base64(p1) {
53
35
                Ok(value) => Ok(PredicateValue::Base64(value)),
54
10735
                Err(e) => Err(e),
55
18164
            },
56
18157
            |p1| match placeholder::parse(p1) {
57
455
                Ok(value) => Ok(PredicateValue::Placeholder(value)),
58
10280
                Err(e) => Err(e),
59
18157
            },
60
18066
            |p1| match quoted_template(p1) {
61
9835
                Ok(value) => Ok(PredicateValue::String(value)),
62
445
                Err(e) => Err(e),
63
18066
            },
64
16097
            |p1| match multiline_string(p1) {
65
175
                Ok(value) => Ok(PredicateValue::MultilineString(value)),
66
260
                Err(e) => Err(e),
67
16097
            },
68
16062
            |p1| match backtick_template(p1) {
69
40
                Ok(value) => Ok(PredicateValue::String(value)),
70
220
                Err(e) => Err(e),
71
16062
            },
72
16054
            |p1| match regex(p1) {
73
210
                Ok(value) => Ok(PredicateValue::Regex(value)),
74
10
                Err(e) => Err(e),
75
16054
            },
76
16010
        ],
77
16010
        reader,
78
16010
    )
79
16014
    .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
16014
    })
87
}
88

            
89
#[cfg(test)]
90
mod tests {
91
    use super::*;
92
    use crate::ast::{Float, Number, I64};
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(I64::new(1, "1".to_string())))
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
}