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>
impl<TAst> ParseResult<TAst>
Sourcepub fn valid_ast(&self) -> Option<&TAst>
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)
Sourcepub fn ast(&self) -> Option<&TAst>
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.
Sourcepub fn into_valid_ast(self) -> Option<TAst>
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().
Sourcepub fn into_ast(self) -> Option<TAst>
pub fn into_ast(self) -> Option<TAst>
Takes ownership of the AST regardless of errors.
This is the consuming version of ast().
Sourcepub fn is_ok(&self) -> bool
pub fn is_ok(&self) -> bool
Returns true if parsing was completely successful (has AST, no
errors).
Sourcepub fn has_errors(&self) -> bool
pub fn has_errors(&self) -> bool
Returns true if any errors were encountered during parsing.
Trait Implementations§
Source§impl<TAst: Debug> Debug for ParseResult<TAst>
impl<TAst: Debug> Debug for ParseResult<TAst>
Source§impl<TAst> From<ParseResult<TAst>> for Result<TAst, Vec<GraphQLParseError>>
impl<TAst> From<ParseResult<TAst>> for Result<TAst, Vec<GraphQLParseError>>
Source§fn from(result: ParseResult<TAst>) -> Self
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.