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::parser::error::*;
20
use crate::parser::ParseResult;
21
use crate::reader::Reader;
22

            
23
/// Parse a function
24
///
25

            
26
2155
pub fn parse(reader: &mut Reader) -> ParseResult<Function> {
27
2155
    let start = reader.cursor();
28
16521
    let function_name = reader.read_while(|c| c.is_alphanumeric() || c == '_' || c == '-');
29
2155
    match function_name.as_str() {
30
2155
        "newUuid" => Ok(Function::NewUuid),
31
2135
        _ => Err(ParseError::new(
32
2135
            start.pos,
33
2135
            true,
34
2135
            ParseErrorKind::Expecting {
35
2135
                value: "function".to_string(),
36
2135
            },
37
2135
        )),
38
    }
39
}
40

            
41
#[cfg(test)]
42
mod tests {
43
    use crate::reader::Pos;
44

            
45
    use super::*;
46

            
47
    #[test]
48
    fn test_exist() {
49
        let mut reader = Reader::new("newUuid");
50
        assert_eq!(parse(&mut reader).unwrap(), Function::NewUuid);
51
    }
52

            
53
    #[test]
54
    fn test_not_exist() {
55
        let mut reader = Reader::new("name");
56
        let err = parse(&mut reader).unwrap_err();
57
        assert_eq!(err.pos, Pos::new(1, 1));
58
        assert_eq!(err.recoverable, true);
59
    }
60
}