@@ -10,18 +10,27 @@ pub(crate) const INFO_TXT: &str = "info";
10
10
pub ( crate ) const NOTE_TXT : & str = "note" ;
11
11
pub ( crate ) const WARNING_TXT : & str = "warning" ;
12
12
13
+ /// Top-level user message
13
14
#[ derive( Debug ) ]
14
15
pub struct Message < ' a > {
15
16
pub ( crate ) id : Option < & ' a str > , // for "correctness", could be sloppy and be on Title
16
17
pub ( crate ) groups : Vec < Group < ' a > > ,
17
18
}
18
19
19
20
impl < ' a > Message < ' a > {
21
+ /// <div class="warning">
22
+ ///
23
+ /// Text passed to this function is considered "untrusted input", as such
24
+ /// all text is passed through a normalization function. Pre-styled text is
25
+ /// not allowed to be passed to this function.
26
+ ///
27
+ /// </div>
20
28
pub fn id ( mut self , id : & ' a str ) -> Self {
21
29
self . id = Some ( id) ;
22
30
self
23
31
}
24
32
33
+ /// Add an [`Element`] container
25
34
pub fn group ( mut self , group : Group < ' a > ) -> Self {
26
35
self . groups . push ( group) ;
27
36
self
@@ -66,6 +75,7 @@ impl<'a> Message<'a> {
66
75
}
67
76
}
68
77
78
+ /// An [`Element`] container
69
79
#[ derive( Debug ) ]
70
80
pub struct Group < ' a > {
71
81
pub ( crate ) elements : Vec < Element < ' a > > ,
@@ -97,6 +107,7 @@ impl<'a> Group<'a> {
97
107
}
98
108
}
99
109
110
+ /// A section of content within a [`Group`]
100
111
#[ derive( Debug ) ]
101
112
#[ non_exhaustive]
102
113
pub enum Element < ' a > {
@@ -137,9 +148,13 @@ impl From<Padding> for Element<'_> {
137
148
}
138
149
}
139
150
151
+ /// A whitespace [`Element`] in a [`Group`]
140
152
#[ derive( Debug ) ]
141
153
pub struct Padding ;
142
154
155
+ /// A text [`Element`] in a [`Group`]
156
+ ///
157
+ /// See [`Level::title`] to create this.
143
158
#[ derive( Debug ) ]
144
159
pub struct Title < ' a > {
145
160
pub ( crate ) level : Level < ' a > ,
@@ -154,6 +169,7 @@ impl Title<'_> {
154
169
}
155
170
}
156
171
172
+ /// A source view [`Element`] in a [`Group`]
157
173
#[ derive( Debug ) ]
158
174
pub struct Snippet < ' a , T > {
159
175
pub ( crate ) origin : Option < & ' a str > ,
@@ -164,9 +180,15 @@ pub struct Snippet<'a, T> {
164
180
}
165
181
166
182
impl < ' a , T : Clone > Snippet < ' a , T > {
183
+ /// The source code to be rendered
184
+ ///
185
+ /// <div class="warning">
186
+ ///
167
187
/// Text passed to this function is considered "untrusted input", as such
168
188
/// all text is passed through a normalization function. Pre-styled text is
169
189
/// not allowed to be passed to this function.
190
+ ///
191
+ /// </div>
170
192
pub fn source ( source : & ' a str ) -> Self {
171
193
Self {
172
194
origin : None ,
@@ -177,49 +199,65 @@ impl<'a, T: Clone> Snippet<'a, T> {
177
199
}
178
200
}
179
201
202
+ /// When manually [`fold`][Self::fold]ing,
203
+ /// the [`source`][Self::source]s line offset from the original start
180
204
pub fn line_start ( mut self , line_start : usize ) -> Self {
181
205
self . line_start = line_start;
182
206
self
183
207
}
184
208
209
+ /// The location of the [`source`][Self::source] (e.g. a path)
210
+ ///
211
+ /// <div class="warning">
212
+ ///
185
213
/// Text passed to this function is considered "untrusted input", as such
186
214
/// all text is passed through a normalization function. Pre-styled text is
187
215
/// not allowed to be passed to this function.
216
+ ///
217
+ /// </div>
188
218
pub fn origin ( mut self , origin : & ' a str ) -> Self {
189
219
self . origin = Some ( origin) ;
190
220
self
191
221
}
192
222
223
+ /// Hide lines without [`Annotation`]s
193
224
pub fn fold ( mut self , fold : bool ) -> Self {
194
225
self . fold = fold;
195
226
self
196
227
}
197
228
}
198
229
199
230
impl < ' a > Snippet < ' a , Annotation < ' a > > {
231
+ /// Highlight and describe a span of text within the [`source`][Self::source]
200
232
pub fn annotation ( mut self , annotation : Annotation < ' a > ) -> Snippet < ' a , Annotation < ' a > > {
201
233
self . markers . push ( annotation) ;
202
234
self
203
235
}
204
236
237
+ /// Highlight and describe spans of text within the [`source`][Self::source]
205
238
pub fn annotations ( mut self , annotation : impl IntoIterator < Item = Annotation < ' a > > ) -> Self {
206
239
self . markers . extend ( annotation) ;
207
240
self
208
241
}
209
242
}
210
243
211
244
impl < ' a > Snippet < ' a , Patch < ' a > > {
245
+ /// Suggest to the user an edit to the [`source`][Self::source]
212
246
pub fn patch ( mut self , patch : Patch < ' a > ) -> Snippet < ' a , Patch < ' a > > {
213
247
self . markers . push ( patch) ;
214
248
self
215
249
}
216
250
251
+ /// Suggest to the user edits to the [`source`][Self::source]
217
252
pub fn patches ( mut self , patches : impl IntoIterator < Item = Patch < ' a > > ) -> Self {
218
253
self . markers . extend ( patches) ;
219
254
self
220
255
}
221
256
}
222
257
258
+ /// Highlighted and describe a span of text within a [`Snippet`]
259
+ ///
260
+ /// See [`AnnotationKind`] to create an annotation.
223
261
#[ derive( Clone , Debug ) ]
224
262
pub struct Annotation < ' a > {
225
263
pub ( crate ) span : Range < usize > ,
@@ -229,20 +267,30 @@ pub struct Annotation<'a> {
229
267
}
230
268
231
269
impl < ' a > Annotation < ' a > {
270
+ /// Describe the reason the span is highlighted
271
+ ///
272
+ /// This will be styled according to the [`AnnotationKind`]
273
+ ///
274
+ /// <div class="warning">
275
+ ///
232
276
/// Text passed to this function is considered "untrusted input", as such
233
277
/// all text is passed through a normalization function. Pre-styled text is
234
278
/// not allowed to be passed to this function.
279
+ ///
280
+ /// </div>
235
281
pub fn label ( mut self , label : & ' a str ) -> Self {
236
282
self . label = Some ( label) ;
237
283
self
238
284
}
239
285
286
+ /// Style the source according to the [`AnnotationKind`]
240
287
pub fn highlight_source ( mut self , highlight_source : bool ) -> Self {
241
288
self . highlight_source = highlight_source;
242
289
self
243
290
}
244
291
}
245
292
293
+ /// The category of the [`Annotation`]
246
294
#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
247
295
pub enum AnnotationKind {
248
296
/// Color to [`Message`]'s [`Level`]
@@ -266,16 +314,23 @@ impl AnnotationKind {
266
314
}
267
315
}
268
316
317
+ /// Suggested edit to the [`Snippet`]
269
318
#[ derive( Clone , Debug ) ]
270
319
pub struct Patch < ' a > {
271
320
pub ( crate ) span : Range < usize > ,
272
321
pub ( crate ) replacement : & ' a str ,
273
322
}
274
323
275
324
impl < ' a > Patch < ' a > {
325
+ /// Splice `replacement` into the [`Snippet`] at the `span`
326
+ ///
327
+ /// <div class="warning">
328
+ ///
276
329
/// Text passed to this function is considered "untrusted input", as such
277
330
/// all text is passed through a normalization function. Pre-styled text is
278
331
/// not allowed to be passed to this function.
332
+ ///
333
+ /// </div>
279
334
pub fn new ( span : Range < usize > , replacement : & ' a str ) -> Self {
280
335
Self { span, replacement }
281
336
}
@@ -328,6 +383,7 @@ impl<'a> Patch<'a> {
328
383
}
329
384
}
330
385
386
+ /// The location of the [`Snippet`] (e.g. a path)
331
387
#[ derive( Clone , Debug ) ]
332
388
pub struct Origin < ' a > {
333
389
pub ( crate ) origin : & ' a str ,
@@ -338,9 +394,13 @@ pub struct Origin<'a> {
338
394
}
339
395
340
396
impl < ' a > Origin < ' a > {
397
+ /// <div class="warning">
398
+ ///
341
399
/// Text passed to this function is considered "untrusted input", as such
342
400
/// all text is passed through a normalization function. Pre-styled text is
343
401
/// not allowed to be passed to this function.
402
+ ///
403
+ /// </div>
344
404
pub fn new ( origin : & ' a str ) -> Self {
345
405
Self {
346
406
origin,
@@ -351,11 +411,17 @@ impl<'a> Origin<'a> {
351
411
}
352
412
}
353
413
414
+ /// Set the default line number to display
415
+ ///
416
+ /// Otherwise this will be inferred from the primary [`Annotation`]
354
417
pub fn line ( mut self , line : usize ) -> Self {
355
418
self . line = Some ( line) ;
356
419
self
357
420
}
358
421
422
+ /// Set the default column to display
423
+ ///
424
+ /// Otherwise this will be inferred from the primary [`Annotation`]
359
425
pub fn char_column ( mut self , char_column : usize ) -> Self {
360
426
self . char_column = Some ( char_column) ;
361
427
self
@@ -366,9 +432,15 @@ impl<'a> Origin<'a> {
366
432
self
367
433
}
368
434
435
+ /// Like [`Annotation::label`], but when there is no source
436
+ ///
437
+ /// <div class="warning">
438
+ ///
369
439
/// Text passed to this function is considered "untrusted input", as such
370
440
/// all text is passed through a normalization function. Pre-styled text is
371
441
/// not allowed to be passed to this function.
442
+ ///
443
+ /// </div>
372
444
pub fn label ( mut self , label : & ' a str ) -> Self {
373
445
self . label = Some ( label) ;
374
446
self
0 commit comments