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

rust-lang / annotate-snippets-rs / 13903310413

17 Mar 2025 03:26PM UTC coverage: 85.793%. Remained the same
13903310413

Pull #185

github

web-flow
Merge 34a69b497 into 5c6ce17f3
Pull Request #185: refactor(display-list): split into separate modules (#184)

636 of 729 new or added lines in 6 files covered. (87.24%)

779 of 908 relevant lines covered (85.79%)

4.62 hits per line

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

86.67
/src/renderer/display/display_annotations.rs
1
use anstyle::Style;
2

3
use crate::{renderer::stylesheet::Stylesheet, snippet};
4

5
use super::{constants::*, display_text::DisplayTextFragment};
6

7
/// A type of the `Annotation` which may impact the sigils, style or text displayed.
8
///
9
/// There are several ways to uses this information when formatting the `DisplayList`:
10
///
11
/// * An annotation may display the name of the type like `error` or `info`.
12
/// * An underline for `Error` may be `^^^` while for `Warning` it could be `---`.
13
/// * `ColorStylesheet` may use different colors for different annotations.
14
#[derive(Debug, Clone, PartialEq)]
15
pub(crate) enum DisplayAnnotationType {
16
    None,
17
    Error,
18
    Warning,
19
    Info,
20
    Note,
21
    Help,
22
}
23

24
/// An inline text
25
/// An indicator of what part of the annotation a given `Annotation` is.
26
#[derive(Debug, Clone, PartialEq)]
27
pub(crate) enum DisplayAnnotationPart {
28
    /// A standalone, single-line annotation.
29
    Standalone,
30
    /// A continuation of a multi-line label of an annotation.
31
    LabelContinuation,
32
    /// A line starting a multiline annotation.
33
    MultilineStart(usize),
34
    /// A line ending a multiline annotation.
35
    MultilineEnd(usize),
36
}
37

38
/// Inline annotation which can be used in either Raw or Source line.
39
#[derive(Clone, Debug, PartialEq)]
40
pub(crate) struct Annotation<'a> {
41
    pub(crate) annotation_type: DisplayAnnotationType,
42
    pub(crate) id: Option<&'a str>,
43
    pub(crate) label: Vec<DisplayTextFragment<'a>>,
44
}
45

46
#[derive(Clone, Debug, PartialEq)]
47
pub(crate) struct DisplaySourceAnnotation<'a> {
48
    pub(crate) annotation: Annotation<'a>,
49
    pub(crate) range: (usize, usize),
50
    pub(crate) annotation_type: DisplayAnnotationType,
51
    pub(crate) annotation_part: DisplayAnnotationPart,
52
}
53

54
impl DisplaySourceAnnotation<'_> {
55
    pub(crate) fn has_label(&self) -> bool {
6✔
56
        !self
5✔
57
            .annotation
58
            .label
59
            .iter()
60
            .all(|label| label.content.is_empty())
6✔
61
    }
62

63
    // Length of this annotation as displayed in the stderr output
64
    pub(crate) fn len(&self) -> usize {
3✔
65
        // Account for usize underflows
66
        if self.range.1 > self.range.0 {
7✔
67
            self.range.1 - self.range.0
7✔
68
        } else {
NEW
69
            self.range.0 - self.range.1
×
70
        }
71
    }
72

73
    pub(crate) fn takes_space(&self) -> bool {
3✔
74
        // Multiline annotations always have to keep vertical space.
75
        matches!(
2✔
76
            self.annotation_part,
3✔
77
            DisplayAnnotationPart::MultilineStart(_) | DisplayAnnotationPart::MultilineEnd(_)
78
        )
79
    }
80
}
81

82
impl From<snippet::Level> for DisplayAnnotationType {
83
    fn from(at: snippet::Level) -> Self {
6✔
84
        match at {
4✔
85
            snippet::Level::Error => DisplayAnnotationType::Error,
6✔
86
            snippet::Level::Warning => DisplayAnnotationType::Warning,
2✔
87
            snippet::Level::Info => DisplayAnnotationType::Info,
1✔
88
            snippet::Level::Note => DisplayAnnotationType::Note,
1✔
89
            snippet::Level::Help => DisplayAnnotationType::Help,
2✔
90
        }
91
    }
92
}
93

94
#[inline]
95
pub(crate) fn annotation_type_str(annotation_type: &DisplayAnnotationType) -> &'static str {
4✔
96
    match annotation_type {
5✔
97
        DisplayAnnotationType::Error => ERROR_TXT,
5✔
98
        DisplayAnnotationType::Help => HELP_TXT,
1✔
99
        DisplayAnnotationType::Info => INFO_TXT,
1✔
100
        DisplayAnnotationType::Note => NOTE_TXT,
1✔
NEW
101
        DisplayAnnotationType::Warning => WARNING_TXT,
×
NEW
102
        DisplayAnnotationType::None => "",
×
103
    }
104
}
105

106
pub(crate) fn annotation_type_len(annotation_type: &DisplayAnnotationType) -> usize {
4✔
107
    match annotation_type {
6✔
108
        DisplayAnnotationType::Error => ERROR_TXT.len(),
4✔
109
        DisplayAnnotationType::Help => HELP_TXT.len(),
1✔
110
        DisplayAnnotationType::Info => INFO_TXT.len(),
1✔
111
        DisplayAnnotationType::Note => NOTE_TXT.len(),
1✔
NEW
112
        DisplayAnnotationType::Warning => WARNING_TXT.len(),
×
113
        DisplayAnnotationType::None => 0,
3✔
114
    }
115
}
116

117
pub(crate) fn get_annotation_style<'a>(
6✔
118
    annotation_type: &DisplayAnnotationType,
119
    stylesheet: &'a Stylesheet,
120
) -> &'a Style {
121
    match annotation_type {
11✔
122
        DisplayAnnotationType::Error => stylesheet.error(),
6✔
123
        DisplayAnnotationType::Warning => stylesheet.warning(),
2✔
124
        DisplayAnnotationType::Info => stylesheet.info(),
1✔
125
        DisplayAnnotationType::Note => stylesheet.note(),
1✔
126
        DisplayAnnotationType::Help => stylesheet.help(),
1✔
NEW
127
        DisplayAnnotationType::None => stylesheet.none(),
×
128
    }
129
}
130

131
#[inline]
132
pub(crate) fn is_annotation_empty(annotation: &Annotation<'_>) -> bool {
5✔
133
    annotation
5✔
NEW
134
        .label
×
135
        .iter()
136
        .all(|fragment| fragment.content.is_empty())
9✔
137
}
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

© 2026 Coveralls, Inc