Skip to main content

Span

Struct Span 

Source
#[non_exhaustive]
pub struct Span { pub start: Marker, pub end: Marker, pub indent: Option<usize>, pub tag_start: Option<Marker>, /* private fields */ }
Expand description

A range of locations in a YAML document.

Construct spans with Self::new, Self::empty, or Self::default, then use Self::with_indent to attach optional indentation metadata. Public fields remain readable and writable, but the type is non-exhaustive so future releases can add metadata. Destructuring patterns must include ...

use granit_parser::{Marker, Span};

let start = Marker::new(0, 1, 0);
let end = Marker::new(3, 1, 3);
let span = Span::new(start, end).with_indent(Some(0));
let Span { start: actual_start, end: actual_end, .. } = span;
assert_eq!(actual_start, start);
assert_eq!(actual_end, end);
assert_eq!(span.indent, Some(0));

Direct struct construction outside this crate is not supported:

ⓘ
use granit_parser::{Marker, Span};

let marker = Marker::new(0, 1, 0);
let span = Span { start: marker, end: marker, indent: None, tag_start: None };

Struct-update syntax is likewise unavailable; use the builders or assign individual fields:

ⓘ
use granit_parser::{Marker, Span};

let span = Span { indent: Some(2), ..Span::empty(Marker::new(0, 1, 0)) };

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§start: Marker

The start (inclusive) of the range.

§end: Marker

The end (exclusive) of the range.

§indent: Option<usize>

Optional indentation hint associated with this span.

This is only meaningful for certain parser-emitted events (notably: block mapping keys). When indentation is not meaningful or cannot be provided, it must be None.

§tag_start: Option<Marker>

Optional source marker for the explicit tag token attached to this node.

This is only meaningful for parser-emitted node events that carry a resolved tag, such as Event::Scalar, Event::SequenceStart, or Event::MappingStart. The normal Span::start and Span::end continue to cover the node value or collection; tag_start points to the tag token when that token appears at a different source location.

Implementations§

Source§

impl Span

Source

pub fn new(start: Marker, end: Marker) -> Span

Create a new Span for the given range.

Source

pub fn empty(mark: Marker) -> Span

Create an empty Span at a given location.

An empty span doesn’t contain any characters, but its position may still be meaningful. For example, for an indented sequence SequenceEnd has a location but an empty span.

Source

pub fn with_indent(self, indent: Option<usize>) -> Span

Return a copy of this Span with the given indentation hint.

Source

pub fn with_tag_start(self, tag_start: Option<Marker>) -> Span

👎Deprecated since 2.1.0:

Only sets a tag start (.tag_span() then returns None). Use with_tag_span.

Return a copy of this Span with the given explicit tag-token start marker.

Clears any stored tag length, so Self::tag_span returns None on the returned span. Retained for compatibility. Parser-emitted spans provide complete tag-token ranges through Self::tag_span.

Source

pub fn with_tag_span(self, tag_span: Option<(Marker, u32)>) -> Span

Attach the start and raw source length of a single-line ASCII tag token.

Source

pub fn tag_start(&self) -> Option<Marker>

Return the source marker of the explicit tag token attached to this node, if any.

The regular span still covers the node value or collection. This accessor is useful for diagnostics that should point at the tag itself, especially when a tagged block collection begins on a later line than the tag token.

Source

pub fn tag_span(&self) -> Option<Span>

Return the complete source span of the explicit tag attached to this node, if available.

Includes the leading !, any verbatim-tag delimiters, and URI escapes exactly as written. The node’s own Self::start and Self::end are unchanged. The returned span carries no indentation or tag metadata. With byte offsets, use Self::slice on it to obtain the raw tag spelling; streaming inputs retain character positions without requiring byte offsets.

Returns None for untagged nodes and tag tokens longer than u32::MAX source characters, or coordinates that would overflow. Oversized tags still parse normally and retain their start marker.

use granit_parser::Parser;

let source = "key: !note%20here value";
let tag_span = Parser::new_from_str(source)
    .filter_map(Result::ok)
    .find_map(|(_, span)| span.tag_span())
    .unwrap();
assert_eq!(tag_span.slice(source), Some("!note%20here"));
Source

pub fn len(&self) -> usize

Return the length of the span (in characters).

§Panics

Panics in debug builds if the end marker precedes the start marker.

Source

pub fn is_empty(&self) -> bool

Return whether the Span has a length of zero.

§Panics

Panics in debug builds if the end marker precedes the start marker.

Source

pub fn byte_range(&self) -> Option<Range<usize>>

Return the half-open UTF-8 byte range of the span, if both markers have byte offsets.

Spans emitted by Parser::new_from_str and its options variant always return Some, including empty ranges. Iterator inputs and manually constructed markers may lack offsets, so the return type is optional. A missing range is not the same as an empty range; do not substitute 0..0 for None.

Unlike Self::len and Marker::index, this range counts bytes, not characters. For manually constructed or modified spans, this method does not validate ordering, bounds, or UTF-8 boundaries. Use Self::slice for checked access to source text.

Source

pub fn slice<'source>(&self, source: &'source str) -> Option<&'source str>

Return the source text covered by this span, if byte offsets are available and the range is valid for the provided input.

Pass the same, unchanged source that produced the span. Bounds and UTF-8 boundaries are checked, but a span does not record source identity. The returned text is the raw event source, which can differ from a decoded scalar value or be empty for implicit events.

Trait Implementations§

Source§

impl Clone for Span

Source§

fn clone(&self) -> Span

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 Copy for Span

Source§

impl Debug for Span

Source§

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

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

impl Default for Span

Source§

fn default() -> Span

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

impl Eq for Span

Source§

impl PartialEq for Span

Source§

fn eq(&self, other: &Span) -> 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 Span

Auto Trait Implementations§

§

impl Freeze for Span

§

impl RefUnwindSafe for Span

§

impl Send for Span

§

impl Sync for Span

§

impl Unpin for Span

§

impl UnsafeUnpin for Span

§

impl UnwindSafe for Span

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<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 = !

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.