Skip to main content

RichString

Struct RichString 

Source
pub struct RichString {
    pub elements: Vec<Element>,
}
Expand description

A string that can have different parts styled.

New lines will always appear as their own non styled element. The RichString is comprised of a collection of Elements that each hold a String and a combination of stylings. The available styles are:

  • **bold** → bold
  • *italic* → italic
  • _underline_ → underline

as specified in the Fountain specification. Emphasis is parsed in accordance to the CommonMark specification. Furthermore, these can be combined in any overlapping order. Use \ for a styling character to be ignored for style parsing.

§Examples

use rustwell::rich_string::RichString;

let mut rs = RichString::from("Hello **world!**");

assert_eq!(rs.elements[0].text, "Hello ".to_string());
assert_eq!(rs.elements[1].text, "world!".to_string());
assert!(rs.elements[1].is_bold());

Fields§

§elements: Vec<Element>

Implementations§

Source§

impl RichString

Source

pub fn new() -> Self

Create a new, empty, RichString.

Source

pub fn char_count(&self) -> usize

The total length of a RichString, meaning the total number of chars.

Source

pub fn get_char(&self, index: usize) -> Option<char>

Gets a char from a “global” index, meaning the index when viewing the RichString as a single string without any style attributes taken into account.

§Examples
use rustwell::rich_string::RichString;

let mut rs = RichString::from("He**llo**");

assert_eq!(rs.get_char(1), Some('e'));
assert_eq!(rs.get_char(3), Some('l'));
assert_eq!(rs.get_char(5), None);
Source

pub fn get_element_from_index(&self, index: usize) -> Option<(&Element, usize)>

Given a “global” index, gets the Element which contains it, and the “local” index pointing to that character in the element.

§Examples
use rustwell::rich_string::RichString;

let mut rs = RichString::from("He**ll**o");

assert!(matches!(rs.get_element_from_index(1), Some((_, 1))));
assert!(matches!(rs.get_element_from_index(2), Some((_, 0))));
assert!(matches!(rs.get_element_from_index(3), Some((_, 1))));
assert!(matches!(rs.get_element_from_index(4), Some((_, 0))));
Source

pub fn iter(&self) -> RichIterator<'_> ⓘ

Creates an char iterator over the RichString, without the style attributes of each char taken into account.

Source

pub fn append(&mut self, other: Self)

Appends a RichString to self. Will merge the last Element of self, and the first of the other if they have the same style attributes.

Source

pub fn push_str(&mut self, str: impl AsRef<str>)

Pushes a string onto the RichString. Will divide the string into multiple elements with different styles if input string can be parsed with styles.

Source

pub fn to_plain_string(&self) -> String

Converts the RichString to a plain String by just combining the string elements without adding delimiters.

§Examples
use rustwell::rich_string::RichString;

let mut rs = RichString::from("He**ll**o");

assert_eq!(rs.to_plain_string(), "Hello".to_string());

Trait Implementations§

Source§

impl Clone for RichString

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for RichString

Source§

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

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

impl Default for RichString

Source§

fn default() -> Self

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

impl Display for RichString

Source§

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

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

impl Eq for RichString

Source§

impl<T> From<T> for RichString
where T: AsRef<str>,

Source§

fn from(str: T) -> Self

Converts to this type from the input type.
Source§

impl Hash for RichString

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 PartialEq for RichString

Source§

fn eq(&self, other: &Self) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl StructuralPartialEq for RichString

Auto Trait Implementations§

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> ErasedDestructor for T
where T: 'static,

Source§

impl<T> Finish for T

Source§

fn finish(self)

Does nothing but move self, equivalent to drop.
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<U, T> ToOwnedObj<U> for T
where U: FromObjRef<T>,

Source§

fn to_owned_obj(&self, data: FontData<'_>) -> U

Convert this type into T, using the provided data to resolve any offsets.
Source§

impl<U, T> ToOwnedTable<U> for T
where U: FromTableRef<T>,

Source§

fn to_owned_table(&self) -> U

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

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

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

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.