1- use super :: annotation:: { Annotation , DisplayAnnotationType } ;
1+ use super :: annotation:: Annotation ;
22use super :: line:: { DisplayLine , DisplayMark , DisplayMarkType , DisplayRawLine , DisplaySourceLine } ;
3+ use crate :: annotation:: AnnotationType ;
4+ use crate :: styles:: { get_stylesheet, Stylesheet } ;
35use crate :: { Slice , Snippet , SourceAnnotation } ;
46use std:: cmp;
57use std:: fmt;
@@ -9,6 +11,34 @@ pub struct DisplayList<'d> {
911pub body : Vec < DisplayLine < ' d > > ,
1012}
1113
14+ impl < ' d > DisplayList < ' d > {
15+ pub fn fmt_with_style (
16+ & self ,
17+ f : & mut fmt:: Formatter < ' _ > ,
18+ style : & impl Stylesheet ,
19+ ) -> fmt:: Result {
20+ let lineno_max =self . body . iter ( ) . rev ( ) . find_map ( |line|{
21+ if let DisplayLine :: Source {
22+ lineno : Some ( lineno) ,
23+ ..
24+ } = line
25+ {
26+ Some ( digits ( * lineno) )
27+ } else {
28+ None
29+ }
30+ } ) ;
31+ let inline_marks_width =self . body . iter ( ) . fold ( 0 , |max, line|match line{
32+ DisplayLine :: Source { inline_marks, ..} => cmp:: max ( inline_marks. len ( ) , max) ,
33+ _ => max,
34+ } ) ;
35+ for linein & self . body {
36+ line. fmt_with_style ( f, style, lineno_max, inline_marks_width) ?
37+ }
38+ Ok ( ( ) )
39+ }
40+ }
41+
1242fn get_header_pos ( slice : & Slice ) ->( Option < usize > , Option < usize > ) {
1343let line = slice. line_start ;
1444( line, None )
@@ -19,10 +49,10 @@ impl<'d> From<&Snippet<'d>> for DisplayList<'d> {
1949let mut body =vec ! [ ] ;
2050
2151if let Some ( annotation) =& snippet. title {
22- let label = annotation. label . clone ( ) . unwrap_or_default ( ) ;
52+ let label = annotation. label . unwrap_or_default ( ) ;
2353 body. push ( DisplayLine :: Raw ( DisplayRawLine :: Annotation {
2454annotation : Annotation {
25- annotation_type : DisplayAnnotationType :: Error ,
55+ annotation_type : AnnotationType :: Error ,
2656id : annotation. id ,
2757label : & label,
2858} ,
@@ -79,15 +109,15 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
79109// Annotation starts in this line
80110 inline_marks. push ( DisplayMark {
81111mark_type : DisplayMarkType :: AnnotationStart ,
82- annotation_type : DisplayAnnotationType :: Error ,
112+ annotation_type : AnnotationType :: Error ,
83113} ) ;
84114true
85115} else if ann. range . 0 < line_start_pos && ann. range . 1 > line_start_pos + line_length
86116{
87117// Annotation goes through this line
88118 inline_marks. push ( DisplayMark {
89119mark_type : DisplayMarkType :: AnnotationThrough ,
90- annotation_type : DisplayAnnotationType :: Error ,
120+ annotation_type : AnnotationType :: Error ,
91121} ) ;
92122true
93123} else if ann. range . 0 < line_start_pos
@@ -97,7 +127,7 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
97127// Annotation ends on this line
98128 inline_marks. push ( DisplayMark {
99129mark_type : DisplayMarkType :: AnnotationThrough ,
100- annotation_type : DisplayAnnotationType :: Error ,
130+ annotation_type : AnnotationType :: Error ,
101131} ) ;
102132 current_annotations. push ( * ann) ;
103133false
@@ -120,7 +150,7 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
120150let inline_marks =if ann. range . 0 < line_start_pos{
121151vec ! [ DisplayMark {
122152 mark_type: DisplayMarkType :: AnnotationThrough ,
123- annotation_type: DisplayAnnotationType :: Error ,
153+ annotation_type: AnnotationType :: Error ,
124154} ]
125155} else {
126156vec ! [ ]
@@ -130,7 +160,7 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
130160 inline_marks,
131161line : DisplaySourceLine :: Annotation {
132162annotation : Annotation {
133- annotation_type : DisplayAnnotationType :: Error ,
163+ annotation_type : AnnotationType :: Error ,
134164id : None ,
135165label : ann. label ,
136166} ,
@@ -152,36 +182,19 @@ impl<'d> From<&Slice<'d>> for DisplayList<'d> {
152182}
153183}
154184
155- fn digits ( n : & usize ) ->usize {
156- let mut n = n. clone ( ) ;
185+ fn digits ( n : usize ) ->usize {
186+ let mut n = n;
157187let mut sum =0 ;
158188while n !=0 {
159- n= n / 10 ;
189+ n/= 10 ;
160190 sum +=1 ;
161191}
162192 sum
163193}
164194
165195impl < ' d > fmt:: Display for DisplayList < ' d > {
166196fn fmt ( & self , f : & mut fmt:: Formatter < ' _ > ) -> fmt:: Result {
167- let lineno_max =self . body . iter ( ) . rev ( ) . find_map ( |line|{
168- if let DisplayLine :: Source {
169- lineno : Some ( lineno) ,
170- ..
171- } = line
172- {
173- Some ( digits ( lineno) )
174- } else {
175- None
176- }
177- } ) ;
178- let inline_marks_width =self . body . iter ( ) . fold ( 0 , |max, line|match line{
179- DisplayLine :: Source { inline_marks, ..} => cmp:: max ( inline_marks. len ( ) , max) ,
180- _ => max,
181- } ) ;
182- for linein & self . body {
183- line. fmt ( f, lineno_max, inline_marks_width) ?
184- }
185- Ok ( ( ) )
197+ let style =get_stylesheet ( ) ;
198+ self . fmt_with_style ( f, & style)
186199}
187200}