@@ -36,40 +36,20 @@ pub const HELP: Level<'_> = Level {
3636 level : LevelInner :: Help ,
3737} ;
3838
39- /// [`Title`] severity level
39+ /// Severity level for [`Title`]s and [`Message`]s
4040#[ derive( Clone , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
4141pub struct Level < ' a > {
4242 pub ( crate ) name : Option < Option < Cow < ' a , str > > > ,
4343 pub ( crate ) level : LevelInner ,
4444}
4545
46+ /// # Constructors
4647impl < ' a > Level < ' a > {
4748 pub const ERROR : Level < ' a > = ERROR ;
4849 pub const WARNING : Level < ' a > = WARNING ;
4950 pub const INFO : Level < ' a > = INFO ;
5051 pub const NOTE : Level < ' a > = NOTE ;
5152 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- }
7353}
7454
7555impl < ' a > Level < ' a > {
@@ -84,6 +64,15 @@ impl<'a> Level<'a> {
8464 /// not allowed to be passed to this function.
8565 ///
8666 /// </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+ /// ```
8776 pub fn title ( self , text : impl Into < Cow < ' a , str > > ) -> Title < ' a > {
8877 Title {
8978 level : self ,
@@ -102,6 +91,20 @@ impl<'a> Level<'a> {
10291 /// used to normalize untrusted text before it is passed to this function.
10392 ///
10493 /// </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+ /// ```
105108 pub fn message ( self , text : impl Into < Cow < ' a , str > > ) -> Message < ' a > {
106109 Message {
107110 level : self ,
@@ -126,6 +129,69 @@ impl<'a> Level<'a> {
126129 }
127130}
128131
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+
129195#[ derive( Clone , Copy , Debug , PartialEq , Eq , PartialOrd , Ord ) ]
130196pub ( crate ) enum LevelInner {
131197 Error ,
0 commit comments