1 //! Structures used as an input for the library.
2 //!
3 //! Example:
4 //!
5 //! ```
6 //! use annotate_snippets::snippet::*;
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 //!     opt: Default::default(),
32 //! };
33 //! ```
34 use crate::display_list::FormatOptions;
35 
36 /// Primary structure provided for formatting
37 #[derive(Debug, Default)]
38 pub struct Snippet<'a> {
39     pub title: Option<Annotation<'a>>,
40     pub footer: Vec<Annotation<'a>>,
41     pub slices: Vec<Slice<'a>>,
42     pub opt: FormatOptions,
43 }
44 
45 /// Structure containing the slice of text to be annotated and
46 /// basic information about the location of the slice.
47 ///
48 /// One `Slice` is meant to represent a single, continuous,
49 /// slice of source code that you want to annotate.
50 #[derive(Debug)]
51 pub struct Slice<'a> {
52     pub source: &'a str,
53     pub line_start: usize,
54     pub origin: Option<&'a str>,
55     pub annotations: Vec<SourceAnnotation<'a>>,
56     /// If set explicitly to `true`, the snippet will fold
57     /// parts of the slice that don't contain any annotations.
58     pub fold: bool,
59 }
60 
61 /// Types of annotations.
62 #[derive(Debug, Clone, Copy, PartialEq)]
63 pub enum AnnotationType {
64     /// Error annotations are displayed using red color and "^" character.
65     Error,
66     /// Warning annotations are displayed using blue color and "-" character.
67     Warning,
68     Info,
69     Note,
70     Help,
71 }
72 
73 /// An annotation for a `Slice`.
74 #[derive(Debug)]
75 pub struct SourceAnnotation<'a> {
76     pub range: (usize, usize),
77     pub label: &'a str,
78     pub annotation_type: AnnotationType,
79 }
80 
81 /// An annotation for a `Snippet`.
82 #[derive(Debug)]
83 pub struct Annotation<'a> {
84     /// Identifier of the annotation. Usually error code like "E0308".
85     pub id: Option<&'a str>,
86     pub label: Option<&'a str>,
87     pub annotation_type: AnnotationType,
88 }
89