1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
use super::super::{combinator::*, identifier::*};
use crate::ast::*;

/// 211 enumeration_items = `(` [enumeration_id] { `,` [enumeration_id] } `)` .
pub fn enumeration_items(input: &str) -> ParseResult<Vec<String>> {
    tuple((char('('), comma_separated(enumeration_id), char(')')))
        .map(|(_open, enums, _close)| enums)
        .parse(input)
}

/// 213 enumeration_type = \[ EXTENSIBLE \] ENUMERATION \[ ( OF [enumeration_items] ) | enumeration_extension \] .
pub fn enumeration_type(input: &str) -> ParseResult<Type> {
    // FIXME enumeration_extension
    tuple((
        opt(tag("EXTENSIBLE")),
        tag("ENUMERATION"),
        tag("OF"),
        enumeration_items,
    ))
    .map(|(extensiblility, _start, _of, items)| Type::Enumeration {
        extensibility: if extensiblility.is_some() {
            Extensibility::Extensible
        } else {
            Extensibility::None
        },
        items,
    })
    .parse(input)
}

#[cfg(test)]
mod tests {
    use nom::Finish;

    #[test]
    fn enumeration_type() {
        let (residual, (e, _remark)) =
            super::enumeration_type("ENUMERATION OF (up, down, left, right)")
                .finish()
                .unwrap();
        assert_eq!(residual, "");
        assert_eq!(
            e,
            super::Type::Enumeration {
                extensibility: super::Extensibility::None,
                items: vec![
                    "up".to_string(),
                    "down".to_string(),
                    "left".to_string(),
                    "right".to_string()
                ]
            }
        );
    }

    #[test]
    fn extensible() {
        let (residual, (e, _remark)) =
            super::enumeration_type("EXTENSIBLE ENUMERATION OF (up, down, left, right)")
                .finish()
                .unwrap();
        assert_eq!(residual, "");
        assert_eq!(
            e,
            super::Type::Enumeration {
                extensibility: super::Extensibility::Extensible,
                items: vec![
                    "up".to_string(),
                    "down".to_string(),
                    "left".to_string(),
                    "right".to_string()
                ]
            }
        );

        // GENERIC_ENTITY is only supported for SELECT
        assert!(super::enumeration_type(
            "EXTENSIBLE GENERIC_ENTITY ENUMERATION OF (up, down, left, right)"
        )
        .finish()
        .is_err());
    }
}