Struct Level
pub struct Level<'a> {
pub(crate) name: Option<Option<Cow<'a, str>>>,
pub(crate) level: LevelInner,
}
Expand description
Severity level for [Title
]s and [Message
]s
Fields§
§name: Option<Option<Cow<'a, str>>>
§level: LevelInner
Implementations§
§impl<'a> Level<'a>
impl<'a> Level<'a>
pub fn title(self, text: impl Into<Cow<'a, str>>) -> Title<'a>
pub fn title(self, text: impl Into<Cow<'a, str>>) -> Title<'a>
A text [Element
][crate::Element] to start a [Group
][crate::Group]
See [Group::with_title
][crate::Group::with_title]
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
let input = &[
Group::with_title(Level::ERROR.title("mismatched types").id("E0308"))
];
pub fn message(self, text: impl Into<Cow<'a, str>>) -> Message<'a>
pub fn message(self, text: impl Into<Cow<'a, str>>) -> Message<'a>
A text [Element
][crate::Element] in a [Group
][crate::Group]
Text passed to this function is allowed to be pre-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.
§Example
let input = &[
Group::with_title(Level::ERROR.title("mismatched types").id("E0308"))
.element(
Level::NOTE
.no_name()
.message("expected reference `&str`\nfound reference `&'static [u8; 0]`"),
),
];
§impl<'a> Level<'a>
§Customize the Level
impl<'a> Level<'a>
§Customize the Level
pub 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::OutputTheme;
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 message = &[
Group::with_title(
Level::ERROR
.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"))
.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().theme(OutputTheme::Unicode);
anstream::println!("{}", renderer.render(message));
}
pub fn no_name(self) -> Level<'a>
pub fn no_name(self) -> Level<'a>
Do not show the Level
s name
§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 input = &[
Group::with_title(Level::ERROR.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]`"),
),
];
Trait Implementations§
§impl<'a> Ord for Level<'a>
impl<'a> Ord for Level<'a>
§impl<'a> PartialOrd for Level<'a>
impl<'a> PartialOrd for Level<'a>
impl<'a> Eq for Level<'a>
impl<'a> StructuralPartialEq for Level<'a>
Auto Trait Implementations§
impl<'a> Freeze for Level<'a>
impl<'a> RefUnwindSafe for Level<'a>
impl<'a> Send for Level<'a>
impl<'a> Sync for Level<'a>
impl<'a> Unpin for Level<'a>
impl<'a> UnwindSafe for Level<'a>
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
§impl<Q, K> Comparable<K> for Q
impl<Q, K> Comparable<K> for Q
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key
and return true
if they are equal.§impl<I, T> ExtractContext<I, ()> for T
impl<I, T> ExtractContext<I, ()> for T
§fn extract_context(self, _original_input: I)
fn extract_context(self, _original_input: I)
§impl<T> Instrument for T
impl<T> Instrument for T
§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left
is true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self
into a Left
variant of Either<Self, Self>
if into_left(&self)
returns true
.
Converts self
into a Right
variant of Either<Self, Self>
otherwise. Read more