@@ -106,39 +106,12 @@ impl<'a> DisplayList<'a> {
106
106
const WARNING_TXT : & ' static str = "warning" ;
107
107
108
108
pub ( crate ) fn new (
109
- snippet:: Message {
110
- level,
111
- id,
112
- title,
113
- footer,
114
- snippets,
115
- } : snippet:: Message < ' a > ,
109
+ message : snippet:: Message < ' a > ,
116
110
stylesheet : & ' a Stylesheet ,
117
111
anonymized_line_numbers : bool ,
118
112
margin : Option < Margin > ,
119
113
) -> DisplayList < ' a > {
120
- let mut body = vec ! [ ] ;
121
-
122
- body. push ( format_title (
123
- snippet:: Label {
124
- level,
125
- label : title,
126
- } ,
127
- id,
128
- ) ) ;
129
-
130
- for ( idx, snippet) in snippets. into_iter ( ) . enumerate ( ) {
131
- body. append ( & mut format_slice (
132
- snippet,
133
- idx == 0 ,
134
- !footer. is_empty ( ) ,
135
- margin,
136
- ) ) ;
137
- }
138
-
139
- for annotation in footer {
140
- body. append ( & mut format_footer ( annotation) ) ;
141
- }
114
+ let body = format_message ( message, margin, true ) ;
142
115
143
116
Self {
144
117
body,
@@ -725,40 +698,64 @@ impl<'a> Iterator for CursorLines<'a> {
725
698
}
726
699
}
727
700
728
- fn format_label (
729
- label : Option < & str > ,
730
- style : Option < DisplayTextStyle > ,
731
- ) -> Vec < DisplayTextFragment < ' _ > > {
732
- let mut result = vec ! [ ] ;
733
- if let Some ( label) = label {
734
- let element_style = style. unwrap_or ( DisplayTextStyle :: Regular ) ;
735
- result. push ( DisplayTextFragment {
736
- content : label,
737
- style : element_style,
738
- } ) ;
701
+ fn format_message (
702
+ snippet:: Message {
703
+ level,
704
+ id,
705
+ title,
706
+ footer,
707
+ snippets,
708
+ } : snippet:: Message < ' _ > ,
709
+ margin : Option < Margin > ,
710
+ primary : bool ,
711
+ ) -> Vec < DisplayLine < ' _ > > {
712
+ let mut body = vec ! [ ] ;
713
+
714
+ if !snippets. is_empty ( ) || primary {
715
+ body. push ( format_title ( level, id, title) ) ;
716
+ } else {
717
+ body. extend ( format_footer ( level, id, title) ) ;
739
718
}
740
- result
719
+
720
+ for ( idx, snippet) in snippets. into_iter ( ) . enumerate ( ) {
721
+ body. extend ( format_snippet (
722
+ snippet,
723
+ idx == 0 ,
724
+ !footer. is_empty ( ) ,
725
+ margin,
726
+ ) ) ;
727
+ }
728
+
729
+ for annotation in footer {
730
+ body. extend ( format_message ( annotation, margin, false ) ) ;
731
+ }
732
+
733
+ body
741
734
}
742
735
743
- fn format_title < ' a > ( title : snippet :: Label < ' a > , id : Option < & ' a str > ) -> DisplayLine < ' a > {
736
+ fn format_title < ' a > ( level : crate :: Level , id : Option < & ' a str > , label : & ' a str ) -> DisplayLine < ' a > {
744
737
DisplayLine :: Raw ( DisplayRawLine :: Annotation {
745
738
annotation : Annotation {
746
- annotation_type : DisplayAnnotationType :: from ( title . level ) ,
739
+ annotation_type : DisplayAnnotationType :: from ( level) ,
747
740
id,
748
- label : format_label ( Some ( title . label ) , Some ( DisplayTextStyle :: Emphasis ) ) ,
741
+ label : format_label ( Some ( label) , Some ( DisplayTextStyle :: Emphasis ) ) ,
749
742
} ,
750
743
source_aligned : false ,
751
744
continuation : false ,
752
745
} )
753
746
}
754
747
755
- fn format_footer ( footer : snippet:: Label < ' _ > ) -> Vec < DisplayLine < ' _ > > {
748
+ fn format_footer < ' a > (
749
+ level : crate :: Level ,
750
+ id : Option < & ' a str > ,
751
+ label : & ' a str ,
752
+ ) -> Vec < DisplayLine < ' a > > {
756
753
let mut result = vec ! [ ] ;
757
- for ( i, line) in footer . label . lines ( ) . enumerate ( ) {
754
+ for ( i, line) in label. lines ( ) . enumerate ( ) {
758
755
result. push ( DisplayLine :: Raw ( DisplayRawLine :: Annotation {
759
756
annotation : Annotation {
760
- annotation_type : DisplayAnnotationType :: from ( footer . level ) ,
761
- id : None ,
757
+ annotation_type : DisplayAnnotationType :: from ( level) ,
758
+ id,
762
759
label : format_label ( Some ( line) , None ) ,
763
760
} ,
764
761
source_aligned : true ,
@@ -768,7 +765,22 @@ fn format_footer(footer: snippet::Label<'_>) -> Vec<DisplayLine<'_>> {
768
765
result
769
766
}
770
767
771
- fn format_slice (
768
+ fn format_label (
769
+ label : Option < & str > ,
770
+ style : Option < DisplayTextStyle > ,
771
+ ) -> Vec < DisplayTextFragment < ' _ > > {
772
+ let mut result = vec ! [ ] ;
773
+ if let Some ( label) = label {
774
+ let element_style = style. unwrap_or ( DisplayTextStyle :: Regular ) ;
775
+ result. push ( DisplayTextFragment {
776
+ content : label,
777
+ style : element_style,
778
+ } ) ;
779
+ }
780
+ result
781
+ }
782
+
783
+ fn format_snippet (
772
784
snippet : snippet:: Snippet < ' _ > ,
773
785
is_first : bool ,
774
786
has_footer : bool ,
@@ -777,14 +789,14 @@ fn format_slice(
777
789
let main_range = snippet. annotations . first ( ) . map ( |x| x. range . start ) ;
778
790
let origin = snippet. origin ;
779
791
let need_empty_header = origin. is_some ( ) || is_first;
780
- let mut body = format_body ( snippet, need_empty_header, has_footer, margin) ;
792
+ let body = format_body ( snippet, need_empty_header, has_footer, margin) ;
781
793
let header = format_header ( origin, main_range, & body, is_first) ;
782
794
let mut result = vec ! [ ] ;
783
795
784
796
if let Some ( header) = header {
785
797
result. push ( header) ;
786
798
}
787
- result. append ( & mut body) ;
799
+ result. extend ( body) ;
788
800
result
789
801
}
790
802
@@ -1444,7 +1456,7 @@ mod tests {
1444
1456
fn test_format_label ( ) {
1445
1457
let input = snippet:: Level :: Error
1446
1458
. title ( "" )
1447
- . footer ( snippet:: Label :: error ( "This __is__ a title" ) ) ;
1459
+ . footer ( snippet:: Level :: Error . title ( "This __is__ a title" ) ) ;
1448
1460
let output = from_display_lines ( vec ! [
1449
1461
DisplayLine :: Raw ( DisplayRawLine :: Annotation {
1450
1462
annotation: Annotation {
0 commit comments