@@ -9,6 +9,9 @@ use std::{
9
9
panic:: Location ,
10
10
} ;
11
11
12
+ #[ cfg( feature = "serde" ) ]
13
+ use serde:: { Deserialize , Serialize } ;
14
+
12
15
use crate :: MietteError ;
13
16
14
17
/// Adds rich metadata to your Error that can be used by
@@ -163,6 +166,7 @@ impl From<Box<dyn std::error::Error + Send + Sync>> for Box<dyn Diagnostic + Sen
163
166
[`Diagnostic`]s are displayed. Defaults to [`Severity::Error`].
164
167
*/
165
168
#[ derive( Copy , Clone , Debug , Eq , Ord , PartialEq , PartialOrd ) ]
169
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
166
170
pub enum Severity {
167
171
/// Just some help. Here's how you could be doing it better.
168
172
Advice ,
@@ -179,6 +183,31 @@ impl Default for Severity {
179
183
}
180
184
}
181
185
186
+ #[ cfg( feature = "serde" ) ]
187
+ #[ test]
188
+ fn test_serialize_severity ( ) {
189
+ use serde_json:: json;
190
+
191
+ assert_eq ! ( json!( Severity :: Advice ) , json!( "Advice" ) ) ;
192
+ assert_eq ! ( json!( Severity :: Warning ) , json!( "Warning" ) ) ;
193
+ assert_eq ! ( json!( Severity :: Error ) , json!( "Error" ) ) ;
194
+ }
195
+
196
+ #[ cfg( feature = "serde" ) ]
197
+ #[ test]
198
+ fn test_deserialize_severity ( ) {
199
+ use serde_json:: json;
200
+
201
+ let severity: Severity = serde_json:: from_value ( json ! ( "Advice" ) ) . unwrap ( ) ;
202
+ assert_eq ! ( severity, Severity :: Advice ) ;
203
+
204
+ let severity: Severity = serde_json:: from_value ( json ! ( "Warning" ) ) . unwrap ( ) ;
205
+ assert_eq ! ( severity, Severity :: Warning ) ;
206
+
207
+ let severity: Severity = serde_json:: from_value ( json ! ( "Error" ) ) . unwrap ( ) ;
208
+ assert_eq ! ( severity, Severity :: Error ) ;
209
+ }
210
+
182
211
/**
183
212
Represents readable source code of some sort.
184
213
@@ -203,14 +232,16 @@ pub trait SourceCode: Send + Sync {
203
232
204
233
/// A labeled [`SourceSpan`].
205
234
#[ derive( Debug , Clone , PartialEq , Eq ) ]
235
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
206
236
pub struct LabeledSpan {
237
+ #[ cfg_attr( feature = "serde" , serde( skip_serializing_if = "Option::is_none" ) ) ]
207
238
label : Option < String > ,
208
239
span : SourceSpan ,
209
240
}
210
241
211
242
impl LabeledSpan {
212
243
/// Makes a new labeled span.
213
- pub fn new ( label : Option < String > , offset : ByteOffset , len : ByteOffset ) -> Self {
244
+ pub fn new ( label : Option < String > , offset : ByteOffset , len : usize ) -> Self {
214
245
Self {
215
246
label,
216
247
span : ( offset, len) . into ( ) ,
@@ -299,6 +330,53 @@ impl LabeledSpan {
299
330
}
300
331
}
301
332
333
+ #[ cfg( feature = "serde" ) ]
334
+ #[ test]
335
+ fn test_serialize_labeled_span ( ) {
336
+ use serde_json:: json;
337
+
338
+ assert_eq ! (
339
+ json!( LabeledSpan :: new( None , 0 , 0 ) ) ,
340
+ json!( {
341
+ "span" : { "offset" : 0 , "length" : 0 }
342
+ } )
343
+ ) ;
344
+
345
+ assert_eq ! (
346
+ json!( LabeledSpan :: new( Some ( "label" . to_string( ) ) , 0 , 0 ) ) ,
347
+ json!( {
348
+ "label" : "label" ,
349
+ "span" : { "offset" : 0 , "length" : 0 }
350
+ } )
351
+ )
352
+ }
353
+
354
+ #[ cfg( feature = "serde" ) ]
355
+ #[ test]
356
+ fn test_deserialize_labeled_span ( ) {
357
+ use serde_json:: json;
358
+
359
+ let span: LabeledSpan = serde_json:: from_value ( json ! ( {
360
+ "label" : null,
361
+ "span" : { "offset" : 0 , "length" : 0 }
362
+ } ) )
363
+ . unwrap ( ) ;
364
+ assert_eq ! ( span, LabeledSpan :: new( None , 0 , 0 ) ) ;
365
+
366
+ let span: LabeledSpan = serde_json:: from_value ( json ! ( {
367
+ "span" : { "offset" : 0 , "length" : 0 }
368
+ } ) )
369
+ . unwrap ( ) ;
370
+ assert_eq ! ( span, LabeledSpan :: new( None , 0 , 0 ) ) ;
371
+
372
+ let span: LabeledSpan = serde_json:: from_value ( json ! ( {
373
+ "label" : "label" ,
374
+ "span" : { "offset" : 0 , "length" : 0 }
375
+ } ) )
376
+ . unwrap ( ) ;
377
+ assert_eq ! ( span, LabeledSpan :: new( Some ( "label" . to_string( ) ) , 0 , 0 ) )
378
+ }
379
+
302
380
/**
303
381
Contents of a [`SourceCode`] covered by [`SourceSpan`].
304
382
@@ -402,23 +480,22 @@ impl<'a> SpanContents<'a> for MietteSpanContents<'a> {
402
480
}
403
481
}
404
482
405
- /**
406
- Span within a [`SourceCode`] with an associated message.
407
- */
483
+ /// Span within a [`SourceCode`]
408
484
#[ derive( Clone , Copy , Debug , Eq , PartialEq , Hash ) ]
485
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
409
486
pub struct SourceSpan {
410
487
/// The start of the span.
411
488
offset : SourceOffset ,
412
- /// The total length of the span. Think of this as an offset from `start`.
413
- length : SourceOffset ,
489
+ /// The total length of the span
490
+ length : usize ,
414
491
}
415
492
416
493
impl SourceSpan {
417
494
/// Create a new [`SourceSpan`].
418
495
pub fn new ( start : SourceOffset , length : SourceOffset ) -> Self {
419
496
Self {
420
497
offset : start,
421
- length,
498
+ length : length . offset ( ) ,
422
499
}
423
500
}
424
501
@@ -429,61 +506,75 @@ impl SourceSpan {
429
506
430
507
/// Total length of the [`SourceSpan`], in bytes.
431
508
pub fn len ( & self ) -> usize {
432
- self . length . offset ( )
509
+ self . length
433
510
}
434
511
435
512
/// Whether this [`SourceSpan`] has a length of zero. It may still be useful
436
513
/// to point to a specific point.
437
514
pub fn is_empty ( & self ) -> bool {
438
- self . length . offset ( ) == 0
515
+ self . length == 0
439
516
}
440
517
}
441
518
442
- impl From < ( ByteOffset , ByteOffset ) > for SourceSpan {
443
- fn from ( ( start, len) : ( ByteOffset , ByteOffset ) ) -> Self {
519
+ impl From < ( ByteOffset , usize ) > for SourceSpan {
520
+ fn from ( ( start, len) : ( ByteOffset , usize ) ) -> Self {
444
521
Self {
445
522
offset : start. into ( ) ,
446
- length : len. into ( ) ,
523
+ length : len,
447
524
}
448
525
}
449
526
}
450
527
451
528
impl From < ( SourceOffset , SourceOffset ) > for SourceSpan {
452
529
fn from ( ( start, len) : ( SourceOffset , SourceOffset ) ) -> Self {
453
- Self {
454
- offset : start,
455
- length : len,
456
- }
530
+ Self :: new ( start, len)
457
531
}
458
532
}
459
533
460
534
impl From < std:: ops:: Range < ByteOffset > > for SourceSpan {
461
535
fn from ( range : std:: ops:: Range < ByteOffset > ) -> Self {
462
536
Self {
463
537
offset : range. start . into ( ) ,
464
- length : range. len ( ) . into ( ) ,
538
+ length : range. len ( ) ,
465
539
}
466
540
}
467
541
}
468
542
469
543
impl From < SourceOffset > for SourceSpan {
470
544
fn from ( offset : SourceOffset ) -> Self {
471
- Self {
472
- offset,
473
- length : 0 . into ( ) ,
474
- }
545
+ Self { offset, length : 0 }
475
546
}
476
547
}
477
548
478
549
impl From < ByteOffset > for SourceSpan {
479
550
fn from ( offset : ByteOffset ) -> Self {
480
551
Self {
481
552
offset : offset. into ( ) ,
482
- length : 0 . into ( ) ,
553
+ length : 0 ,
483
554
}
484
555
}
485
556
}
486
557
558
+ #[ cfg( feature = "serde" ) ]
559
+ #[ test]
560
+ fn test_serialize_source_span ( ) {
561
+ use serde_json:: json;
562
+
563
+ assert_eq ! (
564
+ json!( SourceSpan :: from( 0 ) ) ,
565
+ json!( { "offset" : 0 , "length" : 0 } )
566
+ )
567
+ }
568
+
569
+ #[ cfg( feature = "serde" ) ]
570
+ #[ test]
571
+ fn test_deserialize_source_span ( ) {
572
+ use serde_json:: json;
573
+
574
+ let span: SourceSpan = serde_json:: from_value ( json ! ( { "offset" : 0 , "length" : 0 } ) ) . unwrap ( ) ;
575
+ assert_eq ! ( span, SourceSpan :: from( 0 ) )
576
+ }
577
+
487
578
/**
488
579
"Raw" type for the byte offset from the beginning of a [`SourceCode`].
489
580
*/
@@ -493,6 +584,7 @@ pub type ByteOffset = usize;
493
584
Newtype that represents the [`ByteOffset`] from the beginning of a [`SourceCode`]
494
585
*/
495
586
#[ derive( Clone , Copy , Debug , Eq , PartialEq , Hash ) ]
587
+ #[ cfg_attr( feature = "serde" , derive( Serialize , Deserialize ) ) ]
496
588
pub struct SourceOffset ( ByteOffset ) ;
497
589
498
590
impl SourceOffset {
@@ -576,3 +668,18 @@ fn test_source_offset_from_location() {
576
668
source. len( )
577
669
) ;
578
670
}
671
+
672
+ #[ cfg( feature = "serde" ) ]
673
+ #[ test]
674
+ fn test_serialize_source_offset ( ) {
675
+ use serde_json:: json;
676
+
677
+ assert_eq ! ( json!( SourceOffset :: from( 0 ) ) , 0 )
678
+ }
679
+
680
+ #[ cfg( feature = "serde" ) ]
681
+ #[ test]
682
+ fn test_deserialize_source_offset ( ) {
683
+ let offset: SourceOffset = serde_json:: from_str ( "0" ) . unwrap ( ) ;
684
+ assert_eq ! ( offset, SourceOffset :: from( 0 ) )
685
+ }
0 commit comments