11/// This example describes how to use `apollo-parser` with
2- /// [`miette`](https://docs.rs/miette/3.2.0/miette) diagnostic library.
3- ///
4- ///
2+ /// [`ariadne`](https://docs.rs/ariadne/0.3.0/ariadne) diagnostic library.
53use std:: { fs, path:: Path } ;
64
75use apollo_parser:: { cst, Parser } ;
8- use miette:: { Diagnostic , NamedSource , Report , SourceSpan } ;
9- use thiserror:: Error ;
10-
11- // If your application is using a bunch of other thiserror errors,
12- // `ApolloParserError` can live within that enum and be responsible for just
13- // `apollo-parser` errors. It should work really nicely together!
14- #[ derive( Error , Debug , Diagnostic ) ]
15- #[ error( "{}" , self . ty) ]
16- #[ diagnostic( code( "apollo-parser parsing error." ) ) ]
17- struct ApolloParserError {
18- ty : String ,
19- #[ source_code]
20- src : NamedSource ,
21- #[ label( "{}" , self . ty) ]
22- span : SourceSpan ,
23- }
6+ use ariadne:: { Label , Report , ReportKind , Source } ;
247
258fn parse_schema ( ) -> cst:: Document {
269 let file = Path :: new ( "crates/apollo-parser/examples/schema_with_errors.graphql" ) ;
@@ -41,12 +24,14 @@ fn parse_schema() -> cst::Document {
4124 for err in cst. errors ( ) {
4225 // We need to create a report and print that individually, as the error
4326 // slice can have many errors.
44- let err = Report :: new ( ApolloParserError {
45- src : NamedSource :: new ( file_name, src. clone ( ) ) ,
46- span : ( err. index ( ) , err. data ( ) . len ( ) ) . into ( ) , // (offset, length of error token)
47- ty : err. message ( ) . into ( ) ,
48- } ) ;
49- println ! ( "{err:?}" ) ;
27+ let start = err. index ( ) ;
28+ let end = start + err. data ( ) . len ( ) ;
29+ Report :: build ( ReportKind :: Error , file_name, start)
30+ . with_message ( err. message ( ) )
31+ . with_label ( Label :: new ( ( file_name, start..end) ) . with_message ( err. message ( ) ) )
32+ . finish ( )
33+ . eprint ( ( file_name, Source :: from ( & src) ) )
34+ . unwrap ( ) ;
5035 }
5136
5237 cst. document ( )
0 commit comments