Skip to main content

MarkdownLink

Struct MarkdownLink 

Source
#[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

pub fn kind(&self) -> LinkKind

Which syntax it was written in.

Source

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.

Source

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.

Source

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.

Source

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§

fn clone(&self) -> MarkdownLink

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§

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

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

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

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

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

Inequality operator !=. Read more

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

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

Source§

type Output = T

Should always be Self
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 = !

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.