@@ -18,14 +18,42 @@ use std::hash::{Hash, Hasher};
1818#[ derive( Clone , Debug , PartialEq , Eq , Hash , Encodable , Decodable ) ]
1919pub struct SuggestionsDisabled ;
2020
21+ /// Abstraction over a message in a diagnostic to support both translatable and non-translatable
22+ /// diagnostic messages.
23+ #[ derive( Clone , Debug , PartialEq , Eq , Hash , Encodable , Decodable ) ]
24+ pub enum DiagnosticMessage {
25+ /// Non-translatable diagnostic message.
26+ Str ( String ) ,
27+ /// Identifier for a Fluent message corresponding to the diagnostic message.
28+ FluentIdentifier ( String ) ,
29+ }
30+
31+ impl DiagnosticMessage {
32+ /// Convert `DiagnosticMessage` to a `&str`.
33+ pub fn as_str ( & self ) -> & str {
34+ match self {
35+ DiagnosticMessage :: Str ( msg) => msg,
36+ DiagnosticMessage :: FluentIdentifier ( ..) => unimplemented ! ( ) ,
37+ }
38+ }
39+
40+ /// Convert `DiagnosticMessage` to an owned `String`.
41+ pub fn to_string ( self ) -> String {
42+ match self {
43+ DiagnosticMessage :: Str ( msg) => msg,
44+ DiagnosticMessage :: FluentIdentifier ( ..) => unimplemented ! ( ) ,
45+ }
46+ }
47+ }
48+
2149#[ must_use]
2250#[ derive( Clone , Debug , Encodable , Decodable ) ]
2351pub struct Diagnostic {
2452 // NOTE(eddyb) this is private to disallow arbitrary after-the-fact changes,
2553 // outside of what methods in this crate themselves allow.
2654 crate level : Level ,
2755
28- pub message : Vec < ( String , Style ) > ,
56+ pub message : Vec < ( DiagnosticMessage , Style ) > ,
2957 pub code : Option < DiagnosticId > ,
3058 pub span : MultiSpan ,
3159 pub children : Vec < SubDiagnostic > ,
@@ -52,7 +80,7 @@ pub enum DiagnosticId {
5280#[ derive( Clone , Debug , PartialEq , Hash , Encodable , Decodable ) ]
5381pub struct SubDiagnostic {
5482 pub level : Level ,
55- pub message : Vec < ( String , Style ) > ,
83+ pub message : Vec < ( DiagnosticMessage , Style ) > ,
5684 pub span : MultiSpan ,
5785 pub render_span : Option < MultiSpan > ,
5886}
@@ -112,7 +140,7 @@ impl Diagnostic {
112140 pub fn new_with_code ( level : Level , code : Option < DiagnosticId > , message : & str ) -> Self {
113141 Diagnostic {
114142 level,
115- message : vec ! [ ( message. to_owned( ) , Style :: NoStyle ) ] ,
143+ message : vec ! [ ( DiagnosticMessage :: Str ( message. to_owned( ) ) , Style :: NoStyle ) ] ,
116144 code,
117145 span : MultiSpan :: new ( ) ,
118146 children : vec ! [ ] ,
@@ -465,7 +493,7 @@ impl Diagnostic {
465493 . map( |( span, snippet) | SubstitutionPart { snippet, span } )
466494 . collect( ) ,
467495 } ] ,
468- msg : msg. to_owned ( ) ,
496+ msg : DiagnosticMessage :: Str ( msg. to_owned ( ) ) ,
469497 style,
470498 applicability,
471499 tool_metadata : Default :: default ( ) ,
@@ -493,7 +521,7 @@ impl Diagnostic {
493521 . map( |( span, snippet) | SubstitutionPart { snippet, span } )
494522 . collect( ) ,
495523 } ] ,
496- msg : msg. to_owned ( ) ,
524+ msg : DiagnosticMessage :: Str ( msg. to_owned ( ) ) ,
497525 style : SuggestionStyle :: CompletelyHidden ,
498526 applicability,
499527 tool_metadata : Default :: default ( ) ,
@@ -548,7 +576,7 @@ impl Diagnostic {
548576 substitutions : vec ! [ Substitution {
549577 parts: vec![ SubstitutionPart { snippet: suggestion, span: sp } ] ,
550578 } ] ,
551- msg : msg. to_owned ( ) ,
579+ msg : DiagnosticMessage :: Str ( msg. to_owned ( ) ) ,
552580 style,
553581 applicability,
554582 tool_metadata : Default :: default ( ) ,
@@ -591,7 +619,7 @@ impl Diagnostic {
591619 . collect ( ) ;
592620 self . push_suggestion ( CodeSuggestion {
593621 substitutions,
594- msg : msg. to_owned ( ) ,
622+ msg : DiagnosticMessage :: Str ( msg. to_owned ( ) ) ,
595623 style : SuggestionStyle :: ShowCode ,
596624 applicability,
597625 tool_metadata : Default :: default ( ) ,
@@ -616,7 +644,7 @@ impl Diagnostic {
616644 . collect ( ) ,
617645 } )
618646 . collect ( ) ,
619- msg : msg. to_owned ( ) ,
647+ msg : DiagnosticMessage :: Str ( msg. to_owned ( ) ) ,
620648 style : SuggestionStyle :: ShowCode ,
621649 applicability,
622650 tool_metadata : Default :: default ( ) ,
@@ -698,7 +726,7 @@ impl Diagnostic {
698726 ) {
699727 self . push_suggestion ( CodeSuggestion {
700728 substitutions : vec ! [ ] ,
701- msg : msg. to_owned ( ) ,
729+ msg : DiagnosticMessage :: Str ( msg. to_owned ( ) ) ,
702730 style : SuggestionStyle :: CompletelyHidden ,
703731 applicability,
704732 tool_metadata : ToolMetadata :: new ( tool_metadata) ,
@@ -733,15 +761,15 @@ impl Diagnostic {
733761 }
734762
735763 pub fn set_primary_message < M : Into < String > > ( & mut self , msg : M ) -> & mut Self {
736- self . message [ 0 ] = ( msg. into ( ) , Style :: NoStyle ) ;
764+ self . message [ 0 ] = ( DiagnosticMessage :: Str ( msg. into ( ) ) , Style :: NoStyle ) ;
737765 self
738766 }
739767
740- pub fn message ( & self ) -> String {
741- self . message . iter ( ) . map ( |i| i. 0 . as_str ( ) ) . collect :: < String > ( )
768+ pub fn message ( & self ) -> DiagnosticMessage {
769+ DiagnosticMessage :: Str ( self . message . iter ( ) . map ( |i| i. 0 . as_str ( ) ) . collect :: < String > ( ) )
742770 }
743771
744- pub fn styled_message ( & self ) -> & Vec < ( String , Style ) > {
772+ pub fn styled_message ( & self ) -> & Vec < ( DiagnosticMessage , Style ) > {
745773 & self . message
746774 }
747775
@@ -758,7 +786,7 @@ impl Diagnostic {
758786 ) {
759787 let sub = SubDiagnostic {
760788 level,
761- message : vec ! [ ( message. to_owned( ) , Style :: NoStyle ) ] ,
789+ message : vec ! [ ( DiagnosticMessage :: Str ( message. to_owned( ) ) , Style :: NoStyle ) ] ,
762790 span,
763791 render_span,
764792 } ;
@@ -770,10 +798,11 @@ impl Diagnostic {
770798 fn sub_with_highlights (
771799 & mut self ,
772800 level : Level ,
773- message : Vec < ( String , Style ) > ,
801+ mut message : Vec < ( String , Style ) > ,
774802 span : MultiSpan ,
775803 render_span : Option < MultiSpan > ,
776804 ) {
805+ let message = message. drain ( ..) . map ( |m| ( DiagnosticMessage :: Str ( m. 0 ) , m. 1 ) ) . collect ( ) ;
777806 let sub = SubDiagnostic { level, message, span, render_span } ;
778807 self . children . push ( sub) ;
779808 }
@@ -783,7 +812,7 @@ impl Diagnostic {
783812 & self ,
784813 ) -> (
785814 & Level ,
786- & Vec < ( String , Style ) > ,
815+ & Vec < ( DiagnosticMessage , Style ) > ,
787816 & Option < DiagnosticId > ,
788817 & MultiSpan ,
789818 & Result < Vec < CodeSuggestion > , SuggestionsDisabled > ,
@@ -816,11 +845,11 @@ impl PartialEq for Diagnostic {
816845}
817846
818847impl SubDiagnostic {
819- pub fn message ( & self ) -> String {
820- self . message . iter ( ) . map ( |i| i. 0 . as_str ( ) ) . collect :: < String > ( )
848+ pub fn message ( & self ) -> DiagnosticMessage {
849+ DiagnosticMessage :: Str ( self . message . iter ( ) . map ( |i| i. 0 . as_str ( ) ) . collect :: < String > ( ) )
821850 }
822851
823- pub fn styled_message ( & self ) -> & Vec < ( String , Style ) > {
852+ pub fn styled_message ( & self ) -> & Vec < ( DiagnosticMessage , Style ) > {
824853 & self . message
825854 }
826855}
0 commit comments