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>

§Constructors

pub const ERROR: Level<'a> = ERROR

pub const WARNING: Level<'a> = WARNING

pub const INFO: Level<'a> = INFO

pub const NOTE: Level<'a> = NOTE

pub const HELP: Level<'a> = HELP

§

impl<'a> Level<'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>

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

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));
}
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; ╰╴

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

Do not show the Levels 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> Clone for Level<'a>

§

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
§

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

§

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

Formats the value using the given formatter. Read more
§

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

§

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
§

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

§

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.
§

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

§

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
§

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> 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
§

impl<Q, K> Comparable<K> for Q
where Q: Ord + ?Sized, K: Borrow<Q> + ?Sized,

§

fn compare(&self, key: &K) -> Ordering

Compare self to key and return their ordering.
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
§

impl<I, T> ExtractContext<I, ()> for T

§

fn extract_context(self, _original_input: I)

Given the context attached to a nom error, and given the original input to the nom parser, extract more the useful context information. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

§

impl<T> Instrument for T

§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided [Span], returning an Instrumented wrapper. Read more
§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
§

impl<I> RecreateContext<I> for I

§

fn recreate_context(_original_input: I, tail: I) -> I

Given the original input, as well as the context reported by nom, recreate a context in the original string where the error occurred. Read more
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.
§

impl<T> WithSubscriber for T

§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a [WithDispatch] wrapper. Read more
§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a [WithDispatch] wrapper. Read more