@@ -36,40 +36,20 @@ pub const HELP: Level<'_> = Level {
36
36
level : LevelInner :: Help ,
37
37
} ;
38
38
39
- /// [`Title`] severity level
39
+ /// Severity level for [`Title`]s and [`Message`]s
40
40
#[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
41
41
pub struct Level < ' a > {
42
42
pub ( crate ) name : Option < Option < Cow < ' a , str > > > ,
43
43
pub ( crate ) level : LevelInner ,
44
44
}
45
45
46
+ /// # Constructors
46
47
impl < ' a > Level < ' a > {
47
48
pub const ERROR : Level < ' a > = ERROR ;
48
49
pub const WARNING : Level < ' a > = WARNING ;
49
50
pub const INFO : Level < ' a > = INFO ;
50
51
pub const NOTE : Level < ' a > = NOTE ;
51
52
pub const HELP : Level < ' a > = HELP ;
52
-
53
- /// Replace the name describing this [`Level`]
54
- ///
55
- /// <div class="warning">
56
- ///
57
- /// Text passed to this function is considered "untrusted input", as such
58
- /// all text is passed through a normalization function. Pre-styled text is
59
- /// not allowed to be passed to this function.
60
- ///
61
- /// </div>
62
- pub fn with_name ( self , name : impl Into < OptionCow < ' a > > ) -> Level < ' a > {
63
- Level {
64
- name : Some ( name. into ( ) . 0 ) ,
65
- level : self . level ,
66
- }
67
- }
68
-
69
- /// Do not show the [`Level`]s name
70
- pub fn no_name ( self ) -> Level < ' a > {
71
- self . with_name ( None :: < & str > )
72
- }
73
53
}
74
54
75
55
impl < ' a > Level < ' a > {
@@ -84,6 +64,15 @@ impl<'a> Level<'a> {
84
64
/// not allowed to be passed to this function.
85
65
///
86
66
/// </div>
67
+ ///
68
+ /// # Example
69
+ ///
70
+ /// ```rust
71
+ /// # use annotate_snippets::{Group, Snippet, AnnotationKind, Level};
72
+ /// let input = &[
73
+ /// Group::with_title(Level::ERROR.title("mismatched types").id("E0308"))
74
+ /// ];
75
+ /// ```
87
76
pub fn title ( self , text : impl Into < Cow < ' a , str > > ) -> Title < ' a > {
88
77
Title {
89
78
level : self ,
@@ -102,6 +91,20 @@ impl<'a> Level<'a> {
102
91
/// used to normalize untrusted text before it is passed to this function.
103
92
///
104
93
/// </div>
94
+ ///
95
+ /// # Example
96
+ ///
97
+ /// ```rust
98
+ /// # use annotate_snippets::{Group, Snippet, AnnotationKind, Level};
99
+ /// let input = &[
100
+ /// Group::with_title(Level::ERROR.title("mismatched types").id("E0308"))
101
+ /// .element(
102
+ /// Level::NOTE
103
+ /// .no_name()
104
+ /// .message("expected reference `&str`\nfound reference `&'static [u8; 0]`"),
105
+ /// ),
106
+ /// ];
107
+ /// ```
105
108
pub fn message ( self , text : impl Into < Cow < ' a , str > > ) -> Message < ' a > {
106
109
Message {
107
110
level : self ,
@@ -126,6 +129,69 @@ impl<'a> Level<'a> {
126
129
}
127
130
}
128
131
132
+ /// # Customize the `Level`
133
+ impl < ' a > Level < ' a > {
134
+ /// Replace the name describing this [`Level`]
135
+ ///
136
+ /// <div class="warning">
137
+ ///
138
+ /// Text passed to this function is considered "untrusted input", as such
139
+ /// all text is passed through a normalization function. Pre-styled text is
140
+ /// not allowed to be passed to this function.
141
+ ///
142
+ /// </div>
143
+ ///
144
+ /// # Example
145
+ ///
146
+ /// ```rust
147
+ #[ doc = include_str ! ( "../examples/custom_level.rs" ) ]
148
+ /// ```
149
+ #[ doc = include_str ! ( "../examples/custom_level.svg" ) ]
150
+ pub fn with_name ( self , name : impl Into < OptionCow < ' a > > ) -> Level < ' a > {
151
+ Level {
152
+ name : Some ( name. into ( ) . 0 ) ,
153
+ level : self . level ,
154
+ }
155
+ }
156
+
157
+ /// Do not show the [`Level`]s name
158
+ ///
159
+ /// # Example
160
+ ///
161
+ /// ```rust
162
+ /// # use annotate_snippets::{Group, Snippet, AnnotationKind, Level};
163
+ ///let source = r#"fn main() {
164
+ /// let b: &[u8] = include_str!("file.txt"); //~ ERROR mismatched types
165
+ /// let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
166
+ /// }"#;
167
+ /// let input = &[
168
+ /// Group::with_title(Level::ERROR.title("mismatched types").id("E0308"))
169
+ /// .element(
170
+ /// Snippet::source(source)
171
+ /// .path("$DIR/mismatched-types.rs")
172
+ /// .annotation(
173
+ /// AnnotationKind::Primary
174
+ /// .span(105..131)
175
+ /// .label("expected `&str`, found `&[u8; 0]`"),
176
+ /// )
177
+ /// .annotation(
178
+ /// AnnotationKind::Context
179
+ /// .span(98..102)
180
+ /// .label("expected due to this"),
181
+ /// ),
182
+ /// )
183
+ /// .element(
184
+ /// Level::NOTE
185
+ /// .no_name()
186
+ /// .message("expected reference `&str`\nfound reference `&'static [u8; 0]`"),
187
+ /// ),
188
+ /// ];
189
+ /// ```
190
+ pub fn no_name ( self ) -> Level < ' a > {
191
+ self . with_name ( None :: < & str > )
192
+ }
193
+ }
194
+
129
195
#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
130
196
pub ( crate ) enum LevelInner {
131
197
Error ,
0 commit comments