cynic_parser/type_system/
definitions.rs

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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
use crate::AstLookup;

use super::{
    ids::*,
    iter::{IdReader, Iter},
    DefinitionId, Description, Directive, DirectiveDefinition, EnumDefinition,
    InputObjectDefinition, InterfaceDefinition, ObjectDefinition, ScalarDefinition,
    SchemaDefinition, TypeSystemId, UnionDefinition,
};

#[derive(Clone, Copy)]
pub enum DefinitionRecord {
    Schema(SchemaDefinitionId),
    Scalar(ScalarDefinitionId),
    Object(ObjectDefinitionId),
    Interface(InterfaceDefinitionId),
    Union(UnionDefinitionId),
    Enum(EnumDefinitionId),
    InputObject(InputObjectDefinitionId),
    SchemaExtension(SchemaDefinitionId),
    ScalarExtension(ScalarDefinitionId),
    ObjectExtension(ObjectDefinitionId),
    InterfaceExtension(InterfaceDefinitionId),
    UnionExtension(UnionDefinitionId),
    EnumExtension(EnumDefinitionId),
    InputObjectExtension(InputObjectDefinitionId),
    Directive(DirectiveDefinitionId),
}

#[derive(Clone, Copy)]
pub enum Definition<'a> {
    Schema(SchemaDefinition<'a>),
    SchemaExtension(SchemaDefinition<'a>),
    Type(TypeDefinition<'a>),
    TypeExtension(TypeDefinition<'a>),
    Directive(DirectiveDefinition<'a>),
}

impl Definition<'_> {
    pub fn span(&self) -> crate::Span {
        match self {
            Definition::Schema(def) | Definition::SchemaExtension(def) => def.span(),
            Definition::Type(ty) | Definition::TypeExtension(ty) => ty.span(),
            Definition::Directive(def) => def.span(),
        }
    }
}

#[derive(Clone, Copy)]
pub enum TypeDefinition<'a> {
    Scalar(ScalarDefinition<'a>),
    Object(ObjectDefinition<'a>),
    Interface(InterfaceDefinition<'a>),
    Union(UnionDefinition<'a>),
    Enum(EnumDefinition<'a>),
    InputObject(InputObjectDefinition<'a>),
}

impl<'a> TypeDefinition<'a> {
    pub fn name(&self) -> &'a str {
        match self {
            TypeDefinition::Scalar(inner) => inner.name(),
            TypeDefinition::Object(inner) => inner.name(),
            TypeDefinition::Interface(inner) => inner.name(),
            TypeDefinition::Union(inner) => inner.name(),
            TypeDefinition::Enum(inner) => inner.name(),
            TypeDefinition::InputObject(inner) => inner.name(),
        }
    }

    pub fn directives(&self) -> Iter<'a, Directive<'a>> {
        match self {
            TypeDefinition::Scalar(inner) => inner.directives(),
            TypeDefinition::Object(inner) => inner.directives(),
            TypeDefinition::Interface(inner) => inner.directives(),
            TypeDefinition::Union(inner) => inner.directives(),
            TypeDefinition::Enum(inner) => inner.directives(),
            TypeDefinition::InputObject(inner) => inner.directives(),
        }
    }

    pub fn span(&self) -> crate::Span {
        match self {
            TypeDefinition::Scalar(inner) => inner.span(),
            TypeDefinition::Object(inner) => inner.span(),
            TypeDefinition::Interface(inner) => inner.span(),
            TypeDefinition::Union(inner) => inner.span(),
            TypeDefinition::Enum(inner) => inner.span(),
            TypeDefinition::InputObject(inner) => inner.span(),
        }
    }

    pub fn description(&self) -> Option<Description<'a>> {
        match self {
            TypeDefinition::Scalar(inner) => inner.description(),
            TypeDefinition::Object(inner) => inner.description(),
            TypeDefinition::Interface(inner) => inner.description(),
            TypeDefinition::Union(inner) => inner.description(),
            TypeDefinition::Enum(inner) => inner.description(),
            TypeDefinition::InputObject(inner) => inner.description(),
        }
    }
}

impl TypeSystemId for DefinitionId {
    type Reader<'a> = Definition<'a>;

    fn read(self, document: &super::TypeSystemDocument) -> Self::Reader<'_> {
        match document.lookup(self) {
            DefinitionRecord::Schema(id) => Definition::Schema(document.read(*id)),
            DefinitionRecord::Scalar(id) => {
                Definition::Type(TypeDefinition::Scalar(document.read(*id)))
            }
            DefinitionRecord::Object(id) => {
                Definition::Type(TypeDefinition::Object(document.read(*id)))
            }
            DefinitionRecord::Interface(id) => {
                Definition::Type(TypeDefinition::Interface(document.read(*id)))
            }
            DefinitionRecord::Union(id) => {
                Definition::Type(TypeDefinition::Union(document.read(*id)))
            }
            DefinitionRecord::Enum(id) => {
                Definition::Type(TypeDefinition::Enum(document.read(*id)))
            }
            DefinitionRecord::InputObject(id) => {
                Definition::Type(TypeDefinition::InputObject(document.read(*id)))
            }
            DefinitionRecord::SchemaExtension(id) => {
                Definition::SchemaExtension(document.read(*id))
            }
            DefinitionRecord::ScalarExtension(id) => {
                Definition::TypeExtension(TypeDefinition::Scalar(document.read(*id)))
            }
            DefinitionRecord::ObjectExtension(id) => {
                Definition::TypeExtension(TypeDefinition::Object(document.read(*id)))
            }
            DefinitionRecord::InterfaceExtension(id) => {
                Definition::TypeExtension(TypeDefinition::Interface(document.read(*id)))
            }
            DefinitionRecord::UnionExtension(id) => {
                Definition::TypeExtension(TypeDefinition::Union(document.read(*id)))
            }
            DefinitionRecord::EnumExtension(id) => {
                Definition::TypeExtension(TypeDefinition::Enum(document.read(*id)))
            }
            DefinitionRecord::InputObjectExtension(id) => {
                Definition::TypeExtension(TypeDefinition::InputObject(document.read(*id)))
            }
            DefinitionRecord::Directive(id) => Definition::Directive(document.read(*id)),
        }
    }
}

impl IdReader for Definition<'_> {
    type Id = DefinitionId;
    type Reader<'a> = Definition<'a>;

    fn new(id: Self::Id, document: &'_ super::TypeSystemDocument) -> Self::Reader<'_> {
        document.read(id)
    }
}