Skip to main content

ParseResult

Struct ParseResult 

Source
pub struct ParseResult<TAst> {
    pub errors: Vec<GraphQLParseError>,
    /* private fields */
}
Expand description

The result of a parsing operation.

Unlike Result<T, E>, ParseResult can contain both a partial AST and errors. This enables error recovery: the parser can report multiple errors while still producing as much AST as possible.

§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

§Accessing the AST

Two methods are provided for accessing the AST, depending on your use case:

  • valid_ast() - Returns the AST only if parsing was completely successful (no errors). Use this when you need guaranteed-valid input.

  • ast() - Returns the AST if present, regardless of errors. Use this for tools that want best-effort results (formatters, IDE features, linters).

§Example

let result = parser.parse_schema_document();

// Strict mode: only accept fully valid documents
if let Some(doc) = result.valid_ast() {
    analyze_schema(doc);
}

// Best-effort mode: work with whatever we got
if let Some(doc) = result.ast() {
    provide_ide_completions(doc);
}

// Report any errors
if result.has_errors() {
    for error in &result.errors {
        eprintln!("{}", error.format_detailed(Some(source)));
    }
}

Fields§

§errors: Vec<GraphQLParseError>

Errors encountered during parsing.

Empty if parsing was completely successful.

Implementations§

Source§

impl<TAst> ParseResult<TAst>

Source

pub fn valid_ast(&self) -> Option<&TAst>

Returns the AST 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 failed entirely (no AST produced)
  • Parsing succeeded but with errors (recovered AST)
Source

pub fn ast(&self) -> Option<&TAst>

Returns the AST if present, regardless of whether errors 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() to determine if the AST was produced via error recovery.

Source

pub fn into_valid_ast(self) -> Option<TAst>

Takes ownership of the AST only if parsing was completely successful.

This is the consuming version of valid_ast().

Source

pub fn into_ast(self) -> Option<TAst>

Takes ownership of the AST regardless of errors.

This is the consuming version of ast().

Source

pub fn is_ok(&self) -> bool

Returns true if parsing was completely successful (has AST, no errors).

Source

pub fn has_errors(&self) -> bool

Returns true if any errors were encountered during parsing.

Source

pub fn format_errors(&self, source: Option<&str>) -> String

Formats all errors as a single string for display.

§Arguments
  • source: Optional source text for snippet extraction in error messages.

Trait Implementations§

Source§

impl<TAst: Debug> Debug for ParseResult<TAst>

Source§

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

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

impl<TAst> From<ParseResult<TAst>> for Result<TAst, Vec<GraphQLParseError>>

Source§

fn from(result: ParseResult<TAst>) -> Self

Converts to a standard Result, treating recovered ASTs as errors.

Returns Ok(ast) only if there were no errors. Otherwise returns Err(errors), even if a recovered AST was available.

Auto Trait Implementations§

§

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

§

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

§

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

§

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

§

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

§

impl<TAst> UnwindSafe for ParseResult<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.