pub enum ParseResult<'src, TAst> {
Ok {
ast: TAst,
source_map: SourceMap<'src>,
},
Recovered {
ast: TAst,
errors: Vec<GraphQLParseError>,
source_map: SourceMap<'src>,
},
}Expand description
The result of a parsing operation.
Unlike Result<T, E>, ParseResult can represent a recovered parse:
one that produced an AST and encountered errors. This enables error
recovery — the parser can report multiple errors while still producing
as much AST as possible.
§Variants
| Variant | AST | Errors | Meaning |
|---|---|---|---|
Ok | yes | no | Completely successful parse |
Recovered | yes | yes | Best-effort AST with errors encountered |
An AST is always present — there is no “complete failure” variant. Even when the parser encounters errors, it produces a partial/recovered AST via error recovery.
§Design Rationale
Traditional Result<T, E> forces a binary choice: either success with a
value, or failure with an error. But GraphQL tooling often benefits from
having both:
- IDE integration: Show syntax errors while still providing completions based on the partially-parsed document
- Batch error reporting: Report all syntax errors in one pass rather than stopping at the first error
- Graceful degradation: Process as much of a document as possible even when parts are invalid
§SourceMap
Every ParseResult carries a SourceMap that maps byte offsets
(stored in ByteSpans on AST nodes and tokens)
to line/column positions on demand, and provides access to the
source text via source(). Parse errors carry
pre-resolved SourceSpans and do not need the
SourceMap for position resolution.
§Accessing the AST
-
recovered()- Returns the error-recovered AST, a&[GraphQLParseError], and aSourceMapif there were 1 or more errors parsing the AST. The AST is “recovered” AST in the sense that the parser made a best-effort attempt at either guessing the intended AST or skipping any portion of the input for which it could not make a reasonable guess. -
valid()— Returns the AST andSourceMaponly if parsing was completely successful (no errors). Use this when you need guaranteed-valid input. -
into_recovered- Consuming version ofrecovered. -
into_valid()— Consuming version ofvalid(). -
into_ast()— Extracts the AST unconditionally, consuming theParseResult. Use this for tools that want best-effort results (formatters, IDE features, linters). -
Pattern matching — For borrowing the AST unconditionally while retaining access to errors, match on the enum variants directly.
§Example
let result = parser.parse_schema_document();
// Strict mode: only accept fully valid documents
if let Some((doc, source_map)) = result.valid() {
analyze_schema(doc, source_map);
}
// Best-effort mode: match to borrow the AST unconditionally
let ast = match &result {
ParseResult::Ok { ast, .. }
| ParseResult::Recovered { ast, .. } => ast,
};
provide_ide_completions(ast);
// Report any errors
if result.has_errors() {
for error in result.errors() {
eprintln!("{}", error.format_detailed(result.source_map().source()));
}
}Variants§
Ok
Completely successful parse — the AST is valid and no errors were encountered.
Fields
ast: TAstThe parsed AST.
Recovered
Recovered parse — an AST was produced via error recovery, but errors were encountered. The AST may be incomplete or contain placeholder values.
Invariant: errors is always non-empty for this variant.
Implementations§
Source§impl<'src, TAst> ParseResult<'src, TAst>
impl<'src, TAst> ParseResult<'src, TAst>
Sourcepub fn ast(&self) -> &TAst
pub fn ast(&self) -> &TAst
Returns a reference to the AST unconditionally.
An AST is always present in a ParseResult, even when
parsing errors may have occurred. For the consuming
version, see into_ast().
Sourcepub fn errors(&self) -> &[GraphQLParseError]
pub fn errors(&self) -> &[GraphQLParseError]
Returns the errors encountered during parsing.
Returns an empty slice for Ok, or the non-empty error list for
Recovered.
Sourcepub fn formatted_errors(&self) -> String
pub fn formatted_errors(&self) -> String
Formats all errors as a single string for display.
Uses the bundled SourceMap’s source text (if available)
for snippet extraction.
Sourcepub fn has_errors(&self) -> bool
pub fn has_errors(&self) -> bool
Returns true if any errors were encountered during parsing.
Sourcepub fn into_ast(self) -> TAst
pub fn into_ast(self) -> TAst
Takes ownership of the AST unconditionally.
An AST is always present in a ParseResult, even when parsing errors
may have occurred. Use this for tools that want best-effort results:
- IDE features (completions, hover info)
- Formatters (format what we can parse)
- Linters (report issues even in partially-valid documents)
Check has_errors() before calling if you need
to know whether the AST was produced via error recovery.
Sourcepub fn into_recovered(
self,
) -> Option<(TAst, Vec<GraphQLParseError>, SourceMap<'src>)>
pub fn into_recovered( self, ) -> Option<(TAst, Vec<GraphQLParseError>, SourceMap<'src>)>
Takes ownership of the AST, errors, and source map only if parsing encountered errors (recovered parse).
Returns None for a completely successful parse.
This is the consuming version of
recovered().
Sourcepub fn into_valid(self) -> Option<(TAst, SourceMap<'src>)>
pub fn into_valid(self) -> Option<(TAst, SourceMap<'src>)>
Takes ownership of the AST only if parsing was completely successful.
This is the consuming version of valid().
Sourcepub fn recovered(
&self,
) -> Option<(&TAst, &[GraphQLParseError], &SourceMap<'src>)>
pub fn recovered( &self, ) -> Option<(&TAst, &[GraphQLParseError], &SourceMap<'src>)>
Returns the AST, errors, and source map only if parsing encountered errors (recovered parse).
Returns None for a completely successful parse. For the
consuming version, see
into_recovered().
Sourcepub fn source_map(&self) -> &SourceMap<'src>
pub fn source_map(&self) -> &SourceMap<'src>
Returns a reference to the SourceMap for resolving byte offsets
to line/column positions.
Sourcepub fn valid(&self) -> Option<(&TAst, &SourceMap<'src>)>
pub fn valid(&self) -> Option<(&TAst, &SourceMap<'src>)>
Returns the AST & SourceMap only if parsing was completely
successful (no errors).
Use this when you need guaranteed-valid input, such as when compiling a schema or executing a query.
Returns None if parsing encountered errors (recovered parse).
Source§impl<'src> ParseResult<'src, Document<'src>>
impl<'src> ParseResult<'src, Document<'src>>
Sourcepub fn definitions(&self) -> &[Definition<'src>]
pub fn definitions(&self) -> &[Definition<'src>]
Convenience function for accessing the
Document::definitions field after
parsing a Document.
Sourcepub fn executable_definitions(&self) -> impl Iterator<Item = &Definition<'src>>
pub fn executable_definitions(&self) -> impl Iterator<Item = &Definition<'src>>
Convenience function for calling
Document::executable_definitions()
after parsing a Document.
Sourcepub fn schema_definitions(&self) -> impl Iterator<Item = &Definition<'src>>
pub fn schema_definitions(&self) -> impl Iterator<Item = &Definition<'src>>
Convenience function for calling
Document::schema_definitions()
after parsing a Document.
Sourcepub fn span(&self) -> ByteSpan
pub fn span(&self) -> ByteSpan
Convenience function for accessing the
Document::span field after parsing
a Document.
Sourcepub fn syntax(&self) -> &Option<Box<DocumentSyntax<'src>>>
pub fn syntax(&self) -> &Option<Box<DocumentSyntax<'src>>>
Convenience function for accessing the
Document::syntax field after parsing
a Document.
Sourcepub fn trailing_trivia(&self) -> Option<&[GraphQLTriviaToken<'src>]>
pub fn trailing_trivia(&self) -> Option<&[GraphQLTriviaToken<'src>]>
Convenience function for calling
Document::trailing_trivia() after
parsing a Document.
Trait Implementations§
Source§impl<'src, TAst: Debug> Debug for ParseResult<'src, TAst>
impl<'src, TAst: Debug> Debug for ParseResult<'src, TAst>
Source§impl<'src, TAst> From<ParseResult<'src, TAst>> for Result<(TAst, SourceMap<'src>), Vec<GraphQLParseError>>
impl<'src, TAst> From<ParseResult<'src, TAst>> for Result<(TAst, SourceMap<'src>), Vec<GraphQLParseError>>
Source§fn from(result: ParseResult<'src, TAst>) -> Self
fn from(result: ParseResult<'src, TAst>) -> Self
Converts to a standard Result, treating recovered ASTs as errors.
Returns Ok((ast, source_map)) only if there were no errors.
Otherwise returns Err(errors), discarding the recovered AST.