pub struct Span { /* private fields */ }
Expand description
A Span
is metadata which indicates the start and end positions.
Span
s are combined with AnchorLocation
s to form another type of metadata, a Tag
.
A Span
’s end position must be greater than or equal to its start position.
Implementations§
Source§impl Span
impl Span
pub fn from_list(list: &[impl HasSpan]) -> Span
Sourcepub fn new(start: usize, end: usize) -> Span
pub fn new(start: usize, end: usize) -> Span
Creates a new Span
from start and end inputs. The end parameter must be greater than or equal to the start parameter.
pub fn new_option(start: usize, end: usize) -> Option<Span>
Sourcepub fn for_char(pos: usize) -> Span
pub fn for_char(pos: usize) -> Span
Creates a Span
with a length of 1 from the given position.
§Example
let char_span = Span::for_char(5);
assert_eq!(char_span.start(), 5);
assert_eq!(char_span.end(), 6);
Sourcepub fn contains(&self, pos: usize) -> bool
pub fn contains(&self, pos: usize) -> bool
Returns a bool indicating if the given position falls inside the current Span
.
§Example
let span = Span::new(2, 8);
assert_eq!(span.contains(5), true);
assert_eq!(span.contains(8), false);
assert_eq!(span.contains(100), false);
Sourcepub fn since(&self, other: impl Into<Span>) -> Span
pub fn since(&self, other: impl Into<Span>) -> Span
Returns a new Span by merging an earlier Span with the current Span.
The resulting Span will have the same start position as the given Span and same end as the current Span.
§Example
let original_span = Span::new(4, 6);
let earlier_span = Span::new(1, 3);
let merged_span = origin_span.since(earlier_span);
assert_eq!(merged_span.start(), 1);
assert_eq!(merged_span.end(), 6);
Sourcepub fn until(&self, other: impl Into<Span>) -> Span
pub fn until(&self, other: impl Into<Span>) -> Span
Returns a new Span by merging a later Span with the current Span.
The resulting Span will have the same start position as the current Span and same end as the given Span.
§Example
let original_span = Span::new(4, 6);
let later_span = Span::new(9, 11);
let merged_span = origin_span.until(later_span);
assert_eq!(merged_span.start(), 4);
assert_eq!(merged_span.end(), 11);
pub fn merge(&self, other: impl Into<Span>) -> Span
Sourcepub fn until_option(&self, other: Option<impl Into<Span>>) -> Span
pub fn until_option(&self, other: Option<impl Into<Span>>) -> Span
Returns a new Span by merging a later Span with the current Span.
If the given Span is of the None variant, A Span with the same values as the current Span is returned.
pub fn string(&self, source: &str) -> String
pub fn spanned_slice<'a>(&self, source: &'a str) -> Spanned<&'a str>
pub fn spanned_string(&self, source: &str) -> Spanned<String>
Sourcepub fn is_unknown(&self) -> bool
pub fn is_unknown(&self) -> bool
Returns a bool if the current Span indicates an “unknown” position.
§Example
let unknown_span = Span::unknown();
let known_span = Span::new(4, 6);
assert_eq!(unknown_span.is_unknown(), true);
assert_eq!(known_span.is_unknown(), false);