error_snippet 0.2.0

Library for reporting fancy diagnostics to the console
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
use std::fmt::Display;
use std::ops::Range;
use std::sync::Arc;

pub mod handler;
pub mod render;
pub mod source;

pub use crate::handler::*;
pub use crate::render::*;
pub use crate::source::*;

pub type Error = Box<dyn Diagnostic + Send + Sync>;

pub type Result<T> = std::result::Result<T, Error>;

/// Diagnostic severity level.
///
/// Intended to be used by the reporter to change how the diagnostic is displayed.
/// Diagnostics of [`Error`] or higher also cause the reporter to halt upon draining.
#[derive(Default, Debug, Clone, Copy, PartialEq, Eq)]
pub enum Severity {
    /// Failure. Program cannot continue.
    #[default]
    Error,

    /// Warning. Program can continue but may be affected.
    Warning,

    /// Information. Program can continue and may be unaffected.
    Info,

    /// Note. Has no effect on the program, but may provide additional context.
    Note,

    /// Help. Has no effect on the program, but may provide extra help and tips.
    Help,
}

impl std::fmt::Display for Severity {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Severity::Error => f.write_str("error"),
            Severity::Warning => f.write_str("warning"),
            Severity::Info => f.write_str("info"),
            Severity::Note => f.write_str("note"),
            Severity::Help => f.write_str("help"),
        }
    }
}

/// Defines some span within a [`Source`] instance.
///
/// The range within the span is an absolute zero-indexed range of characters within the source file.
/// It is not a line-column representation and does not provide information about the line and column numbers.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct SpanRange(pub Range<usize>);

impl From<Range<usize>> for SpanRange {
    fn from(range: Range<usize>) -> SpanRange {
        SpanRange(Range {
            start: range.start,
            end: range.end,
        })
    }
}

impl From<SpanRange> for Range<usize> {
    fn from(span: SpanRange) -> Range<usize> {
        span.0
    }
}

impl std::fmt::Display for SpanRange {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.0)
    }
}

/// Defines some location with a [`Source`] instance.
///
/// The location within the structure is an absolute zero-indexed position of characters within the source file.
/// It is not a line-column representation and does not provide information about the line and column numbers.
#[derive(Debug, Clone)]
pub struct SourceLocation {
    /// Defines the source which the range is referring to.
    source: Arc<dyn Source>,

    /// Defines the character offset into the file.
    offset: usize,
}

impl SourceLocation {
    /// Creates a new [`SourceLocation`] with the given source and offset.
    pub fn new(source: Arc<dyn Source>, offset: usize) -> Self {
        Self { source, offset }
    }
}

impl PartialEq for SourceLocation {
    fn eq(&self, other: &Self) -> bool {
        self.source.name() == other.source.name()
            && self.source.content() == other.source.content()
            && self.offset == other.offset
    }
}

impl std::cmp::Eq for SourceLocation {}

impl PartialOrd for SourceLocation {
    fn partial_cmp(&self, other: &SourceLocation) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl std::cmp::Ord for SourceLocation {
    fn cmp(&self, other: &SourceLocation) -> std::cmp::Ordering {
        let other_offset = other.offset;

        match self.offset {
            v if v < other_offset => std::cmp::Ordering::Less,
            v if v > other_offset => std::cmp::Ordering::Greater,
            _ => std::cmp::Ordering::Equal,
        }
    }
}

/// Defines some span with a [`Source`] instance.
///
/// The range within the span is an absolute zero-indexed range of characters within the source file.
/// It is not a line-column representation and does not provide information about the line and column numbers.
#[derive(Debug, Clone)]
pub struct SourceRange {
    /// Defines the source which the range is referring to.
    source: Arc<dyn Source>,

    /// Defines the underlying span.
    span: SpanRange,
}

impl SourceRange {
    /// Creates a new [`SourceRange`] with the given source and span.
    pub fn new(source: Arc<dyn Source>, span: impl Into<SpanRange>) -> Self {
        Self {
            source,
            span: span.into(),
        }
    }
}

impl PartialEq for SourceRange {
    fn eq(&self, other: &Self) -> bool {
        self.source.name() == other.source.name()
            && self.source.content() == other.source.content()
            && self.span == other.span
    }
}

impl std::cmp::Eq for SourceRange {}

impl PartialOrd for SourceRange {
    fn partial_cmp(&self, other: &SourceRange) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl std::cmp::Ord for SourceRange {
    fn cmp(&self, other: &SourceRange) -> std::cmp::Ordering {
        let other_start = other.span.0.start;

        match self.span.0.start {
            v if v < other_start => std::cmp::Ordering::Less,
            v if v > other_start => std::cmp::Ordering::Greater,
            _ => std::cmp::Ordering::Equal,
        }
    }
}

/// Represents a labelled span of some source code.
///
/// Each label is meant to be used as a snippet within a larger source code. It provides
/// a way to highlight a specific portion of the source code, and uses labels to provide
/// additional information about the span.
#[derive(Debug, Clone)]
pub struct Label {
    /// Defines the actual label to print on the snippet.
    message: String,

    /// Defines the source span where the label should be placed.
    ///
    /// If this method returns `None`, the parent diagnostic is expected to have
    /// a source attached via the [`Diagnostic::source_code()`] method.
    source: Option<Arc<dyn Source>>,

    /// Defines the index range where the label should be placed.
    range: SpanRange,

    /// Defines the severity of the label, which can be independant from the parent diagnostic.
    severity: Option<Severity>,
}

impl PartialEq for Label {
    fn eq(&self, other: &Label) -> bool {
        self.message == other.message && self.range == other.range
    }
}

impl Eq for Label {}

impl Label {
    /// Creates a new [`Label`] from the given source, range, and label.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'");
    ///
    /// assert_eq!(label.message(), "could not find method 'invok'");
    /// assert_eq!(label.severity(), None);
    /// ```
    pub fn new(source: Option<Arc<dyn Source>>, range: impl Into<SpanRange>, message: impl Into<String>) -> Self {
        Self {
            source,
            range: range.into(),
            message: message.into(),
            severity: None,
        }
    }

    /// Creates a new [`Label`] from the given source, range, and label, with a
    /// severity of [`Severity::Error`].
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::error(Some(source.clone()), 60..65, "could not find method 'invok'");
    ///
    /// assert_eq!(label.message(), "could not find method 'invok'");
    /// assert_eq!(label.severity(), Some(Severity::Error));
    /// ```
    pub fn error(source: Option<Arc<dyn Source>>, range: impl Into<SpanRange>, label: impl Into<String>) -> Self {
        Self {
            source,
            range: range.into(),
            message: label.into(),
            severity: Some(Severity::Error),
        }
    }

    /// Creates a new [`Label`] from the given source, range, and label, with a
    /// severity of [`Severity::Warning`].
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::warning(Some(source.clone()), 60..65, "could not find method 'invok'");
    ///
    /// assert_eq!(label.message(), "could not find method 'invok'");
    /// assert_eq!(label.severity(), Some(Severity::Warning));
    /// ```
    pub fn warning(source: Option<Arc<dyn Source>>, range: impl Into<SpanRange>, label: impl Into<String>) -> Self {
        Self {
            source,
            range: range.into(),
            message: label.into(),
            severity: Some(Severity::Warning),
        }
    }

    /// Creates a new [`Label`] from the given source, range, and label, with a
    /// severity of [`Severity::Info`].
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::info(Some(source.clone()), 60..65, "could not find method 'invok'");
    ///
    /// assert_eq!(label.message(), "could not find method 'invok'");
    /// assert_eq!(label.severity(), Some(Severity::Info));
    /// ```
    pub fn info(source: Option<Arc<dyn Source>>, range: impl Into<SpanRange>, label: impl Into<String>) -> Self {
        Self {
            source,
            range: range.into(),
            message: label.into(),
            severity: Some(Severity::Info),
        }
    }

    /// Creates a new [`Label`] from the given source, range, and label, with a
    /// severity of [`Severity::Note`].
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::note(Some(source.clone()), 60..65, "could not find method 'invok'");
    ///
    /// assert_eq!(label.message(), "could not find method 'invok'");
    /// assert_eq!(label.severity(), Some(Severity::Note));
    /// ```
    pub fn note(source: Option<Arc<dyn Source>>, range: impl Into<SpanRange>, label: impl Into<String>) -> Self {
        Self {
            source,
            range: range.into(),
            message: label.into(),
            severity: Some(Severity::Note),
        }
    }

    /// Creates a new [`Label`] from the given source, range, and label, with a
    /// severity of [`Severity::Help`].
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::help(Some(source.clone()), 60..65, "could not find method 'invok'");
    ///
    /// assert_eq!(label.message(), "could not find method 'invok'");
    /// assert_eq!(label.severity(), Some(Severity::Help));
    /// ```
    pub fn help(source: Option<Arc<dyn Source>>, range: impl Into<SpanRange>, label: impl Into<String>) -> Self {
        Self {
            source,
            range: range.into(),
            message: label.into(),
            severity: Some(Severity::Help),
        }
    }

    /// Gets the message of the current label instance.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'");
    ///
    /// assert_eq!(label.message(), "could not find method 'invok'");
    /// ```
    pub fn message(&self) -> &str {
        &self.message
    }

    /// Gets the integer span of the current label instance.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity, SpanRange};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'");
    ///
    /// assert_eq!(label.range(), &SpanRange(60..65));
    /// ```
    pub fn range(&self) -> &SpanRange {
        &self.range
    }

    /// Gets the source code of the current label instance.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity, Source, SpanRange};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'");
    ///
    /// assert_eq!(label.source().unwrap().name(), source.name());
    /// assert_eq!(label.source().unwrap().content(), source.content());
    /// ```
    pub fn source(&self) -> Option<Arc<dyn Source>> {
        self.source.clone()
    }

    /// Gets the severity of the current label instance.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'")
    ///     .with_severity(Severity::Warning);
    ///
    /// assert_eq!(label.severity(), Some(Severity::Warning));
    /// ```
    pub fn severity(&self) -> Option<Severity> {
        self.severity
    }

    /// Sets the severity for the current label instance.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'")
    ///     .with_severity(Severity::Warning);
    ///
    /// assert_eq!(label.message(), "could not find method 'invok'");
    /// assert_eq!(label.severity(), Some(Severity::Warning));
    /// ```
    pub fn with_severity(mut self, severity: Severity) -> Self {
        self.severity = Some(severity);
        self
    }

    /// Reads a span of the source using the range within the
    /// label itself, including a dynamic amount of context lines.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Label, Severity};
    ///
    /// let source = Arc::new(r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return 0;
    /// }"#);
    ///
    /// let label = Label::new(Some(source.clone()), 58..67, String::new());
    /// let span = label.read_span(None, 0).unwrap();
    ///
    /// assert_eq!(span.data, "    let b = a.invok();");
    /// assert_eq!(span.start_line, 2);
    /// assert_eq!(span.line, 2);
    /// ```
    pub fn read_span(&self, diagnostic: Option<&dyn Diagnostic>, context_lines: usize) -> Option<LabelSpan> {
        let diag_source = diagnostic.and_then(|d| d.source_code());
        let source = self.source.clone().or(diag_source)?;

        let content = source.content();
        let range = self.range().0.clone();

        let mut line_start = 0;
        let mut line_spans = Vec::new();

        for line in content.lines() {
            let line_len = line.len();
            let span = line_start..(line_start + line_len);

            line_spans.push(span);

            // +1 for '\n' (assuming UNIX-style newlines)
            line_start += line_len + 1;
        }

        // Determine the lines that intersect with the byte range
        let mut matching_lines = Vec::new();
        for (i, span) in line_spans.iter().enumerate() {
            if span.end > range.start && span.start < range.end {
                matching_lines.push(i);
            }
        }

        // If the range is outside the span of the input string,
        // we return the first context window of the string as a fallback.
        if matching_lines.is_empty() {
            // Get the end of the context window, if possible.
            // Otherwise, just return the entire string.
            let last_line_span = line_spans.get(context_lines * 2 + 1).or_else(|| line_spans.last());

            let last_line_idx = last_line_span.map(|s| s.end).unwrap_or_default();

            return Some(LabelSpan {
                data: content[0..last_line_idx].to_string(),
                start_line: context_lines,
                line: 0,
            });
        }

        let first_matching_line = *matching_lines.first().unwrap();

        let first_match = first_matching_line.saturating_sub(context_lines);
        let last_match = (matching_lines.last().unwrap() + context_lines).min(line_spans.len() - 1);

        let start_byte = line_spans[first_match].start;
        let end_byte = line_spans[last_match].end;

        Some(LabelSpan {
            data: content[start_byte..end_byte].to_string(),
            start_line: first_matching_line,
            line: first_match,
        })
    }
}

/// Represents a span within a label.
#[derive(Default, Debug, Clone, PartialEq, Eq)]
pub struct LabelSpan {
    /// Defines the string inside the associated span.
    pub data: String,

    /// Defines the zero-indexed line in the associated source, where the span
    /// starts (including context lines).
    pub line: usize,

    /// Defines the zero-indexed line in the associated source, where the span
    /// starts (excluding context lines).
    pub start_line: usize,
}

impl LabelSpan {
    /// Gets the line count in the span.
    pub fn line_count(&self) -> usize {
        self.data.lines().count()
    }
}

/// Represents a suggested fix with a source file attached.
///
/// Suggestions can guide the user to change some part of the source code,
/// in order to fix diagnostics.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum Suggestion {
    /// Defines some span within a file should be deleted.
    Deletion { range: SourceRange },

    /// Defines some string should be inserted at some position within a file.
    Insertion { location: SourceLocation, value: String },

    /// Defines some span within a file should be replaced.
    Replacement { range: SourceRange, replacement: String },
}

impl Suggestion {
    /// Creates a new [`Suggestion`] where a certain span within
    /// a file should be deleted.
    pub fn delete(range: SourceRange) -> Self {
        Self::Deletion { range }
    }

    /// Creates a new [`Suggestion`] where a certain location
    /// should have a string value inserted.
    pub fn insert(location: SourceLocation, value: impl Into<String>) -> Self {
        Self::Insertion {
            location,
            value: value.into(),
        }
    }

    /// Creates a new [`Suggestion`] where a certain span should
    /// be replaced with a string value.
    pub fn replace(range: SourceRange, replacement: impl Into<String>) -> Self {
        Self::Replacement {
            range,
            replacement: replacement.into(),
        }
    }

    /// Gets the source file of the suggestion.
    pub fn source(&self) -> Arc<dyn Source> {
        match self {
            Suggestion::Deletion { range, .. } => range.source.clone(),
            Suggestion::Insertion { location, .. } => location.source.clone(),
            Suggestion::Replacement { range, .. } => range.source.clone(),
        }
    }

    /// Gets the span which the suggestion refers to.
    ///
    /// All suggestion types, except insertions, returns the inner span directly,
    /// where-as insertions will create a new span with a distance of 1.
    pub fn span(&self) -> Range<usize> {
        match self {
            Suggestion::Replacement { range, .. } => range.span.0.clone(),
            Suggestion::Deletion { range, .. } => range.span.0.clone(),
            Suggestion::Insertion { location, .. } => location.offset..location.offset + 1,
        }
    }
}

/// Represents a help message, which can be attached to diagnostics to aid users.
///
/// Each help message is accompanied by zero-or-more suggestions, which can guide the user
/// to change some part of the source code, in order to fix the diagnostic.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Help {
    /// Defines the actual message to print in the footer.
    pub message: String,

    /// A list of zero-or-more suggestions to apply to the original source code.
    pub suggestions: Vec<Suggestion>,
}

impl Help {
    /// Creates a new [`Help`] with the given message.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::Help;
    ///
    /// let help = Help::new("have you checked your syntax?");
    ///
    /// assert_eq!(help.message, "have you checked your syntax?");
    /// assert_eq!(help.suggestions, vec![]);
    /// ```
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            suggestions: Vec::new(),
        }
    }

    /// Adds the given suggestion to the help message.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Help, NamedSource, SourceRange, Suggestion};
    ///
    /// let source = Arc::new(NamedSource::new(
    ///     "src/lib.rs",
    ///     r#"fn main() -> int {
    ///     /// doc comment
    ///     let a = Testing::new();
    ///     let b = a.invoke();
    ///
    ///     return 0;
    /// }"#,
    /// ));
    ///
    /// let source_range = SourceRange::new(source.clone(), 23..38);
    /// let suggestion = Suggestion::delete(source_range);
    ///
    /// let help = Help::new("remove this doc comment")
    ///     .with_suggestion(suggestion.clone());
    ///
    /// assert_eq!(help.suggestions, vec![suggestion]);
    /// ```
    pub fn with_suggestion(mut self, suggestion: impl Into<Suggestion>) -> Self {
        self.suggestions.push(suggestion.into());
        self
    }

    /// Adds the given suggestions to the help message.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Help, NamedSource, SourceRange, Suggestion};
    ///
    /// let source = Arc::new(NamedSource::new(
    ///     "src/lib.rs",
    ///     r#"fn main() -> int {
    ///     let a = Testing::new();
    ///     let b = a.invoke();
    ///
    ///     return (0);
    /// }"#,
    /// ));
    ///
    /// let suggestion1 = Suggestion::delete(SourceRange::new(source.clone(), 83..84));
    /// let suggestion2 = Suggestion::delete(SourceRange::new(source.clone(), 85..86));
    ///
    /// let help = Help::new("remove this doc comment")
    ///     .with_suggestions([suggestion1.clone(), suggestion2.clone()]);
    ///
    /// assert_eq!(help.suggestions, vec![suggestion1, suggestion2]);
    /// ```
    pub fn with_suggestions(mut self, suggestions: impl IntoIterator<Item = Suggestion>) -> Self {
        self.suggestions.extend(suggestions);
        self
    }
}

impl From<&str> for Help {
    fn from(value: &str) -> Self {
        Help::new(value)
    }
}

impl From<String> for Help {
    fn from(value: String) -> Self {
        Help::new(value)
    }
}

impl From<&String> for Help {
    fn from(value: &String) -> Self {
        Help::new(value)
    }
}

/// Represents a single diagnostic message, which can be
/// pretty-printed into an intuitive and fancy error message.
pub trait Diagnostic: std::fmt::Debug {
    /// Defines which message to be raised to the user, when reported.
    fn message(&self) -> String;

    /// Diagnostic severity level.
    ///
    /// This may be used by the renderer to determine how to display the diagnostic or
    /// even halt the program, depending on the severity level.
    fn severity(&self) -> Severity {
        Severity::default()
    }

    /// Unique diagnostic code, which can be used to look up more information about the error.
    fn code<'a>(&'a self) -> Option<Box<dyn Display + 'a>> {
        None
    }

    /// Gets the source code which the diagnostic refers to.
    ///
    /// This isn't used if only defined by itself. It will only be used if one or more
    /// labels are defined without any source directly attached.
    fn source_code(&self) -> Option<Arc<dyn Source>> {
        None
    }

    /// Labels to attach to snippets of the source code.
    fn labels(&self) -> Option<Box<dyn Iterator<Item = Label> + '_>> {
        None
    }

    /// Any errors which were the underlying cause for the diagnostic to be raised.
    fn causes(&self) -> Box<dyn Iterator<Item = &(dyn Diagnostic + Send + Sync)> + '_> {
        Box::new(std::iter::empty())
    }

    /// Any related errors, which can be used to provide additional information about the diagnostic.
    fn related(&self) -> Box<dyn Iterator<Item = &(dyn Diagnostic + Send + Sync)> + '_> {
        Box::new(std::iter::empty())
    }

    /// Help messages, which can be used to provide additional information about the diagnostic.
    fn help(&self) -> Option<Box<dyn Iterator<Item = Help> + '_>> {
        None
    }
}

impl std::fmt::Display for Box<dyn Diagnostic + Send + Sync + 'static> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", self.message())
    }
}

impl<T: Diagnostic + Send + Sync + 'static> From<T> for Box<dyn Diagnostic + Send + Sync + 'static> {
    fn from(value: T) -> Self {
        Box::new(value)
    }
}

impl<T: Diagnostic + Send + Sync + 'static> From<T> for Box<dyn Diagnostic + Send + 'static> {
    fn from(value: T) -> Self {
        Box::<dyn Diagnostic + Send + Sync>::from(value)
    }
}

impl<T: Diagnostic + Send + Sync + 'static> From<T> for Box<dyn Diagnostic + 'static> {
    fn from(value: T) -> Self {
        Box::<dyn Diagnostic + Send + Sync>::from(value)
    }
}

impl From<Box<dyn std::error::Error + Send + Sync>> for Box<dyn Diagnostic + Send + Sync> {
    fn from(err: Box<dyn std::error::Error + Send + Sync>) -> Self {
        err.into_diagnostic()
    }
}

impl From<std::io::Error> for Box<dyn Diagnostic + Send + Sync> {
    fn from(s: std::io::Error) -> Self {
        From::<Box<dyn std::error::Error + Send + Sync>>::from(Box::new(s))
    }
}

impl std::cmp::PartialEq for Box<dyn Diagnostic + Send + Sync> {
    fn eq(&self, other: &Self) -> bool {
        self.message() == other.message()
    }
}

impl std::cmp::Eq for Box<dyn Diagnostic + Send + Sync> {}

/// Trait for converting implementations into implementations of [`Diagnostic`].
pub trait IntoDiagnostic {
    /// Converts the instance into an implementation of [`Diagnostic`].
    fn into_diagnostic(self) -> Box<dyn Diagnostic + Send + Sync>;
}

impl<T: std::error::Error + Send + Sync> IntoDiagnostic for T {
    fn into_diagnostic(self) -> Box<dyn Diagnostic + Send + Sync> {
        Box::new(SimpleDiagnostic::new(self.to_string()))
    }
}

/// Diagnostic which can be created at runtime.
#[derive(Default, Debug)]
pub struct SimpleDiagnostic {
    /// Defines the message being displayed along with the diagnostic.
    pub message: String,

    /// Unique code for the diagnostic, which can be used to look up
    /// more information about the diagnostic.
    pub code: Option<String>,

    /// Defines the severity of the diagnostic. Defaults to `Severity::Error`.
    pub severity: Severity,

    /// Defines a list of help messages which can help or guide the user about the diagnostic.
    pub help: Vec<Help>,

    /// Defines a list of labels which can provide additional context about the diagnostic.
    pub labels: Option<Vec<Label>>,

    /// Defines the underlying cause for the diagnostic to be raised.
    pub causes: Vec<Box<dyn Diagnostic + Send + Sync>>,

    /// Defines the diagnostics which are related to the current one, if any.
    pub related: Vec<Box<dyn Diagnostic + Send + Sync>>,
}

impl SimpleDiagnostic {
    /// Creates a new [`SimpleDiagnostic`] with the given message content.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::SimpleDiagnostic;
    ///
    /// let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!");
    /// assert_eq!(diag.to_string(), "Whoops, that wasn't supposed to happen!");
    /// assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
    /// ```
    pub fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
            ..Self::default()
        }
    }

    /// Sets the severity for the current diagnostic instance.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::{Severity, SimpleDiagnostic};
    ///
    /// let diag = SimpleDiagnostic::new("Hmm, this could certainly be done better.")
    ///     .with_severity(Severity::Warning);
    ///
    /// assert_eq!(diag.message, "Hmm, this could certainly be done better.");
    /// assert_eq!(diag.severity, Severity::Warning);
    /// ```
    pub fn with_severity(mut self, severity: impl Into<Severity>) -> Self {
        self.severity = severity.into();
        self
    }

    /// Sets the diagnostic code for the current instance.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::SimpleDiagnostic;
    ///
    /// let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
    ///     .with_code("E1010");
    ///
    /// assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
    /// assert_eq!(diag.code, Some(String::from("E1010")));
    /// ```
    pub fn with_code(mut self, code: impl Into<String>) -> Self {
        self.code = Some(code.into());
        self
    }

    /// Adds a new help message to the current instance.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::{Help, SimpleDiagnostic};
    ///
    /// let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
    ///     .with_help("have you tried restarting?");
    ///
    /// assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
    /// assert_eq!(diag.help, vec![Help::new("have you tried restarting?")]);
    /// ```
    pub fn with_help(mut self, help: impl Into<Help>) -> Self {
        self.help.push(help.into());
        self
    }

    /// Sets the help message of the current instance.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::{Help, SimpleDiagnostic};
    ///
    /// let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
    ///     .set_help("have you tried restarting?");
    ///
    /// assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
    /// assert_eq!(diag.help, vec![Help::new("have you tried restarting?")]);
    /// ```
    pub fn set_help(mut self, help: impl Into<Help>) -> Self {
        self.help = vec![help.into()];
        self
    }

    /// Adds a new label to the current instance.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{SimpleDiagnostic, Label, NamedSource};
    ///
    /// let source = Arc::new(NamedSource::new(
    ///     "src/lib.rs",
    ///     r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return false;
    /// }"#,
    /// ));
    ///
    /// let label1 = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'");
    /// let label2 = Label::new(Some(source.clone()), 81..86, "expected 'int', found 'boolean'");
    ///
    /// let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
    ///     .with_label(label1.clone())
    ///     .with_label(label2.clone());
    ///
    /// assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
    /// assert_eq!(diag.labels, Some(vec![label1, label2]));
    /// ```
    pub fn with_label(mut self, label: impl Into<Label>) -> Self {
        let mut labels = self.labels.unwrap_or_default();
        labels.push(label.into());

        self.labels = Some(labels);
        self
    }

    /// Adds a list of labels to the current instance. The given
    /// labels are appended onto the existing label array in the
    /// diagnostic, so nothing is overwritten.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{SimpleDiagnostic, Label, NamedSource};
    ///
    /// let source = Arc::new(NamedSource::new(
    ///     "src/lib.rs",
    ///     r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return false;
    /// }"#,
    /// ));
    ///
    /// let label1 = Label::new(Some(source.clone()), 60..65, "could not find method 'invok'");
    /// let label2 = Label::new(Some(source.clone()), 81..86, "expected 'int', found 'boolean'");
    ///
    /// let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
    ///     .with_labels([label1.clone(), label2.clone()]);
    ///
    /// assert_eq!(diag.message, "Whoops, that wasn't supposed to happen!");
    /// assert_eq!(diag.labels, Some(vec![label1, label2]));
    /// ```
    pub fn with_labels(mut self, labels: impl IntoIterator<Item = impl Into<Label>>) -> Self {
        let labels = labels
            .into_iter()
            .map(|r| Into::<Label>::into(r))
            .collect::<Vec<Label>>();

        let mut all_labels = self.labels.unwrap_or_default();
        all_labels.extend(labels);

        self.labels = Some(all_labels);
        self
    }

    /// Adds a related diagnostic to the current instance.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::SimpleDiagnostic;
    ///
    /// let related1 = std::io::Error::new(std::io::ErrorKind::Other, "failed to read file");
    /// let related2 = std::io::Error::new(std::io::ErrorKind::Other, "file is unaccessible");
    ///
    /// let diag = SimpleDiagnostic::new("failed to perform I/O operation")
    ///     .add_related(related1)
    ///     .add_related(related2);
    ///
    /// assert_eq!(diag.message, "failed to perform I/O operation");
    /// assert_eq!(diag.related.iter().map(|e| e.to_string()).collect::<Vec<_>>(), vec![
    ///     "failed to read file".to_string(),
    ///     "file is unaccessible".to_string()
    /// ]);
    /// ```
    pub fn add_related(mut self, related: impl Into<Box<dyn Diagnostic + Send + Sync>>) -> Self {
        self.related.push(related.into());
        self
    }

    /// Adds multiple related diagnostics to the current instance.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::SimpleDiagnostic;
    ///
    /// let related1 = std::io::Error::new(std::io::ErrorKind::Other, "failed to read file");
    /// let related2 = std::io::Error::new(std::io::ErrorKind::Other, "file is unaccessible");
    ///
    /// let diag = SimpleDiagnostic::new("failed to perform I/O operation")
    ///     .append_related([related1, related2]);
    ///
    /// assert_eq!(diag.message, "failed to perform I/O operation");
    /// assert_eq!(diag.related.iter().map(|e| e.to_string()).collect::<Vec<_>>(), vec![
    ///     "failed to read file".to_string(),
    ///     "file is unaccessible".to_string()
    /// ]);
    /// ```
    pub fn append_related(
        mut self,
        related: impl IntoIterator<Item = impl Into<Box<dyn Diagnostic + Send + Sync>>>,
    ) -> Self {
        let related = related
            .into_iter()
            .map(|r| Into::<Box<dyn Diagnostic + Send + Sync>>::into(r))
            .collect::<Vec<Box<dyn Diagnostic + Send + Sync>>>();

        self.related.extend(related);
        self
    }

    /// Adds a causing error diagnostic to the current instance.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::SimpleDiagnostic;
    ///
    /// let cause1 = std::io::Error::new(std::io::ErrorKind::Other, "failed to read file");
    /// let cause2 = std::io::Error::new(std::io::ErrorKind::Other, "file is unaccessible");
    ///
    /// let diag = SimpleDiagnostic::new("failed to perform I/O operation")
    ///     .add_cause(cause1)
    ///     .add_cause(cause2);
    ///
    /// assert_eq!(diag.message, "failed to perform I/O operation");
    /// assert_eq!(diag.causes.iter().map(|e| e.to_string()).collect::<Vec<_>>(), vec![
    ///     "failed to read file".to_string(),
    ///     "file is unaccessible".to_string()
    /// ]);
    /// ```
    pub fn add_cause(mut self, cause: impl Into<Box<dyn Diagnostic + Send + Sync>>) -> Self {
        self.causes.push(cause.into());
        self
    }

    /// Adds multiple causing error diagnostics to the current instance.
    ///
    /// # Examples
    /// ```
    /// use error_snippet::SimpleDiagnostic;
    ///
    /// let cause1 = std::io::Error::new(std::io::ErrorKind::Other, "failed to read file");
    /// let cause2 = std::io::Error::new(std::io::ErrorKind::Other, "file is unaccessible");
    ///
    /// let diag = SimpleDiagnostic::new("failed to perform I/O operation")
    ///     .add_causes([cause1, cause2]);
    ///
    /// assert_eq!(diag.message, "failed to perform I/O operation");
    /// assert_eq!(diag.causes.iter().map(|e| e.to_string()).collect::<Vec<_>>(), vec![
    ///     "failed to read file".to_string(),
    ///     "file is unaccessible".to_string()
    /// ]);
    /// ```
    pub fn add_causes(
        mut self,
        causes: impl IntoIterator<Item = impl Into<Box<dyn Diagnostic + Send + Sync>>>,
    ) -> Self {
        let causes = causes
            .into_iter()
            .map(|r| Into::<Box<dyn Diagnostic + Send + Sync>>::into(r))
            .collect::<Vec<Box<dyn Diagnostic + Send + Sync>>>();

        self.causes.extend(causes);
        self
    }
}

impl Diagnostic for SimpleDiagnostic {
    fn message(&self) -> String {
        self.message.clone()
    }

    fn severity(&self) -> Severity {
        self.severity
    }

    fn code(&self) -> Option<Box<dyn Display + '_>> {
        self.code.as_ref().map(|c| Box::new(c) as Box<dyn Display>)
    }

    fn help(&self) -> Option<Box<dyn Iterator<Item = Help> + '_>> {
        Some(Box::new(self.help.clone().into_iter()))
    }

    fn labels(&self) -> Option<Box<dyn Iterator<Item = Label> + '_>> {
        self.labels
            .as_ref()
            .map(|ls| ls.iter().cloned())
            .map(Box::new)
            .map(|b| b as Box<dyn Iterator<Item = Label>>)
    }

    fn related(&self) -> Box<dyn Iterator<Item = &(dyn Diagnostic + Send + Sync)> + '_> {
        Box::new(self.related.iter().map(|b| b.as_ref()))
    }

    fn causes(&self) -> Box<dyn Iterator<Item = &(dyn Diagnostic + Send + Sync)> + '_> {
        Box::new(self.causes.iter().map(|b| b.as_ref()))
    }
}

impl std::fmt::Display for SimpleDiagnostic {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", &self.message)
    }
}

#[derive(Debug)]
pub struct SourceWrapped {
    pub(crate) diagnostic: Box<dyn Diagnostic + Send + Sync>,
    pub(crate) source: Arc<dyn Source + Send + Sync>,
}

impl Diagnostic for SourceWrapped {
    fn message(&self) -> String {
        self.diagnostic.message()
    }

    fn severity(&self) -> Severity {
        self.diagnostic.severity()
    }

    fn code(&self) -> Option<Box<dyn Display + '_>> {
        self.diagnostic.code()
    }

    fn help(&self) -> Option<Box<dyn Iterator<Item = Help> + '_>> {
        self.diagnostic.help()
    }

    fn labels(&self) -> Option<Box<dyn Iterator<Item = Label> + '_>> {
        self.diagnostic.labels()
    }

    fn related(&self) -> Box<dyn Iterator<Item = &(dyn Diagnostic + Send + Sync)> + '_> {
        self.diagnostic.related()
    }

    fn causes(&self) -> Box<dyn Iterator<Item = &(dyn Diagnostic + Send + Sync)> + '_> {
        self.diagnostic.causes()
    }

    fn source_code(&self) -> Option<Arc<dyn Source>> {
        self.diagnostic.source_code().or_else(|| Some(self.source.clone()))
    }
}

impl std::fmt::Display for SourceWrapped {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}", &self.message())
    }
}

pub trait WithSource {
    /// Provides the current diagnostic with source code, so it
    /// can still be reported, even though no source is available at
    /// the time of diagnostic creation.
    ///
    /// # Examples
    /// ```
    /// use std::sync::Arc;
    /// use error_snippet::{Diagnostic, SimpleDiagnostic, Label, NamedSource, Source, WithSource};
    ///
    /// // no source attached
    /// let label = Label::new(None, 60..65, "could not find method 'invok'");
    ///
    /// let diag = SimpleDiagnostic::new("Whoops, that wasn't supposed to happen!")
    ///     .with_label(label.clone());
    ///
    /// let source = Arc::new(NamedSource::new(
    ///     "src/lib.rs",
    ///     r#"fn main() -> int {
    ///     let a = new Testing();
    ///     let b = a.invok();
    ///
    ///     return false;
    /// }"#,
    /// ));
    ///
    /// // attach the source code to the diagnostic
    /// let diag = diag.with_source(source.clone());
    ///
    /// assert_eq!(diag.message(), "Whoops, that wasn't supposed to happen!");
    /// assert_eq!(diag.source_code().unwrap().name(), source.name());
    /// assert_eq!(diag.source_code().unwrap().content(), source.content());
    /// ```
    fn with_source(self, source: Arc<dyn Source>) -> impl Diagnostic;
}

impl<T: Diagnostic + Send + Sync + 'static> WithSource for T {
    fn with_source(self, source: Arc<dyn Source>) -> impl Diagnostic {
        SourceWrapped {
            diagnostic: Box::new(self),
            source,
        }
    }
}