11use crate :: parser:: SourceCode ;
2- use annotate_snippets:: display_list:: { DisplayList , FormatOptions } ;
3- use annotate_snippets:: snippet:: { Annotation , AnnotationType , Slice , Snippet , SourceAnnotation } ;
2+ use miette:: { Diagnostic , LabeledSpan } ;
43use std:: fmt:: Formatter ;
54
65#[ must_use]
6+ #[ derive( Debug , Diagnostic ) ]
7+ #[ diagnostic( severity( Error ) , code( "file_io_error" ) ) ]
78pub struct FileIOError < ' a > {
89 file_path : & ' a str ,
910 io_error : std:: io:: Error ,
@@ -18,72 +19,45 @@ impl<'a> FileIOError<'a> {
1819 }
1920
2021 pub fn emit ( & self ) -> ErrorReported {
21- println ! ( "{} " , self . to_string ( self . file_path, & self . io_error) ) ;
22+ eprintln ! ( "couldn't read {}: {} " , self . file_path, self . io_error) ;
2223 ErrorReported
2324 }
24-
25- fn to_string ( & self , path_to_file : & str , err : & std:: io:: Error ) -> String {
26- let label = format ! ( "couldn't read {}: {}" , path_to_file, err) ;
27- let snippet = Snippet {
28- title : Some ( Annotation {
29- id : None ,
30- label : Some ( & label) ,
31- annotation_type : AnnotationType :: Error ,
32- } ) ,
33- footer : vec ! [ ] ,
34- slices : vec ! [ ] ,
35- opt : default_format ( ) ,
36- } ;
37- DisplayList :: from ( snippet) . to_string ( )
38- }
3925}
4026
41- impl std:: fmt:: Debug for FileIOError < ' _ > {
27+ impl < ' a > std:: fmt:: Display for FileIOError < ' a > {
4228 fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
43- write ! ( f, "{}" , self . io_error)
29+ write ! ( f, "couldn't read {}: {}" , self . file_path , self . io_error)
4430 }
4531}
4632
47- pub fn default_format ( ) -> FormatOptions {
48- FormatOptions {
49- color : true ,
50- anonymized_line_numbers : false ,
51- margin : None ,
33+ impl < ' a > std:: error:: Error for FileIOError < ' a > {
34+ fn source ( & self ) -> Option < & ( dyn std:: error:: Error + ' static ) > {
35+ Some ( & self . io_error )
5236 }
5337}
5438
5539pub fn single_line_snippet < ' a > (
5640 source : & ' a SourceCode < ' a > ,
57- label : & ' a str ,
41+ _label : & ' a str ,
5842 annotation : & ' a str ,
5943 line_num : u32 ,
6044 begin_column : u32 ,
6145 end_column : u32 ,
62- ) -> Snippet < ' a > {
63- let slices = if let Some ( line ) = source. get_line ( line_num) {
64- vec ! [ Slice {
65- source: line ,
66- line_start : line_num as usize + 1 ,
67- origin : source . file_path ,
68- annotations : vec! [ SourceAnnotation {
69- range : ( begin_column as usize , end_column as usize ) ,
70- label : annotation ,
71- annotation_type : AnnotationType :: Error ,
72- } ] ,
73- fold : false ,
74- } ]
46+ ) -> miette :: Report {
47+ if let Some ( _line ) = source. get_line ( line_num) {
48+ let start_offset = source . get_offset ( line_num , begin_column ) ;
49+ let end_offset = source. get_offset ( line_num , end_column ) ;
50+
51+ miette :: miette! (
52+ labels = [ LabeledSpan :: new_with_span (
53+ Some ( annotation . to_string ( ) ) ,
54+ start_offset..end_offset
55+ ) ] ,
56+ "{}" ,
57+ annotation
58+ )
7559 } else {
76- vec ! [ ]
77- } ;
78- Snippet {
79- title : Some ( Annotation {
80- id : None ,
81- label : Some ( label) ,
82- annotation_type : AnnotationType :: Error ,
83- } ) ,
84- footer : vec ! [ ] ,
85- slices,
86- opt : default_format ( ) ,
60+ miette:: miette!( "{}" , annotation)
8761 }
8862}
8963
0 commit comments