pub struct ParsedText {
pub text: String,
pub spans: Vec<Span>,
pub links: Vec<Link>,
}Expand description
The output of every parser in this crate.
Fields§
§text: StringThe text to draw, with all markup removed.
spans: Vec<Span>Styled runs, sorted by start offset and non-overlapping. Runs with a wholly default style are omitted rather than materialised, so a plain message parses to zero spans.
links: Vec<Link>Link targets, indexed by LinkId.
Implementations§
Source§impl ParsedText
impl ParsedText
Sourcepub fn compact(&mut self)
pub fn compact(&mut self)
Give back the spare capacity building left behind.
A parser pushes spans one at a time, so a line with two styled runs
typically holds room for four; kept for the life of the scrollback,
that slack is the largest single cost of a row. The buffer compacts
every message it takes (see Message::compact).
By copying into an exact allocation rather than shrink_to_fit:
shrinking in place splits every block into an odd-sized remainder,
and a scrollback’s worth of those fragments the heap badly enough
that appending runs three times slower and scrolling five. The copy
frees whole blocks the allocator can hand straight back out.
Sourcepub fn plain(text: impl Into<String>) -> ParsedText
pub fn plain(text: impl Into<String>) -> ParsedText
A plain, unstyled string.
pub fn is_empty(&self) -> bool
pub fn len(&self) -> usize
Sourcepub fn style_at(&self, at: usize) -> Style
pub fn style_at(&self, at: usize) -> Style
The style in effect at byte offset at.
Linear over spans; callers walking the whole text in order should
iterate spans directly instead. Present for hit-test and test
assertions, which touch one offset at a time.
Sourcepub fn add_link(
&mut self,
range: Range<usize>,
href: impl Into<String>,
) -> Option<LinkId>
pub fn add_link( &mut self, range: Range<usize>, href: impl Into<String>, ) -> Option<LinkId>
Mark range as a hyperlink, returning its id.
Used for autodetected URLs, which are found after parsing — markdown links come out of the parser already resolved. The difference matters: an autodetected URL can land anywhere, including straddling existing style runs, so this splits spans at the boundaries rather than assuming a clean fit.
Returns None for an empty or out-of-bounds range, or one that
isn’t on char boundaries — a detector working in bytes shouldn’t
be able to corrupt the span list.
Sourcepub fn debug_assert_well_formed(&self)
pub fn debug_assert_well_formed(&self)
Panics (in debug) unless the spans are sorted, non-empty, non-overlapping and inside the text. Called by the parsers’ tests; cheap enough to also call in debug builds of callers.