1 | //! Structures used as an input for the library. |
2 | //! |
3 | //! Example: |
4 | //! |
5 | //! ``` |
6 | //! use annotate_snippets::*; |
7 | //! |
8 | //! Snippet { |
9 | //! title: Some(Annotation { |
10 | //! label: Some("mismatched types" ), |
11 | //! id: None, |
12 | //! annotation_type: AnnotationType::Error, |
13 | //! }), |
14 | //! footer: vec![], |
15 | //! slices: vec![ |
16 | //! Slice { |
17 | //! source: "Foo" , |
18 | //! line_start: 51, |
19 | //! origin: Some("src/format.rs" ), |
20 | //! fold: false, |
21 | //! annotations: vec![], |
22 | //! }, |
23 | //! Slice { |
24 | //! source: "Faa" , |
25 | //! line_start: 129, |
26 | //! origin: Some("src/display.rs" ), |
27 | //! fold: false, |
28 | //! annotations: vec![], |
29 | //! }, |
30 | //! ], |
31 | //! }; |
32 | //! ``` |
33 | |
34 | /// Primary structure provided for formatting |
35 | #[derive (Debug, Default)] |
36 | pub struct Snippet<'a> { |
37 | pub title: Option<Annotation<'a>>, |
38 | pub footer: Vec<Annotation<'a>>, |
39 | pub slices: Vec<Slice<'a>>, |
40 | } |
41 | |
42 | /// Structure containing the slice of text to be annotated and |
43 | /// basic information about the location of the slice. |
44 | /// |
45 | /// One `Slice` is meant to represent a single, continuous, |
46 | /// slice of source code that you want to annotate. |
47 | #[derive (Debug)] |
48 | pub struct Slice<'a> { |
49 | pub source: &'a str, |
50 | pub line_start: usize, |
51 | pub origin: Option<&'a str>, |
52 | pub annotations: Vec<SourceAnnotation<'a>>, |
53 | /// If set explicitly to `true`, the snippet will fold |
54 | /// parts of the slice that don't contain any annotations. |
55 | pub fold: bool, |
56 | } |
57 | |
58 | /// Types of annotations. |
59 | #[derive (Debug, Clone, Copy, PartialEq)] |
60 | pub enum AnnotationType { |
61 | /// Error annotations are displayed using red color and "^" character. |
62 | Error, |
63 | /// Warning annotations are displayed using blue color and "-" character. |
64 | Warning, |
65 | Info, |
66 | Note, |
67 | Help, |
68 | } |
69 | |
70 | /// An annotation for a `Slice`. |
71 | #[derive (Debug)] |
72 | pub struct SourceAnnotation<'a> { |
73 | pub range: (usize, usize), |
74 | pub label: &'a str, |
75 | pub annotation_type: AnnotationType, |
76 | } |
77 | |
78 | /// An annotation for a `Snippet`. |
79 | #[derive (Debug)] |
80 | pub struct Annotation<'a> { |
81 | /// Identifier of the annotation. Usually error code like "E0308". |
82 | pub id: Option<&'a str>, |
83 | pub label: Option<&'a str>, |
84 | pub annotation_type: AnnotationType, |
85 | } |
86 | |