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

rust-lang / annotate-snippets-rs / 15975677962

30 Jun 2025 02:27PM UTC coverage: 87.75% (-0.5%) from 88.235%
15975677962

push

github

web-flow
Merge pull request #232 from Muscraft/use-cow

fix: Take Into<Cow> instead of &str

52 of 67 new or added lines in 4 files covered. (77.61%)

3 existing lines in 3 files now uncovered.

1404 of 1600 relevant lines covered (87.75%)

4.66 hits per line

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

80.0
/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::{OptionCow, Title};
6
use anstyle::Style;
7
use std::borrow::Cow;
8

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

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

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

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

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

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

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

53
    /// <div class="warning">
54
    ///
55
    /// Text passed to this function is considered "untrusted input", as such
56
    /// all text is passed through a normalization function. Pre-styled text is
57
    /// not allowed to be passed to this function.
58
    ///
59
    /// </div>
60
    pub fn text(self, text: impl Into<OptionCow<'a>>) -> Level<'a> {
1✔
61
        Level {
62
            name: Some(text.into().0),
2✔
63
            level: self.level,
1✔
64
        }
65
    }
66
}
67

68
impl<'a> Level<'a> {
69
    /// <div class="warning">
70
    ///
71
    /// Text passed to this function is considered "untrusted input", as such
72
    /// all text is passed through a normalization function. Pre-styled text is
73
    /// not allowed to be passed to this function.
74
    ///
75
    /// </div>
76
    pub fn title(self, title: impl Into<Cow<'a, str>>) -> Title<'a> {
12✔
77
        Title {
78
            level: self,
79
            id: None,
80
            title: title.into(),
12✔
81
            is_pre_styled: false,
82
        }
83
    }
84

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

102
    pub(crate) fn as_str(&'a self) -> &'a str {
5✔
103
        match (&self.name, self.level) {
5✔
104
            (Some(Some(name)), _) => name.as_ref(),
1✔
UNCOV
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,
3✔
110
            (None, LevelInner::Help) => HELP_TXT,
3✔
111
        }
112
    }
113

114
    pub(crate) fn style(&self, stylesheet: &Stylesheet) -> Style {
7✔
115
        self.level.style(stylesheet)
3✔
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 {
4✔
131
            LevelInner::Error => stylesheet.error,
6✔
132
            LevelInner::Warning => stylesheet.warning,
1✔
133
            LevelInner::Info => stylesheet.info,
×
134
            LevelInner::Note => stylesheet.note,
3✔
135
            LevelInner::Help => stylesheet.help,
3✔
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