Skip to main content

ParseResult

Enum ParseResult 

Source
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

VariantASTErrorsMeaning
OkyesnoCompletely successful parse
RecoveredyesyesBest-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 a SourceMap if 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 and SourceMap only if parsing was completely successful (no errors). Use this when you need guaranteed-valid input.

  • into_recovered - Consuming version of recovered.

  • into_valid() — Consuming version of valid().

  • into_ast() — Extracts the AST unconditionally, consuming the ParseResult. 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: TAst

The parsed AST.

§source_map: SourceMap<'src>

Maps byte offsets to line/column positions.

§

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.

Fields

§ast: TAst

The recovered AST.

§errors: Vec<GraphQLParseError>

Errors encountered during parsing (always non-empty).

§source_map: SourceMap<'src>

Maps byte offsets to line/column positions.

Implementations§

Source§

impl<'src, TAst> ParseResult<'src, TAst>

Source

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().

Source

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.

Source

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.

Source

pub fn has_errors(&self) -> bool

Returns true if any errors were encountered during parsing.

Source

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.

Source

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().

Source

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().

Source

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().

Source

pub fn source_map(&self) -> &SourceMap<'src>

Returns a reference to the SourceMap for resolving byte offsets to line/column positions.

Source

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>>

Source

pub fn definitions(&self) -> &[Definition<'src>]

Convenience function for accessing the Document::definitions field after parsing a Document.

Source

pub fn executable_definitions(&self) -> impl Iterator<Item = &Definition<'src>>

Convenience function for calling Document::executable_definitions() after parsing a Document.

Source

pub fn schema_definitions(&self) -> impl Iterator<Item = &Definition<'src>>

Convenience function for calling Document::schema_definitions() after parsing a Document.

Source

pub fn span(&self) -> ByteSpan

Convenience function for accessing the Document::span field after parsing a Document.

Source

pub fn syntax(&self) -> &Option<Box<DocumentSyntax<'src>>>

Convenience function for accessing the Document::syntax field after parsing a Document.

Source

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>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'src, TAst> From<ParseResult<'src, TAst>> for Result<(TAst, SourceMap<'src>), Vec<GraphQLParseError>>

Source§

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.

Auto Trait Implementations§

§

impl<'src, TAst> Freeze for ParseResult<'src, TAst>
where TAst: Freeze,

§

impl<'src, TAst> RefUnwindSafe for ParseResult<'src, TAst>
where TAst: RefUnwindSafe,

§

impl<'src, TAst> Send for ParseResult<'src, TAst>
where TAst: Send,

§

impl<'src, TAst> Sync for ParseResult<'src, TAst>
where TAst: Sync,

§

impl<'src, TAst> Unpin for ParseResult<'src, TAst>
where TAst: Unpin,

§

impl<'src, TAst> UnsafeUnpin for ParseResult<'src, TAst>
where TAst: UnsafeUnpin,

§

impl<'src, TAst> UnwindSafe for ParseResult<'src, TAst>
where TAst: UnwindSafe,

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.