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#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
6#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
7pub enum GraphQLElementType {
8    StringLiteral,
9    IntLiteral,
10    FloatLiteral,
11    BooleanLiteral,
12    NullLiteral,
13    Name,
14    QueryKeyword,
15    MutationKeyword,
16    SubscriptionKeyword,
17    FragmentKeyword,
18    OnKeyword,
19    TypeKeyword,
20    InterfaceKeyword,
21    UnionKeyword,
22    ScalarKeyword,
23    EnumKeyword,
24    InputKeyword,
25    ExtendKeyword,
26    SchemaKeyword,
27    DirectiveKeyword,
28    ImplementsKeyword,
29    RepeatsKeyword,
30    Spread,
31    LeftParen,
32    RightParen,
33    LeftBracket,
34    RightBracket,
35    LeftBrace,
36    RightBrace,
37    Comma,
38    Colon,
39    Semicolon,
40    Pipe,
41    Ampersand,
42    Equals,
43    Exclamation,
44    At,
45    Dollar,
46    Whitespace,
47    Comment,
48    SourceFile,
49    Newline,
50    Eof,
51    Error,
52}
53
54impl GraphQLElementType {
55    pub fn is_keyword(&self) -> bool {
56        matches!(
57            self,
58            Self::QueryKeyword
59                | Self::MutationKeyword
60                | Self::SubscriptionKeyword
61                | Self::FragmentKeyword
62                | Self::OnKeyword
63                | Self::TypeKeyword
64                | Self::InterfaceKeyword
65                | Self::UnionKeyword
66                | Self::ScalarKeyword
67                | Self::EnumKeyword
68                | Self::InputKeyword
69                | Self::ExtendKeyword
70                | Self::SchemaKeyword
71                | Self::DirectiveKeyword
72                | Self::ImplementsKeyword
73                | Self::RepeatsKeyword
74        )
75    }
76}
77
78impl ElementType for GraphQLElementType {
79    type Role = UniversalElementRole;
80
81    fn role(&self) -> Self::Role {
82        match self {
83            Self::SourceFile => UniversalElementRole::Root,
84            Self::Error => UniversalElementRole::Error,
85            _ => UniversalElementRole::None,
86        }
87    }
88}
89
90impl From<crate::lexer::token_type::GraphQLTokenType> for GraphQLElementType {
91    fn from(token: crate::lexer::token_type::GraphQLTokenType) -> Self {
92        unsafe { std::mem::transmute(token) }
93    }
94}