1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2026 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::Function;
19
use crate::reader::Reader;
20

            
21
use super::ParseResult;
22
use super::error::{ParseError, ParseErrorKind};
23

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

            
42
#[cfg(test)]
43
mod tests {
44
    use super::*;
45
    use crate::reader::Pos;
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!(err.recoverable);
59
    }
60
}