Skip to main content

Annotations

Struct Annotations 

Source
pub struct Annotations<'a, M> { /* private fields */ }
Expand description

A collection of annotations attached to a Snippet.

§Spans

Each annotation is a Range<usize> whose indices are positions in the snippet’s source unit sequence (as defined by the concrete Snippet implementation).

Spans are half-open: start is inclusive, end is exclusive. Zero-length spans (start == end) are allowed and render as a single caret pointing at that position.

§Labels

A label is stored as a list of (String, M) fragments. This allows attaching different metadata to different parts of the label (for example, to highlight a keyword inside the label). If you do not need per-fragment metadata, you can use a single-element vector.

Empty label fragments are allowed; the renderer will simply output nothing for them (just the carets or connector lines of the annotation).

§Example

See the crate-level documentation.

Implementations§

Source§

impl<'a, M> Annotations<'a, M>

Source

pub fn new(snippet: &'a Snippet, main_style: &'a MainStyle<M>) -> Self

Creates a new annotation collection for snippet.

Source

pub fn add_annotation( &mut self, span: Range<usize>, style: &'a AnnotStyle<M>, label: Vec<(String, M)>, )

Adds an annotation span with the given style and label.

span is a half-open range (start inclusive, end exclusive) expressed in the snippet’s source units (see Annotations docs).

label is a list of text fragments with associated metadata; these fragments are concatenated when rendered.

Source

pub fn max_line_no_width(&self) -> usize

Source

pub fn render<O: Output<M>>( &self, max_line_no_width: usize, max_fill_after_first: usize, max_fill_before_last: usize, out: O, ) -> Result<(), O::Error>

Renders the snippet with the annotations.

If no annotations have been added, this outputs nothing.

max_line_no_width should be at least self.max_line_no_width(), but it can be greater to align vertically the margin of multiple snippets.

max_fill_after_first and max_fill_before_last control how many unannotated lines are rendered when there is a gap between two annotated lines:

  • If the gap size is greater than max_fill_after_first + max_fill_before_last, the renderer outputs only the first max_fill_after_first lines after the previous annotated line, then a dotted separator line, then the last max_fill_before_last lines before the next annotated line.
  • Otherwise, all lines in the gap are rendered.

Trait Implementations§

Source§

impl<'a, M: Debug> Debug for Annotations<'a, M>

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

§

impl<'a, M> Freeze for Annotations<'a, M>

§

impl<'a, M> RefUnwindSafe for Annotations<'a, M>
where M: RefUnwindSafe,

§

impl<'a, M> Send for Annotations<'a, M>
where M: Sync + Send,

§

impl<'a, M> Sync for Annotations<'a, M>
where M: Sync,

§

impl<'a, M> Unpin for Annotations<'a, M>
where M: Unpin,

§

impl<'a, M> UnsafeUnpin for Annotations<'a, M>

§

impl<'a, M> UnwindSafe for Annotations<'a, M>

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