Skip to main content

oak_graphql/parser/
element_type.rs

1use oak_core::{ElementType, Parser, UniversalElementRole};
2
3/// Element types for GraphQL.
4#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
5#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
6pub enum GraphQLElementType {
7    /// A string literal.
8    StringLiteral,
9    /// An integer literal.
10    IntLiteral,
11    /// A float literal.
12    FloatLiteral,
13    /// A boolean literal.
14    BooleanLiteral,
15    /// A null literal.
16    NullLiteral,
17    /// A name (identifier).
18    Name,
19    /// The `query` keyword.
20    QueryKeyword,
21    /// The `mutation` keyword.
22    MutationKeyword,
23    /// The `subscription` keyword.
24    SubscriptionKeyword,
25    /// The `fragment` keyword.
26    FragmentKeyword,
27    /// The `on` keyword.
28    OnKeyword,
29    /// The `type` keyword.
30    TypeKeyword,
31    /// The `interface` keyword.
32    InterfaceKeyword,
33    /// The `union` keyword.
34    UnionKeyword,
35    /// The `scalar` keyword.
36    ScalarKeyword,
37    /// The `enum` keyword.
38    EnumKeyword,
39    /// The `input` keyword.
40    InputKeyword,
41    /// The `extend` keyword.
42    ExtendKeyword,
43    /// The `schema` keyword.
44    SchemaKeyword,
45    /// The `directive` keyword.
46    DirectiveKeyword,
47    /// The `implements` keyword.
48    ImplementsKeyword,
49    /// The `repeatable` keyword.
50    RepeatsKeyword,
51    /// The spread operator `...`.
52    Spread,
53    /// Left parenthesis `(`.
54    LeftParen,
55    /// Right parenthesis `)`.
56    RightParen,
57    /// Left bracket `[`.
58    LeftBracket,
59    /// Right bracket `]`.
60    RightBracket,
61    /// Left brace `{`.
62    LeftBrace,
63    /// Right brace `}`.
64    RightBrace,
65    /// Comma `,`.
66    Comma,
67    /// Colon `:`.
68    Colon,
69    /// Semicolon `;`.
70    Semicolon,
71    /// Pipe `|`.
72    Pipe,
73    /// Ampersand `&`.
74    Ampersand,
75    /// Equals `=`.
76    Equals,
77    /// Exclamation `!`.
78    Exclamation,
79    /// At symbol `@`.
80    At,
81    /// Dollar sign `$`.
82    Dollar,
83    /// Whitespace.
84    Whitespace,
85    /// A comment.
86    Comment,
87    /// The root source file.
88    SourceFile,
89    /// A newline.
90    Newline,
91    /// End of file.
92    Eof,
93    /// An error element.
94    Error,
95}
96
97impl GraphQLElementType {
98    /// Returns true if the element type is a keyword.
99    pub fn is_keyword(&self) -> bool {
100        matches!(
101            self,
102            Self::QueryKeyword
103                | Self::MutationKeyword
104                | Self::SubscriptionKeyword
105                | Self::FragmentKeyword
106                | Self::OnKeyword
107                | Self::TypeKeyword
108                | Self::InterfaceKeyword
109                | Self::UnionKeyword
110                | Self::ScalarKeyword
111                | Self::EnumKeyword
112                | Self::InputKeyword
113                | Self::ExtendKeyword
114                | Self::SchemaKeyword
115                | Self::DirectiveKeyword
116                | Self::ImplementsKeyword
117                | Self::RepeatsKeyword
118        )
119    }
120}
121
122impl ElementType for GraphQLElementType {
123    type Role = UniversalElementRole;
124
125    fn role(&self) -> Self::Role {
126        match self {
127            Self::SourceFile => UniversalElementRole::Root,
128            Self::Error => UniversalElementRole::Error,
129            _ => UniversalElementRole::None,
130        }
131    }
132}
133
134impl From<crate::lexer::token_type::GraphQLTokenType> for GraphQLElementType {
135    fn from(token: crate::lexer::token_type::GraphQLTokenType) -> Self {
136        unsafe { std::mem::transmute(token) }
137    }
138}