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

rust-lang / annotate-snippets-rs / 14503271047

16 Apr 2025 09:49PM UTC coverage: 87.161%. Remained the same
14503271047

Pull #198

github

web-flow
Merge 66369feec into 68342cce3
Pull Request #198: fix!: Iterate on the API

26 of 33 new or added lines in 5 files covered. (78.79%)

2 existing lines in 2 files now uncovered.

1351 of 1550 relevant lines covered (87.16%)

3.82 hits per line

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

70.83
/src/level.rs
1
//! [`Level`] constants for easy importing
2

3
use crate::renderer::stylesheet::Stylesheet;
4
use crate::snippet::{ERROR_TXT, HELP_TXT, INFO_TXT, NOTE_TXT, WARNING_TXT};
5
use crate::{Element, Group, Message, Title};
6
use anstyle::Style;
7

8
/// Default `error:` [`Level`]
9
pub const ERROR: Level<'_> = Level {
10
    name: None,
11
    level: LevelInner::Error,
12
};
13

14
/// Default `warning:` [`Level`]
15
pub const WARNING: Level<'_> = Level {
16
    name: None,
17
    level: LevelInner::Warning,
18
};
19

20
/// Default `info:` [`Level`]
21
pub const INFO: Level<'_> = Level {
22
    name: None,
23
    level: LevelInner::Info,
24
};
25

26
/// Default `note:` [`Level`]
27
pub const NOTE: Level<'_> = Level {
28
    name: None,
29
    level: LevelInner::Note,
30
};
31

32
/// Default `help:` [`Level`]
33
pub const HELP: Level<'_> = Level {
34
    name: None,
35
    level: LevelInner::Help,
36
};
37

38
/// [`Message`] or [`Title`] severity level
39
#[derive(Clone, Debug, PartialEq, Eq, PartialOrd, Ord)]
40
pub struct Level<'a> {
41
    pub(crate) name: Option<Option<&'a str>>,
42
    pub(crate) level: LevelInner,
43
}
44

45
impl<'a> Level<'a> {
46
    pub const ERROR: Level<'a> = ERROR;
47
    pub const WARNING: Level<'a> = WARNING;
48
    pub const INFO: Level<'a> = INFO;
49
    pub const NOTE: Level<'a> = NOTE;
50
    pub const HELP: Level<'a> = HELP;
51

52
    /// <div class="warning">
53
    ///
54
    /// Text passed to this function is considered "untrusted input", as such
55
    /// all text is passed through a normalization function. Pre-styled text is
56
    /// not allowed to be passed to this function.
57
    ///
58
    /// </div>
UNCOV
59
    pub fn text(self, text: Option<&'a str>) -> Level<'a> {
×
60
        Level {
61
            name: Some(text),
×
62
            level: self.level,
×
63
        }
64
    }
65
}
66

67
impl<'a> Level<'a> {
68
    /// <div class="warning">
69
    ///
70
    /// Text passed to this function is considered "untrusted input", as such
71
    /// all text is passed through a normalization function. Pre-styled text is
72
    /// not allowed to be passed to this function.
73
    ///
74
    /// </div>
75
    pub fn header(self, header: &'a str) -> Message<'a> {
9✔
76
        Message {
77
            id: None,
78
            groups: vec![Group::new().element(Element::Title(Title {
9✔
79
                level: self,
80
                title: header,
81
                primary: true,
82
            }))],
83
        }
84
    }
85

86
    /// <div class="warning">
87
    ///
88
    /// Text passed to this function is allowed to be pre-styled, as such all
89
    /// text is considered "trusted input" and has no normalizations applied to
90
    /// it. [`normalize_untrusted_str`](crate::normalize_untrusted_str) can be
91
    /// used to normalize untrusted text before it is passed to this function.
92
    ///
93
    /// </div>
94
    pub fn title(self, title: &'a str) -> Title<'a> {
3✔
95
        Title {
96
            level: self,
97
            title,
98
            primary: false,
99
        }
100
    }
101

102
    pub(crate) fn as_str(&self) -> &'a str {
4✔
103
        match (self.name, self.level) {
6✔
104
            (Some(Some(name)), _) => name,
×
105
            (Some(None), _) => "",
×
106
            (None, LevelInner::Error) => ERROR_TXT,
4✔
107
            (None, LevelInner::Warning) => WARNING_TXT,
1✔
108
            (None, LevelInner::Info) => INFO_TXT,
×
109
            (None, LevelInner::Note) => NOTE_TXT,
2✔
110
            (None, LevelInner::Help) => HELP_TXT,
2✔
111
        }
112
    }
113

114
    pub(crate) fn style(&self, stylesheet: &Stylesheet) -> Style {
4✔
115
        self.level.style(stylesheet)
5✔
116
    }
117
}
118

119
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
120
pub(crate) enum LevelInner {
121
    Error,
122
    Warning,
123
    Info,
124
    Note,
125
    Help,
126
}
127

128
impl LevelInner {
129
    pub(crate) fn style(self, stylesheet: &Stylesheet) -> Style {
6✔
130
        match self {
3✔
131
            LevelInner::Error => stylesheet.error,
5✔
132
            LevelInner::Warning => stylesheet.warning,
1✔
133
            LevelInner::Info => stylesheet.info,
×
134
            LevelInner::Note => stylesheet.note,
2✔
135
            LevelInner::Help => stylesheet.help,
2✔
136
        }
137
    }
138
}
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