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
use super::attribute::*;
use crate::{
    ast::*,
    parser::{combinator::*, identifier::*},
};

/// 333 unique_clause = UNIQUE [unique_rule] `;` { [unique_rule] `;` } .
pub fn unique_clause(input: &str) -> ParseResult<UniqueClause> {
    tuple((tag("UNIQUE"), many0(tuple((unique_rule, char(';'))))))
        .map(|(_unique, seq)| UniqueClause {
            rules: seq.into_iter().map(|(rule, _semicolon)| rule).collect(),
        })
        .parse(input)
}

/// 334 unique_rule = \[ [rule_label_id] `:` \] [referenced_attribute] { `,` [referenced_attribute] } .
pub fn unique_rule(input: &str) -> ParseResult<UniqueRule> {
    tuple((
        opt(tuple((rule_label_id, char(':')))),
        comma_separated(referenced_attribute),
    ))
    .map(|(opt_id, attributes)| UniqueRule {
        name: opt_id.map(|(name, _colon)| name),
        attributes,
    })
    .parse(input)
}

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

    #[test]
    fn unique_clause() {
        let (residual, (c, _remarks)) = super::unique_clause(
            r#"
            UNIQUE
              ur1 : revision_identifier, drawing_identifier;
            "#
            .trim(),
        )
        .finish()
        .unwrap();
        assert_eq!(residual, "");
        assert_eq!(c.rules.len(), 1);
    }

    #[test]
    fn unique_clause_termination() {
        let (residual, (c, _remarks)) = super::unique_clause(
            r#"
            UNIQUE
              ur1 : revision_identifier1, drawing_identifier1;
              ur2 : revision_identifier2, drawing_identifier2;
            END_ENTITY;
            "#
            .trim(),
        )
        .finish()
        .unwrap();
        assert_eq!(residual, "END_ENTITY;");
        assert_eq!(c.rules.len(), 2);
    }
}