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

            
23
/// Parse a placeholder {{ expr }}
24
102665
pub fn parse(reader: &mut Reader) -> ParseResult<Placeholder> {
25
102665
    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::{parser::ParseErrorKind, reader::Pos};
41

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

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

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