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
use annotate_snippets::display_list::DisplayList;
use annotate_snippets::formatter::DisplayListFormatter;
use annotate_snippets::snippet::{Annotation, AnnotationType, Slice, Snippet, SourceAnnotation};

/// An warning formatter.
pub struct Warning {
    snippet: Snippet,
}

impl Warning {
    /// Create a new `Warning` formatter.
    pub fn new(label: impl ToString) -> Self {
        Self {
            snippet: Snippet {
                title: Some(Annotation {
                    label: Some(label.to_string()),
                    id: None,
                    annotation_type: AnnotationType::Warning,
                }),
                slices: vec![],
                footer: vec![],
            },
        }
    }

    /// Pass a new warning to the formatter.
    pub fn warning(
        mut self,
        line_start: usize,
        start: usize,
        end: usize,
        source: impl ToString,
        label: impl ToString,
    ) -> Self {
        self.snippet.slices.push(Slice {
            source: source.to_string(),
            line_start,
            origin: None,
            fold: false,
            annotations: vec![SourceAnnotation {
                label: label.to_string(),
                annotation_type: AnnotationType::Warning,
                range: (start, end),
            }],
        });
        self
    }

    /// Create a new footer.
    pub fn help(mut self, label: impl ToString) -> Self {
        self.snippet.footer.push(Annotation {
            label: Some(label.to_string()),
            id: None,
            annotation_type: AnnotationType::Help,
        });
        self
    }

    pub fn to_string(self) -> String {
        let dl = DisplayList::from(self.snippet);
        let dlf = DisplayListFormatter::new(true, false);
        format!("{}", dlf.format(&dl))
    }
}