Group

Struct Group 

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

A Title with supporting context within a Report

Decor is used to visually connect Elements of a Group.

Generally, you will create separate group’s for:

§Example

use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};
use anstyle::AnsiColor;
use anstyle::Effects;
use anstyle::Style;

fn main() {
    let source = r#"// Make sure "highlighted" code is colored purple

//@ compile-flags: --error-format=human --color=always
//@ edition:2018

use core::pin::Pin;
use core::future::Future;
use core::any::Any;

fn query(_: fn(Box<(dyn Any + Send + '_)>) -> Pin<Box<(
    dyn Future<Output = Result<Box<(dyn Any + 'static)>, String>> + Send + 'static
)>>) {}

fn wrapped_fn<'a>(_: Box<(dyn Any + Send)>) -> Pin<Box<(
    dyn Future<Output = Result<Box<(dyn Any + 'static)>, String>> + Send + 'static
)>> {
    Box::pin(async { Err("nope".into()) })
}

fn main() {
    query(wrapped_fn);
}"#;

    const MAGENTA: Style = AnsiColor::Magenta.on_default().effects(Effects::BOLD);
    let message = format!(
        "expected fn pointer `{MAGENTA}for<'a>{MAGENTA:#} fn(Box<{MAGENTA}(dyn Any + Send + 'a){MAGENTA:#}>) -> Pin<_>`
      found fn item `fn(Box<{MAGENTA}(dyn Any + Send + 'static){MAGENTA:#}>) -> Pin<_> {MAGENTA}{{wrapped_fn}}{MAGENTA:#}`",
    );

    let report = &[
        Group::with_title(Level::ERROR.primary_title("mismatched types").id("E0308"))
            .element(
                Snippet::source(source)
                    .path("$DIR/highlighting.rs")
                    .annotation(
                        AnnotationKind::Primary
                            .span(553..563)
                            .label("one type is more general than the other"),
                    )
                    .annotation(
                        AnnotationKind::Context
                            .span(547..552)
                            .label("arguments to this function are incorrect"),
                    ),
            )
            .element(Level::NOTE.message(&message)),
        Group::with_title(Level::NOTE.secondary_title("function defined here")).element(
            Snippet::source(source)
                .path("$DIR/highlighting.rs")
                .annotation(AnnotationKind::Context.span(200..333).label(""))
                .annotation(AnnotationKind::Primary.span(194..199)),
        ),
    ];

    let renderer = Renderer::styled().anonymized_line_numbers(true);
    anstream::println!("{}", renderer.render(report));
}
error[E0308]: mismatched types --> $DIR/highlighting.rs:21:11 | LL | query(wrapped_fn); | ----- ^^^^^^^^^^ one type is more general than the other | | | arguments to this function are incorrect | = note: expected fn pointer `for<'a> fn(Box<(dyn Any + Send + 'a)>) -> Pin<_>` found fn item `fn(Box<(dyn Any + Send + 'static)>) -> Pin<_> {wrapped_fn}` note: function defined here --> $DIR/highlighting.rs:10:4 | LL | fn query(_: fn(Box<(dyn Any + Send + '_)>) -> Pin<Box<( | ____^^^^^_- LL | | dyn Future<Output = Result<Box<(dyn Any + 'static)>, String>> + Send + 'static LL | | )>>) {} | |___-

Implementations§

Source§

impl<'a> Group<'a>

Source

pub fn with_title(title: Title<'a>) -> Self

Create group with a Title, deriving AnnotationKind::Primary from its Level

Source

pub fn with_level(level: Level<'a>) -> Self

Create a title-less group with a primary Level for AnnotationKind::Primary

§Example
use annotate_snippets::{AnnotationKind, Group, Level, Renderer, Snippet};

fn main() {
    let source = r#"# Docstring followed by a newline

def foobar(door, bar={}):
    """
    """
"#;

    let report = &[Group::with_level(Level::NOTE)
        .element(
            Snippet::source(source)
                .fold(false)
                .annotation(AnnotationKind::Primary.span(56..58).label("B006")),
        )
        .element(Level::HELP.message("Replace with `None`; initialize within function"))];

    let renderer = Renderer::styled();
    anstream::println!("{}", renderer.render(report));
}
| 1 | # Docstring followed by a newline 2 | 3 | def foobar(door, bar={}): | ^^ B006 4 | """ 5 | """ | = help: Replace with `None`; initialize within function
Source

pub fn element(self, section: impl Into<Element<'a>>) -> Self

Append an Element that adds context to the Title

Source

pub fn elements( self, sections: impl IntoIterator<Item = impl Into<Element<'a>>>, ) -> Self

Append Elements that adds context to the Title

Source

pub fn is_empty(&self) -> bool

Trait Implementations§

Source§

impl<'a> Clone for Group<'a>

Source§

fn clone(&self) -> Group<'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 Group<'a>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a> Freeze for Group<'a>

§

impl<'a> RefUnwindSafe for Group<'a>

§

impl<'a> Send for Group<'a>

§

impl<'a> Sync for Group<'a>

§

impl<'a> Unpin for Group<'a>

§

impl<'a> UnwindSafe for Group<'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.