pub struct Level<'a> { /* private fields */ }
Expand description
Implementations§
Source§impl<'a> Level<'a>
impl<'a> Level<'a>
Sourcepub fn primary_title(self, text: impl Into<Cow<'a, str>>) -> Title<'a>
pub fn primary_title(self, text: impl Into<Cow<'a, str>>) -> Title<'a>
Sourcepub fn secondary_title(self, text: impl Into<Cow<'a, str>>) -> Title<'a>
pub fn secondary_title(self, text: impl Into<Cow<'a, str>>) -> Title<'a>
For any secondary, or context, Group
s (subsequent) in a Report
Text passed to this function is allowed to be styled, as such all
text is considered “trusted input” and has no normalizations applied to
it. normalize_untrusted_str
can be
used to normalize untrusted text before it is passed to this function.
Sourcepub fn message(self, text: impl Into<Cow<'a, str>>) -> Message<'a>
pub fn message(self, text: impl Into<Cow<'a, str>>) -> Message<'a>
Text passed to this function is allowed to be styled, as such all
text is considered “trusted input” and has no normalizations applied to
it. normalize_untrusted_str
can be
used to normalize untrusted text before it is passed to this function.
Source§impl<'a> Level<'a>
§Customize the Level
impl<'a> Level<'a>
§Customize the Level
Sourcepub fn with_name(self, name: impl Into<OptionCow<'a>>) -> Level<'a>
pub fn with_name(self, name: impl Into<OptionCow<'a>>) -> Level<'a>
Replace the name describing this Level
Text passed to this function is considered “untrusted input”, as such all text is passed through a normalization function. Pre-styled text is not allowed to be passed to this function.
§Example
use annotate_snippets::renderer::DecorStyle;
use annotate_snippets::{AnnotationKind, Group, Level, Patch, Renderer, Snippet};
fn main() {
let source = r#"// Regression test for issue #114529
// Tests that we do not ICE during const eval for a
// break-with-value in contexts where it is illegal
#[allow(while_true)]
fn main() {
[(); {
while true {
break 9; //~ ERROR `break` with value from a `while` loop
};
51
}];
[(); {
while let Some(v) = Some(9) {
break v; //~ ERROR `break` with value from a `while` loop
};
51
}];
while true {
break (|| { //~ ERROR `break` with value from a `while` loop
let local = 9;
});
}
}
"#;
let report =
&[
Group::with_title(
Level::ERROR
.primary_title("`break` with value from a `while` loop")
.id("E0571"),
)
.element(
Snippet::source(source)
.line_start(1)
.path("$DIR/issue-114529-illegal-break-with-value.rs")
.annotation(
AnnotationKind::Primary
.span(483..581)
.label("can only break with a value inside `loop` or breakable block"),
)
.annotation(
AnnotationKind::Context
.span(462..472)
.label("you can't `break` with a value in a `while` loop"),
),
),
Group::with_title(Level::HELP.with_name(Some("suggestion")).secondary_title(
"use `break` on its own without a value inside this `while` loop",
))
.element(
Snippet::source(source)
.line_start(1)
.path("$DIR/issue-114529-illegal-break-with-value.rs")
.patch(Patch::new(483..581, "break")),
),
];
let renderer = Renderer::styled().decor_style(DecorStyle::Unicode);
anstream::println!("{}", renderer.render(report));
}
Sourcepub fn no_name(self) -> Level<'a>
pub fn no_name(self) -> Level<'a>
Do not show the Level
s name
Useful for:
- Another layer of the application will include the level (e.g. when rendering errors)
Message
s that are part of a previousGroup
Element
s
§Example
let source = r#"fn main() {
let b: &[u8] = include_str!("file.txt"); //~ ERROR mismatched types
let s: &str = include_bytes!("file.txt"); //~ ERROR mismatched types
}"#;
let report = &[
Group::with_title(Level::ERROR.primary_title("mismatched types").id("E0308"))
.element(
Snippet::source(source)
.path("$DIR/mismatched-types.rs")
.annotation(
AnnotationKind::Primary
.span(105..131)
.label("expected `&str`, found `&[u8; 0]`"),
)
.annotation(
AnnotationKind::Context
.span(98..102)
.label("expected due to this"),
),
)
.element(
Level::NOTE
.no_name()
.message("expected reference `&str`\nfound reference `&'static [u8; 0]`"),
),
];