Struct SpannedString

Source
pub struct SpannedString<T> { /* private fields */ }
Expand description

A string with associated spans.

Each span has an associated attribute T.

Implementations§

Source§

impl SpannedString<Style>

Source

pub fn plain<S>(content: S) -> Self
where S: Into<String>,

Returns a plain StyledString without any style.

You got no style, Dutch. You know that.

Source

pub fn styled<S, T>(content: S, style: T) -> Self
where S: Into<String>, T: Into<Style>,

Creates a new StyledString using a single style for the entire text.

Source

pub fn append_plain<S>(&mut self, text: S)
where S: Into<String>,

Appends the given plain text to self.

Source

pub fn append_styled<S, T>(&mut self, text: S, style: T)
where S: Into<String>, T: Into<Style>,

Appends text to self, using style.

Source§

impl SpannedString<()>

Source

pub fn plain<S>(content: S) -> Self
where S: Into<String>,

Returns a simple spanned string without any attribute.

Source§

impl<T> SpannedString<T>

Source

pub fn new() -> Self

Returns an empty SpannedString.

Source

pub fn concatenate<I>(spans: I) -> Self
where I: IntoIterator<Item = Self>,

Concatenates all styled strings.

Same as spans.into_iter().collect().

Source

pub fn with_spans<S>(source: S, spans: Vec<IndexedSpan<T>>) -> Self
where S: Into<String>,

Creates a new SpannedString manually.

It is not recommended to use this directly. Instead, look for methods like Markdown::parse.

Source

pub fn canonicalize(&mut self)
where T: PartialEq,

Compacts and simplifies this string, resulting in a canonical form.

If two styled strings represent the same styled text, they should have equal canonical forms.

(The PartialEq implementation for StyledStrings requires both the source and spans to be equals, so non-visible changes such as text in the source between spans could cause StyledStrings to evaluate as non-equal.)

Source

pub fn canonical(self) -> Self
where T: PartialEq,

Returns the canonical form of this styled string.

Source

pub fn compact(&mut self)

Compacts the source to only include the spans content.

This does not change the number of spans, but changes the source.

Source

pub fn simplify(&mut self)
where T: PartialEq,

Attemps to reduce the number of spans by merging consecutive similar ones.

Source

pub fn trim_end(&mut self)

Shrink the source to discard any unused suffix.

Source

pub fn trim_start(&mut self)

Shrink the source to discard any unused prefix.

Source

pub fn trim(&mut self)

Shrink the source to discard any unused prefix or suffix.

Source

pub fn single_span<S>(source: S, attr: T) -> Self
where S: Into<String>,

Returns a new SpannedString with a single span.

Source

pub fn append<S>(&mut self, other: S)
where S: Into<Self>,

Appends the given StyledString to self.

Source

pub fn append_raw(&mut self, source: &str, spans: Vec<IndexedSpan<T>>)

Appends content and its corresponding spans to the end.

It is not recommended to use this directly; instead, look at the append method.

Source

pub fn remove_spans<R>(&mut self, range: R)
where R: RangeBounds<usize>,

Remove the given range of spans from the styled string.

You may want to follow this with either compact(), trim_start() or trim_end().

Source

pub fn spans( &self, ) -> impl DoubleEndedIterator<Item = Span<'_, T>> + ExactSizeIterator<Item = Span<'_, T>>

Iterates on the resolved spans.

Source

pub fn spans_attr_mut(&mut self) -> impl Iterator<Item = SpanMut<'_, T>>

Iterates on the resolved spans, with mutable access to the attributes.

Source

pub fn spans_raw(&self) -> &[IndexedSpan<T>]

Returns a reference to the indexed spans.

Source

pub fn spans_raw_attr_mut( &mut self, ) -> impl DoubleEndedIterator<Item = IndexedSpanRefMut<'_, T>> + ExactSizeIterator<Item = IndexedSpanRefMut<'_, T>>

Returns a mutable iterator on the spans of this string.

This can be used to modify the style of each span.

Source

pub fn source(&self) -> &str

Returns a reference to the source string.

This is the non-parsed string.

Source

pub fn into_source(self) -> String

Get the source, consuming this StyledString.

Source

pub fn is_empty(&self) -> bool

Returns true if self is empty.

Source

pub fn width(&self) -> usize

Returns the width taken by this string.

This is the sum of the width of each span.

Trait Implementations§

Source§

impl<T: Clone> Clone for SpannedString<T>

Source§

fn clone(&self) -> SpannedString<T>

Returns a copy 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<T: Debug> Debug for SpannedString<T>

Source§

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

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

impl<T> Default for SpannedString<T>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a, T> From<&'a SpannedString<T>> for SpannedStr<'a, T>

Source§

fn from(other: &'a SpannedString<T>) -> Self

Converts to this type from the input type.
Source§

impl<S, T> From<S> for SpannedString<T>
where S: Into<String>, T: Default,

Source§

fn from(value: S) -> Self

Converts to this type from the input type.
Source§

impl<T> FromIterator<SpannedString<T>> for SpannedString<T>

Source§

fn from_iter<I: IntoIterator<Item = SpannedString<T>>>( iter: I, ) -> SpannedString<T>

Creates a value from an iterator. Read more
Source§

impl<T: Hash> Hash for SpannedString<T>

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl<T: PartialEq> PartialEq for SpannedString<T>

Source§

fn eq(&self, other: &SpannedString<T>) -> 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, T> SpannedText for &'a SpannedString<T>

Source§

type S = IndexedSpan<T>

Type of span returned by SpannedText::spans(). Read more
Source§

fn source(&self) -> &str

Returns the source text.
Source§

fn spans(&self) -> &[IndexedSpan<T>]

Returns the spans for this text.
Source§

fn as_ref(&self) -> SpannedTextRef<'_, Self>

Returns a SpannedText by reference.
Source§

impl<T: Eq> Eq for SpannedString<T>

Source§

impl<T> StructuralPartialEq for SpannedString<T>

Auto Trait Implementations§

§

impl<T> Freeze for SpannedString<T>

§

impl<T> RefUnwindSafe for SpannedString<T>
where T: RefUnwindSafe,

§

impl<T> Send for SpannedString<T>
where T: Send,

§

impl<T> Sync for SpannedString<T>
where T: Sync,

§

impl<T> Unpin for SpannedString<T>
where T: Unpin,

§

impl<T> UnwindSafe for SpannedString<T>
where T: UnwindSafe,

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<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

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

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

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

Source§

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

Compare self to key and return true if they are equal.
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.
Source§

impl<T> With for T

Source§

fn wrap_with<U, F: FnOnce(Self) -> U>(self, f: F) -> U

Calls the given closure and return the result. Read more
Source§

fn with<F: FnOnce(&mut Self)>(self, f: F) -> Self

Calls the given closure on self.
Source§

fn try_with<E, F>(self, f: F) -> Result<Self, E>
where F: FnOnce(&mut Self) -> Result<(), E>,

Calls the given closure on self.
Source§

fn with_if<F>(self, condition: bool, f: F) -> Self
where F: FnOnce(&mut Self),

Calls the given closure if condition == true.