pub enum GraphQLTokenKind<'src> {
Show 23 variants
Ampersand,
At,
Bang,
Colon,
CurlyBraceClose,
CurlyBraceOpen,
Dollar,
Ellipsis,
Equals,
ParenClose,
ParenOpen,
Pipe,
SquareBracketClose,
SquareBracketOpen,
Name(Cow<'src, str>),
IntValue(Cow<'src, str>),
FloatValue(Cow<'src, str>),
StringValue(Cow<'src, str>),
True,
False,
Null,
Eof,
Error(Box<GraphQLTokenError>),
}Expand description
The kind of a GraphQL token.
Literal values (IntValue, FloatValue, StringValue) store only the raw
source text.
§Lifetime Parameter
The 'src lifetime enables zero-copy lexing: StrGraphQLTokenSource can
borrow string slices directly from the source text using Cow::Borrowed,
while RustMacroGraphQLTokenSource uses Cow::Owned since proc_macro2
doesn’t expose contiguous source text.
§Negative Numeric Literals
Negative numbers like -123 are lexed as single tokens (e.g.
IntValue("-123")), not as separate minus and number tokens. This matches
the GraphQL spec’s grammar for IntValue/FloatValue.
Variants§
Ampersand
&
At
@
Bang
!
Colon
:
CurlyBraceClose
}
CurlyBraceOpen
{
Dollar
$
Ellipsis
...
Equals
=
ParenClose
)
ParenOpen
(
Pipe
|
SquareBracketClose
]
SquareBracketOpen
[
Name(Cow<'src, str>)
A GraphQL name/identifier.
Uses Cow<'src, str> to enable zero-copy lexing from string sources.
IntValue(Cow<'src, str>)
Raw source text of an integer literal, including optional negative sign
(e.g. "-123", "0").
Use parse_int_value() to parse the raw text into an i64.
Uses Cow<'src, str> to enable zero-copy lexing from string sources.
FloatValue(Cow<'src, str>)
Raw source text of a float literal, including optional negative sign
(e.g. "-1.23e-4", "0.5").
Use parse_float_value() to parse the raw text into an f64.
Uses Cow<'src, str> to enable zero-copy lexing from string sources.
StringValue(Cow<'src, str>)
Raw source text of a string literal, including quotes
(e.g. "\"hello\\nworld\"", "\"\"\"block\"\"\"")
Use parse_string_value() to process escape sequences and get the
unescaped content.
Uses Cow<'src, str> to enable zero-copy lexing from string sources.
True
The true literal.
False
The false literal.
Null
The null literal.
Eof
End of input. The associated GraphQLToken may carry trailing trivia.
Error(Box<GraphQLTokenError>)
A lexer error. This allows the parser to continue and collect multiple errors in a single pass.
§Performance Note (B19)
The error payload is boxed to avoid bloating the enum’s size. Without
the Box, the SmallVec<[GraphQLErrorNote; 2]> error-notes field
(~208 bytes) would force every variant of GraphQLTokenKind to be
~232 bytes — even zero-data punctuators. Boxing shrinks the Error
variant to a single pointer, which dramatically reduces
the size of every GraphQLToken on the happy path where errors
never occur (zero additional heap allocations in practice).
TODO: Explore replacing error_notes with a richer diagnostics structure that includes things like severity level and “fix action” for IDE integration.
Implementations§
Source§impl<'src> GraphQLTokenKind<'src>
impl<'src> GraphQLTokenKind<'src>
Sourcepub fn name_borrowed(s: &'src str) -> Self
pub fn name_borrowed(s: &'src str) -> Self
Create a Name token from a borrowed string slice (zero-copy).
Use this in StrGraphQLTokenSource where the source text can be
borrowed directly.
Sourcepub fn name_owned(s: String) -> Self
pub fn name_owned(s: String) -> Self
Create a Name token from an owned String.
Use this in RustMacroGraphQLTokenSource where the string must be
allocated (e.g., from ident.to_string()).
Sourcepub fn int_value_borrowed(s: &'src str) -> Self
pub fn int_value_borrowed(s: &'src str) -> Self
Create an IntValue token from a borrowed string slice (zero-copy).
Sourcepub fn int_value_owned(s: String) -> Self
pub fn int_value_owned(s: String) -> Self
Create an IntValue token from an owned String.
Sourcepub fn float_value_borrowed(s: &'src str) -> Self
pub fn float_value_borrowed(s: &'src str) -> Self
Create a FloatValue token from a borrowed string slice (zero-copy).
Sourcepub fn float_value_owned(s: String) -> Self
pub fn float_value_owned(s: String) -> Self
Create a FloatValue token from an owned String.
Sourcepub fn string_value_borrowed(s: &'src str) -> Self
pub fn string_value_borrowed(s: &'src str) -> Self
Create a StringValue token from a borrowed string slice (zero-copy).
Sourcepub fn string_value_owned(s: String) -> Self
pub fn string_value_owned(s: String) -> Self
Create a StringValue token from an owned String.
Sourcepub fn error(
message: impl Into<String>,
error_notes: SmallVec<[GraphQLErrorNote; 2]>,
) -> Self
pub fn error( message: impl Into<String>, error_notes: SmallVec<[GraphQLErrorNote; 2]>, ) -> Self
Create an Error token.
Error messages are always dynamically constructed, so they use plain
String rather than Cow.
Sourcepub fn is_punctuator(&self) -> bool
pub fn is_punctuator(&self) -> bool
Returns true if this token is a punctuator.
Sourcepub fn as_punctuator_str(&self) -> Option<&'static str>
pub fn as_punctuator_str(&self) -> Option<&'static str>
Returns the string representation of this token if it is a punctuator.
Sourcepub fn is_value(&self) -> bool
pub fn is_value(&self) -> bool
Returns true if this token is a value literal (IntValue,
FloatValue, StringValue, True, False, or Null).
Sourcepub fn parse_int_value(&self) -> Option<Result<i64, ParseIntError>>
pub fn parse_int_value(&self) -> Option<Result<i64, ParseIntError>>
Parse an IntValue’s raw text to i64.
Returns None if this is not an IntValue, or Some(Err(...)) if
parsing fails.
Sourcepub fn parse_float_value(&self) -> Option<Result<f64, ParseFloatError>>
pub fn parse_float_value(&self) -> Option<Result<f64, ParseFloatError>>
Parse a FloatValue’s raw text to f64.
Returns None if this is not a FloatValue, or Some(Err(...)) if
parsing fails.
Sourcepub fn parse_string_value(
&self,
) -> Option<Result<String, GraphQLStringParsingError>>
pub fn parse_string_value( &self, ) -> Option<Result<String, GraphQLStringParsingError>>
Parse a StringValue’s raw text to unescaped content.
Handles escape sequences per the GraphQL spec:
- For single-line strings (
"..."): processes\n,\r,\t,\\,\",\/,\b,\f,\uXXXX(fixed 4-digit), and\u{X...}(variable length). - For block strings (
"""..."""): applies the indentation stripping algorithm per spec, then processes\"""escape only.
Returns None if this is not a StringValue, or Some(Err(...)) if
parsing fails.
Trait Implementations§
Source§impl<'src> Clone for GraphQLTokenKind<'src>
impl<'src> Clone for GraphQLTokenKind<'src>
Source§fn clone(&self) -> GraphQLTokenKind<'src>
fn clone(&self) -> GraphQLTokenKind<'src>
1.0.0 · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read more