Level

Struct Level 

Source
pub struct Level<'a> { /* private fields */ }
Expand description

Severity level for Titles and Messages

§Example

let report = &[
    Group::with_title(
        Level::ERROR.primary_title("mismatched types").id("E0308")
    )
        .element(
            Level::NOTE.message("expected reference"),
        ),
    Group::with_title(
        Level::HELP.secondary_title("function defined here")
    ),
];

Implementations§

Source§

impl<'a> Level<'a>

§Constructors

Source

pub const ERROR: Level<'a> = ERROR

Source

pub const WARNING: Level<'a> = WARNING

Source

pub const INFO: Level<'a> = INFO

Source

pub const NOTE: Level<'a> = NOTE

Source

pub const HELP: Level<'a> = HELP

Source§

impl<'a> Level<'a>

Source

pub fn primary_title(self, text: impl Into<Cow<'a, str>>) -> Title<'a>

For the primary, or root cause, Group (the first) in a Report

See Group::with_title

Text passed to this function is considered “untrusted input”, as such all text is passed through a normalization function. Styled text is not allowed to be passed to this function.

Source

pub fn secondary_title(self, text: impl Into<Cow<'a, str>>) -> Title<'a>

For any secondary, or context, Groups (subsequent) in a Report

See Group::with_title

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

pub fn message(self, text: impl Into<Cow<'a, str>>) -> Message<'a>

A text Element in a Group

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

Source

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));
}
error[E0571]: `break` with value from a `while` loop ╭▸ $DIR/issue-114529-illegal-break-with-value.rs:22:9 21 while true { ────────── you can't `break` with a value in a `while` loop 22 break (|| { //~ ERROR `break` with value from a `while` loop 23 let local = 9; 24 }); ┗━━━━━━━━━━┛ can only break with a value inside `loop` or breakable block ╰╴ suggestion: use `break` on its own without a value inside this `while` loop ╭╴ 22 - break (|| { //~ ERROR `break` with value from a `while` loop 23 - let local = 9; 24 - }); 22 + break; ╰╴
Source

pub fn no_name(self) -> Level<'a>

Do not show the Levels name

Useful for:

  • Another layer of the application will include the level (e.g. when rendering errors)
  • Messages that are part of a previous Group Elements
§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]`"),
        ),
];

Trait Implementations§

Source§

impl<'a> Clone for Level<'a>

Source§

fn clone(&self) -> Level<'a>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<'a> Debug for Level<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'a> Ord for Level<'a>

Source§

fn cmp(&self, other: &Level<'a>) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl<'a> PartialEq for Level<'a>

Source§

fn eq(&self, other: &Level<'a>) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl<'a> PartialOrd for Level<'a>

Source§

fn partial_cmp(&self, other: &Level<'a>) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl<'a> Eq for Level<'a>

Source§

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> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.