• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

rust-lang / annotate-snippets-rs / 12241839644

09 Dec 2024 06:22PM UTC coverage: 85.466%. Remained the same
12241839644

Pull #166

github

web-flow
Merge 72dd8c7b9 into 6f28705b1
Pull Request #166: chore: Release annotate-snippets version 0.11.5

788 of 922 relevant lines covered (85.47%)

5.27 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

100.0
/src/snippet.rs
1
//! Structures used as an input for the library.
2
//!
3
//! Example:
4
//!
5
//! ```
6
//! use annotate_snippets::*;
7
//!
8
//! Level::Error.title("mismatched types")
9
//!     .snippet(Snippet::source("Foo").line_start(51).origin("src/format.rs"))
10
//!     .snippet(Snippet::source("Faa").line_start(129).origin("src/display.rs"));
11
//! ```
12

13
use std::ops::Range;
14

15
/// Primary structure provided for formatting
16
///
17
/// See [`Level::title`] to create a [`Message`]
18
#[derive(Debug)]
19
pub struct Message<'a> {
20
    pub(crate) level: Level,
21
    pub(crate) id: Option<&'a str>,
22
    pub(crate) title: &'a str,
23
    pub(crate) snippets: Vec<Snippet<'a>>,
24
    pub(crate) footer: Vec<Message<'a>>,
25
}
26

27
impl<'a> Message<'a> {
28
    pub fn id(mut self, id: &'a str) -> Self {
2✔
29
        self.id = Some(id);
2✔
30
        self
2✔
31
    }
32

33
    pub fn snippet(mut self, slice: Snippet<'a>) -> Self {
2✔
34
        self.snippets.push(slice);
6✔
35
        self
2✔
36
    }
37

38
    pub fn snippets(mut self, slice: impl IntoIterator<Item = Snippet<'a>>) -> Self {
3✔
39
        self.snippets.extend(slice);
3✔
40
        self
3✔
41
    }
42

43
    pub fn footer(mut self, footer: Message<'a>) -> Self {
1✔
44
        self.footer.push(footer);
1✔
45
        self
1✔
46
    }
47

48
    pub fn footers(mut self, footer: impl IntoIterator<Item = Message<'a>>) -> Self {
1✔
49
        self.footer.extend(footer);
3✔
50
        self
1✔
51
    }
52
}
53

54
/// Structure containing the slice of text to be annotated and
55
/// basic information about the location of the slice.
56
///
57
/// One `Snippet` is meant to represent a single, continuous,
58
/// slice of source code that you want to annotate.
59
#[derive(Debug)]
60
pub struct Snippet<'a> {
61
    pub(crate) origin: Option<&'a str>,
62
    pub(crate) line_start: usize,
63

64
    pub(crate) source: &'a str,
65
    pub(crate) annotations: Vec<Annotation<'a>>,
66

67
    pub(crate) fold: bool,
68
}
69

70
impl<'a> Snippet<'a> {
71
    pub fn source(source: &'a str) -> Self {
8✔
72
        Self {
73
            origin: None,
74
            line_start: 1,
75
            source,
76
            annotations: vec![],
9✔
77
            fold: false,
78
        }
79
    }
80

81
    pub fn line_start(mut self, line_start: usize) -> Self {
5✔
82
        self.line_start = line_start;
7✔
83
        self
5✔
84
    }
85

86
    pub fn origin(mut self, origin: &'a str) -> Self {
5✔
87
        self.origin = Some(origin);
6✔
88
        self
5✔
89
    }
90

91
    pub fn annotation(mut self, annotation: Annotation<'a>) -> Self {
6✔
92
        self.annotations.push(annotation);
2✔
93
        self
6✔
94
    }
95

96
    pub fn annotations(mut self, annotation: impl IntoIterator<Item = Annotation<'a>>) -> Self {
1✔
97
        self.annotations.extend(annotation);
3✔
98
        self
1✔
99
    }
100

101
    /// Hide lines without [`Annotation`]s
102
    pub fn fold(mut self, fold: bool) -> Self {
5✔
103
        self.fold = fold;
5✔
104
        self
5✔
105
    }
106
}
107

108
/// An annotation for a [`Snippet`].
109
///
110
/// See [`Level::span`] to create a [`Annotation`]
111
#[derive(Debug)]
112
pub struct Annotation<'a> {
113
    /// The byte range of the annotation in the `source` string
114
    pub(crate) range: Range<usize>,
115
    pub(crate) label: Option<&'a str>,
116
    pub(crate) level: Level,
117
}
118

119
impl<'a> Annotation<'a> {
120
    pub fn label(mut self, label: &'a str) -> Self {
5✔
121
        self.label = Some(label);
5✔
122
        self
5✔
123
    }
124
}
125

126
/// Types of annotations.
127
#[derive(Debug, Clone, Copy, PartialEq)]
128
pub enum Level {
129
    /// Error annotations are displayed using red color and "^" character.
130
    Error,
131
    /// Warning annotations are displayed using blue color and "-" character.
132
    Warning,
133
    Info,
134
    Note,
135
    Help,
136
}
137

138
impl Level {
139
    pub fn title(self, title: &str) -> Message<'_> {
7✔
140
        Message {
141
            level: self,
142
            id: None,
143
            title,
144
            snippets: vec![],
11✔
145
            footer: vec![],
7✔
146
        }
147
    }
148

149
    /// Create a [`Annotation`] with the given span for a [`Snippet`]
150
    pub fn span<'a>(self, span: Range<usize>) -> Annotation<'a> {
5✔
151
        Annotation {
152
            range: span,
153
            label: None,
154
            level: self,
155
        }
156
    }
157
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2025 Coveralls, Inc