@@ -85,11 +85,58 @@ impl<FileId> Label<FileId> {
8585 Label :: new ( LabelStyle :: Secondary , file_id, range)
8686 }
8787
88- /// Add a message to the diagnostic.
88+ /// Set the message for the diagnostic. The old message (if any) is discarded .
8989 pub fn with_message ( mut self , message : impl ToString ) -> Label < FileId > {
9090 self . message = message. to_string ( ) ;
9191 self
9292 }
93+
94+ /// Set the file id. The old file id (if any) is discarded.
95+ pub fn with_file < NewFileId > ( self , file_id : NewFileId ) -> Label < NewFileId > {
96+ Label {
97+ style : self . style ,
98+ file_id,
99+ range : self . range ,
100+ message : self . message ,
101+ }
102+ }
103+ }
104+
105+ // use a separate impl so we do not have to specify the type like this in e.g.
106+ // `primary_anon`:
107+ // ```
108+ // Label::<()>::new_anon(..)
109+ // ```
110+ impl Label < ( ) > {
111+ /// Create a new label without specifying a [`file_id`].
112+ ///
113+ /// [`file_id`]: Label::file_id
114+ pub fn new_anon ( style : LabelStyle , range : impl Into < Range < usize > > ) -> Label < ( ) > {
115+ Label {
116+ style,
117+ file_id : ( ) ,
118+ range : range. into ( ) ,
119+ message : String :: new ( ) ,
120+ }
121+ }
122+
123+ /// Create a new label with a style of [`LabelStyle::Primary`] and without
124+ /// specifying a [`file_id`].
125+ ///
126+ /// [`LabelStyle::Primary`]: LabelStyle::Primary
127+ /// [`file_id`]: Label::file_id
128+ pub fn primary_anon ( range : impl Into < Range < usize > > ) -> Label < ( ) > {
129+ Label :: new_anon ( LabelStyle :: Primary , range)
130+ }
131+
132+ /// Create a new label with a style of [`LabelStyle::Secondary`] and without
133+ /// specifying a [`file_id`].
134+ ///
135+ /// [`LabelStyle::Secondary`]: LabelStyle::Secondary
136+ /// [`file_id`]: Label::file_id
137+ pub fn secondary_anon ( range : impl Into < Range < usize > > ) -> Label < ( ) > {
138+ Label :: new_anon ( LabelStyle :: Secondary , range)
139+ }
93140}
94141
95142/// Represents a diagnostic message that can provide information like errors and
@@ -188,4 +235,20 @@ impl<FileId> Diagnostic<FileId> {
188235 self . notes . append ( & mut notes) ;
189236 self
190237 }
238+
239+ /// Set the file id for all labels in this Diagnostic by calling
240+ /// [`Label::with_file`] on each label.
241+ pub fn with_file < NewFileId : Clone > ( mut self , file_id : NewFileId ) -> Diagnostic < NewFileId > {
242+ Diagnostic {
243+ severity : self . severity ,
244+ code : self . code ,
245+ message : self . message ,
246+ labels : self
247+ . labels
248+ . drain ( ..)
249+ . map ( |label| label. with_file ( file_id. clone ( ) ) )
250+ . collect ( ) ,
251+ notes : self . notes ,
252+ }
253+ }
191254}
0 commit comments