#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Marker {
index: usize,
line: usize,
column: usize,
}
impl Marker {
#[must_use]
pub const fn new(index: usize, line: usize, column: usize) -> Self {
Self {
index,
line,
column,
}
}
#[must_use]
pub const fn index(self) -> usize {
self.index
}
#[must_use]
pub const fn line(self) -> usize {
self.line
}
#[must_use]
pub const fn column(self) -> usize {
self.column
}
#[must_use]
pub const fn location(self) -> Location {
Location::new(self.line, self.column)
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Location {
line: usize,
column: usize,
}
impl Location {
#[must_use]
pub const fn new(line: usize, column: usize) -> Self {
Self { line, column }
}
#[must_use]
pub const fn line(self) -> usize {
self.line
}
#[must_use]
pub const fn column(self) -> usize {
self.column
}
}
impl From<Marker> for Location {
fn from(marker: Marker) -> Self {
marker.location()
}
}
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq, Hash)]
pub struct Span {
start: Marker,
end: Marker,
}
impl Span {
#[must_use]
pub const fn new(start: Marker, end: Marker) -> Self {
Self { start, end }
}
#[must_use]
pub const fn empty(marker: Marker) -> Self {
Self {
start: marker,
end: marker,
}
}
#[must_use]
pub const fn start(self) -> Marker {
self.start
}
#[must_use]
pub const fn end(self) -> Marker {
self.end
}
#[must_use]
pub const fn len(self) -> usize {
self.end.index - self.start.index
}
#[must_use]
pub const fn is_empty(self) -> bool {
self.len() == 0
}
#[must_use]
pub const fn contains(self, index: usize) -> bool {
index >= self.start.index && index < self.end.index
}
#[must_use]
pub fn slice(self, source: &str) -> Option<&str> {
source.get(self.start.index..self.end.index)
}
}
#[cfg(test)]
mod tests {
use super::{Location, Marker, Span};
#[test]
fn markers_are_one_based_and_byte_indexed() {
let marker = Marker::new(4, 2, 3);
assert_eq!(marker.index(), 4);
assert_eq!(marker.line(), 2);
assert_eq!(marker.column(), 3);
assert_eq!(marker.location(), Location::new(2, 3));
}
#[test]
fn spans_are_half_open_and_slice_utf8() {
let text = "é: 1\n";
let start = Marker::new(0, 1, 1);
let end = Marker::new(2, 1, 2);
let span = Span::new(start, end);
assert_eq!(span.len(), 2);
assert_eq!(span.slice(text), Some("é"));
assert!(!span.contains(2));
assert!(span.contains(1));
}
#[test]
fn invalid_utf8_boundary_is_none() {
let text = "é";
let span = Span::new(Marker::new(0, 1, 1), Marker::new(1, 1, 2));
assert_eq!(span.slice(text), None);
}
#[test]
fn empty_span_has_zero_length() {
let span = Span::empty(Marker::new(3, 1, 4));
assert!(span.is_empty());
assert_eq!(span.len(), 0);
}
}