aboutsummaryrefslogtreecommitdiff
path: root/src/spans.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/spans.rs')
-rw-r--r--src/spans.rs57
1 files changed, 36 insertions, 21 deletions
diff --git a/src/spans.rs b/src/spans.rs
index 89595aa..14392cd 100644
--- a/src/spans.rs
+++ b/src/spans.rs
@@ -6,21 +6,29 @@
//! * one for `()` which acts as the no-op implementation for when you don't want to track spans
//! * one for [`Range<usize>`] for when you do want to track spans
//!
-//! To use the latter your reader however has to implement [`Position`].
+//! To use the latter your reader however has to implement [`Position<usize>`].
//! You can easily use any existing reader by wrapping it in the [`PosTrackingReader`] struct
-//! which implements the [`Position`] trait and takes care of tracking the current position.
+//! which implements the [`Position<usize>`] trait and takes care of tracking the current position.
-use std::ops::Range;
+use std::ops::{Add, Range};
use crate::reader::{IntoReader, Reader};
+pub struct NoopOffset;
+
/// A trait to be implemented by readers that track their own position.
-pub trait Position {
+pub trait Position<T> {
/// Returns the byte index of the current position.
- fn position(&self) -> usize;
+ fn position(&self) -> T;
+}
+
+impl<R: Reader> Position<NoopOffset> for R {
+ fn position(&self) -> NoopOffset {
+ NoopOffset
+ }
}
-/// Wraps a [`Reader`] so that it implements [`Position`].
+/// Wraps a [`Reader`] so that it implements [`Position<usize>`].
pub struct PosTrackingReader<R> {
/// The wrapped reader.
reader: R,
@@ -29,7 +37,7 @@ pub struct PosTrackingReader<R> {
}
impl<R> PosTrackingReader<R> {
- /// Wraps the given [`Reader`] so that it implements [`Position`] with the position starting from 0.
+ /// Wraps the given [`Reader`] so that it implements [`Position<usize>`] with the position starting from 0.
pub fn new<'a>(into_reader: impl IntoReader<'a, Reader = R>) -> Self {
Self {
reader: into_reader.into_reader(),
@@ -38,39 +46,46 @@ impl<R> PosTrackingReader<R> {
}
}
-impl<R> Position for PosTrackingReader<R> {
+impl<R> Position<usize> for PosTrackingReader<R> {
fn position(&self) -> usize {
self.position
}
}
/// A byte range in the source code.
-pub trait Span<R>: Default + Clone {
- /// Initializes a new span at the current position of the reader.
- fn from_reader(reader: &R) -> Self;
+pub trait Span: Default + Clone {
+ type Offset: Add<usize, Output = Self::Offset>;
- /// Initializes a new span at the current position of the reader with the given offset.
- fn from_reader_with_offset(reader: &R, offset: usize) -> Self;
+ /// Constructs a new span from the given byte offsets.
+ fn new(start: Self::Offset, end: Self::Offset) -> Self;
/// Extends the span by the length of the given string.
fn push_str(&mut self, str: &str);
}
-impl<R> Span<R> for () {
- fn from_reader(_reader: &R) -> Self {}
+impl Span for () {
+ type Offset = NoopOffset;
- fn from_reader_with_offset(_reader: &R, _offset: usize) -> Self {}
+ fn new(_start: Self::Offset, _end: Self::Offset) -> Self {
+ ()
+ }
fn push_str(&mut self, _str: &str) {}
}
-impl<P: Position> Span<P> for Range<usize> {
- fn from_reader(reader: &P) -> Self {
- reader.position() - 1..reader.position() - 1
+impl Add<usize> for NoopOffset {
+ type Output = Self;
+
+ fn add(self, _rhs: usize) -> NoopOffset {
+ self
}
+}
+
+impl Span for Range<usize> {
+ type Offset = usize;
- fn from_reader_with_offset(reader: &P, offset: usize) -> Self {
- reader.position() - 1 + offset..reader.position() - 1 + offset
+ fn new(start: Self::Offset, end: Self::Offset) -> Self {
+ start - 1..end - 1
}
fn push_str(&mut self, str: &str) {