1
use super::{expr, ParseResult};
2
use crate::ast::Placeholder;
3
use crate::parser::primitives::{literal, try_literal, zero_or_more_spaces};
4
/*
5
 * Hurl (https://hurl.dev)
6
 * Copyright (C) 2024 Orange
7
 *
8
 * Licensed under the Apache License, Version 2.0 (the "License");
9
 * you may not use this file except in compliance with the License.
10
 * You may obtain a copy of the License at
11
 *
12
 *          http://www.apache.org/licenses/LICENSE-2.0
13
 *
14
 * Unless required by applicable law or agreed to in writing, software
15
 * distributed under the License is distributed on an "AS IS" BASIS,
16
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17
 * See the License for the specific language governing permissions and
18
 * limitations under the License.
19
 *
20
 */
21
use crate::reader::Reader;
22

            
23
/// Parse a placeholder {{ expr }}
24
103110
pub fn parse(reader: &mut Reader) -> ParseResult<Placeholder> {
25
103110
    try_literal("{{", reader)?;
26
1260
    let space0 = zero_or_more_spaces(reader)?;
27
1260
    let expr = expr::parse(reader)?;
28
1260
    let space1 = zero_or_more_spaces(reader)?;
29
1260
    literal("}}", reader)?;
30
1260
    Ok(Placeholder {
31
1260
        space0,
32
1260
        expr,
33
1260
        space1,
34
1260
    })
35
}
36

            
37
#[cfg(test)]
38
mod tests {
39
    use super::*;
40
    use crate::ast::{Expr, ExprKind, SourceInfo, Variable, Whitespace};
41
    use crate::{parser::ParseErrorKind, reader::Pos};
42

            
43
    #[test]
44
    fn test_ok() {
45
        let mut reader = Reader::new("{{ name}}");
46
        assert_eq!(
47
            parse(&mut reader).unwrap(),
48
            Placeholder {
49
                space0: Whitespace {
50
                    value: String::from(" "),
51
                    source_info: SourceInfo::new(Pos::new(1, 3), Pos::new(1, 4)),
52
                },
53
                expr: Expr {
54
                    kind: ExprKind::Variable(Variable {
55
                        name: "name".to_string(),
56
                        source_info: SourceInfo::new(Pos::new(1, 4), Pos::new(1, 8)),
57
                    }),
58
                    source_info: SourceInfo::new(Pos::new(1, 4), Pos::new(1, 8)),
59
                },
60
                space1: Whitespace {
61
                    value: String::new(),
62
                    source_info: SourceInfo::new(Pos::new(1, 8), Pos::new(1, 8)),
63
                },
64
            }
65
        );
66
    }
67

            
68
    #[test]
69
    fn test_error() {
70
        let mut reader = Reader::new("{{host>}}");
71
        let error = parse(&mut reader).err().unwrap();
72
        assert_eq!(error.pos, Pos { line: 1, column: 7 });
73
        assert_eq!(
74
            error.kind,
75
            ParseErrorKind::Expecting {
76
                value: String::from("}}")
77
            }
78
        );
79
        assert!(!error.recoverable);
80
    }
81

            
82
    #[test]
83
    fn test_error_eof() {
84
        let mut reader = Reader::new("{{host");
85
        let error = parse(&mut reader).err().unwrap();
86
        assert_eq!(error.pos, Pos { line: 1, column: 7 });
87
        assert_eq!(
88
            error.kind,
89
            ParseErrorKind::Expecting {
90
                value: String::from("}}")
91
            }
92
        );
93
        assert!(!error.recoverable);
94
    }
95
}