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
use crate::parser::grammar::value::Constness;
use crate::parser::grammar::{description, directive, field, name, object};
use crate::{Parser, SyntaxKind, TokenKind, T};

/// See: https://spec.graphql.org/October2021/#InterfaceTypeDefinition
///
/// *InterfaceTypeDefinition*:
///     Description? **interface** Name ImplementsInterface? Directives[Const]? FieldsDefinition?
pub(crate) fn interface_type_definition(p: &mut Parser) {
    let _g = p.start_node(SyntaxKind::INTERFACE_TYPE_DEFINITION);

    if let Some(TokenKind::StringValue) = p.peek() {
        description::description(p);
    }

    if let Some("interface") = p.peek_data().as_deref() {
        p.bump(SyntaxKind::interface_KW);
    }

    match p.peek() {
        Some(TokenKind::Name) => name::name(p),
        _ => p.err("expected a Name"),
    }

    if let Some("implements") = p.peek_data().as_deref() {
        object::implements_interfaces(p);
    }

    if let Some(T![@]) = p.peek() {
        directive::directives(p, Constness::Const);
    }

    if let Some(T!['{']) = p.peek() {
        field::fields_definition(p);
    }
}

/// See: https://spec.graphql.org/October2021/#InterfaceTypeExtension
///
/// *InterfaceTypeExtension*:
///     **extend** **interface** Name ImplementsInterface? Directives[Const]? FieldsDefinition
///     **extend** **interface** Name ImplementsInterface? Directives[Const]
///     **extend** **interface** Name ImplementsInterface
pub(crate) fn interface_type_extension(p: &mut Parser) {
    let _g = p.start_node(SyntaxKind::INTERFACE_TYPE_EXTENSION);
    p.bump(SyntaxKind::extend_KW);
    p.bump(SyntaxKind::interface_KW);

    let mut meets_requirements = false;

    match p.peek() {
        Some(TokenKind::Name) => name::name(p),
        _ => p.err("expected a Name"),
    }

    if let Some("implements") = p.peek_data().as_deref() {
        meets_requirements = true;
        object::implements_interfaces(p);
    }

    if let Some(T![@]) = p.peek() {
        meets_requirements = true;
        directive::directives(p, Constness::Const);
    }

    if let Some(T!['{']) = p.peek() {
        meets_requirements = true;
        field::fields_definition(p);
    }

    if !meets_requirements {
        p.err("exptected an Implements Interfaces, Directives, or a Fields Definition");
    }
}