@@ -106,39 +106,12 @@ impl<'a> DisplayList<'a> {
106106 const WARNING_TXT : & ' static str = "warning" ;
107107
108108 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 > ,
116110 stylesheet : & ' a Stylesheet ,
117111 anonymized_line_numbers : bool ,
118112 margin : Option < Margin > ,
119113 ) -> 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 ) ;
142115
143116 Self {
144117 body,
@@ -725,40 +698,64 @@ impl<'a> Iterator for CursorLines<'a> {
725698 }
726699}
727700
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) ) ;
739718 }
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
741734}
742735
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 > {
744737 DisplayLine :: Raw ( DisplayRawLine :: Annotation {
745738 annotation : Annotation {
746- annotation_type : DisplayAnnotationType :: from ( title . level ) ,
739+ annotation_type : DisplayAnnotationType :: from ( level) ,
747740 id,
748- label : format_label ( Some ( title . label ) , Some ( DisplayTextStyle :: Emphasis ) ) ,
741+ label : format_label ( Some ( label) , Some ( DisplayTextStyle :: Emphasis ) ) ,
749742 } ,
750743 source_aligned : false ,
751744 continuation : false ,
752745 } )
753746}
754747
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 > > {
756753 let mut result = vec ! [ ] ;
757- for ( i, line) in footer . label . lines ( ) . enumerate ( ) {
754+ for ( i, line) in label. lines ( ) . enumerate ( ) {
758755 result. push ( DisplayLine :: Raw ( DisplayRawLine :: Annotation {
759756 annotation : Annotation {
760- annotation_type : DisplayAnnotationType :: from ( footer . level ) ,
761- id : None ,
757+ annotation_type : DisplayAnnotationType :: from ( level) ,
758+ id,
762759 label : format_label ( Some ( line) , None ) ,
763760 } ,
764761 source_aligned : true ,
@@ -768,7 +765,22 @@ fn format_footer(footer: snippet::Label<'_>) -> Vec<DisplayLine<'_>> {
768765 result
769766}
770767
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 (
772784 snippet : snippet:: Snippet < ' _ > ,
773785 is_first : bool ,
774786 has_footer : bool ,
@@ -777,14 +789,14 @@ fn format_slice(
777789 let main_range = snippet. annotations . first ( ) . map ( |x| x. range . start ) ;
778790 let origin = snippet. origin ;
779791 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) ;
781793 let header = format_header ( origin, main_range, & body, is_first) ;
782794 let mut result = vec ! [ ] ;
783795
784796 if let Some ( header) = header {
785797 result. push ( header) ;
786798 }
787- result. append ( & mut body) ;
799+ result. extend ( body) ;
788800 result
789801}
790802
@@ -1444,7 +1456,7 @@ mod tests {
14441456 fn test_format_label ( ) {
14451457 let input = snippet:: Level :: Error
14461458 . title ( "" )
1447- . footer ( snippet:: Label :: error ( "This __is__ a title" ) ) ;
1459+ . footer ( snippet:: Level :: Error . title ( "This __is__ a title" ) ) ;
14481460 let output = from_display_lines ( vec ! [
14491461 DisplayLine :: Raw ( DisplayRawLine :: Annotation {
14501462 annotation: Annotation {
0 commit comments