#[non_exhaustive]pub struct MarkdownLink { /* private fields */ }Expand description
One Markdown link found on one line by markdown_links.
§Its invariants are enforced, not merely documented
Every claim the accessors below make is made true by MarkdownLink::new,
which is the only constructor this crate has, and the fields are private,
so the guarantees hold for the whole life of the value rather than only at
the moment it is built.
Both halves were earned. Three rounds of review on #806 and #807 each turned
up a field whose doc comment stated a guarantee its constructor did not keep —
target promised “trimmed, and never empty” while the angle-destination
branch could return whitespace — so the invariants moved into new. The
round after that pointed out that new was only half the job: the fields
were pub, so a caller holding one could assign to target, scope or
span afterwards and break every one of them, including the one that keeps
&line[link.span()] from panicking. A guarantee that lasts until somebody
writes to a field is not a guarantee, and this is now the single link scanner
every crate in the workspace reads through. Hence accessors.
#[non_exhaustive] is kept for what it is actually for — letting a field be
added later without breaking a downstream pattern — rather than for the
invariant, which privacy now carries on its own. New fields belong in new
as much as they belong here.
Implementations§
Source§impl MarkdownLink
impl MarkdownLink
Sourcepub fn target(&self) -> &str
pub fn target(&self) -> &str
Where it points: the inner text for a wiki-link, the destination for an inline one.
Trimmed, and never empty — a link naming nothing is not a link, which
is the reading that cannot invent an edge out of stray punctuation. Held
by MarkdownLink::new, which refuses to build one otherwise.
Sourcepub fn text(&self) -> &str
pub fn text(&self) -> &str
What a reader sees.
For an inline link that is its bracketed text, which is the half a citation label needs and which no scanner here used to keep. For a wiki-link the visible text is the target, so this repeats it rather than being empty: a caller labelling links does not have to know which kind it is holding. Derived from the kind rather than passed in, so those two sentences cannot come apart.
Sourcepub fn scope(&self) -> LinkScope
pub fn scope(&self) -> LinkScope
Whether target names something outside this repository.
Always LinkScope::Internal for a wiki-link, which addresses a graph
node by key and cannot name a URL — also derived from the kind.
Sourcepub fn span(&self) -> Range<usize> ⓘ
pub fn span(&self) -> Range<usize> ⓘ
The byte range the whole link occupies in the line as given, so a caller can rewrite it in place.
Code spans are excluded from the scan but not from this range: a link
whose brackets straddle one covers it. Always non-empty, inside the line,
and on character boundaries, so &line[link.span()] cannot panic.
Trait Implementations§
Source§impl Clone for MarkdownLink
impl Clone for MarkdownLink
Source§fn clone(&self) -> MarkdownLink
fn clone(&self) -> MarkdownLink
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for MarkdownLink
impl Debug for MarkdownLink
impl Eq for MarkdownLink
Source§impl PartialEq for MarkdownLink
impl PartialEq for MarkdownLink
impl StructuralPartialEq for MarkdownLink
Auto Trait Implementations§
impl Freeze for MarkdownLink
impl RefUnwindSafe for MarkdownLink
impl Send for MarkdownLink
impl Sync for MarkdownLink
impl Unpin for MarkdownLink
impl UnsafeUnpin for MarkdownLink
impl UnwindSafe for MarkdownLink
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.