1
/*
2
 * Hurl (https://hurl.dev)
3
 * Copyright (C) 2025 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::parser::{ParseError, ParseErrorKind, ParseResult};
20
use crate::reader::Reader;
21

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

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

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

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