Skip to main content

oak_graphql/parser/
element_type.rs

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