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::Function;
19
use crate::parser::{ParseError, ParseErrorKind, ParseResult};
20
use crate::reader::Reader;
21

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

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

            
44
    use super::*;
45

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

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