diff options
author | Martin Fischer <martin@push-f.com> | 2023-08-17 08:59:05 +0200 |
---|---|---|
committer | Martin Fischer <martin@push-f.com> | 2023-08-19 11:41:55 +0200 |
commit | c169e78f120ea9be451f337306b8bff6c1fb4955 (patch) | |
tree | d04dd60435a2191f3d34b3680a87d92ff5df0edc /src/spans.rs | |
parent | 91074b6e7e6e8463f15ca26bc39e70b80f954227 (diff) |
refactor!: remove Span trait, just use Range
`std::mem::size_of::<Range<NoopOffset>>()` is 0
so there's no need to abstract over Range.
Diffstat (limited to 'src/spans.rs')
-rw-r--r-- | src/spans.rs | 118 |
1 files changed, 0 insertions, 118 deletions
diff --git a/src/spans.rs b/src/spans.rs deleted file mode 100644 index 14392cd..0000000 --- a/src/spans.rs +++ /dev/null @@ -1,118 +0,0 @@ -//! Source code spans. -//! -//! The [`DefaultEmitter`](crate::DefaultEmitter) is generic over a [`Span`]. -//! This library comes with two Span implementations: -//! -//! * 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<usize>`]. -//! You can easily use any existing reader by wrapping it in the [`PosTrackingReader`] struct -//! which implements the [`Position<usize>`] trait and takes care of tracking the current position. - -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<T> { - /// Returns the byte index of the current position. - fn position(&self) -> T; -} - -impl<R: Reader> Position<NoopOffset> for R { - fn position(&self) -> NoopOffset { - NoopOffset - } -} - -/// Wraps a [`Reader`] so that it implements [`Position<usize>`]. -pub struct PosTrackingReader<R> { - /// The wrapped reader. - reader: R, - /// The current position. - position: usize, -} - -impl<R> PosTrackingReader<R> { - /// 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(), - position: 0, - } - } -} - -impl<R> Position<usize> for PosTrackingReader<R> { - fn position(&self) -> usize { - self.position - } -} - -/// A byte range in the source code. -pub trait Span: Default + Clone { - type Offset: Add<usize, Output = Self::Offset>; - - /// 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 Span for () { - type Offset = NoopOffset; - - fn new(_start: Self::Offset, _end: Self::Offset) -> Self { - () - } - - fn push_str(&mut self, _str: &str) {} -} - -impl Add<usize> for NoopOffset { - type Output = Self; - - fn add(self, _rhs: usize) -> NoopOffset { - self - } -} - -impl Span for Range<usize> { - type Offset = usize; - - fn new(start: Self::Offset, end: Self::Offset) -> Self { - start - 1..end - 1 - } - - fn push_str(&mut self, str: &str) { - self.end += str.len(); - } -} - -impl<R: Reader> Reader for PosTrackingReader<R> { - type Error = R::Error; - - fn read_char(&mut self) -> Result<Option<char>, Self::Error> { - match self.reader.read_char()? { - Some(char) => { - self.position += char.len_utf8(); - Ok(Some(char)) - } - None => Ok(None), - } - } - - fn try_read_string(&mut self, s: &str, case_sensitive: bool) -> Result<bool, Self::Error> { - match self.reader.try_read_string(s, case_sensitive)? { - true => { - self.position += s.len(); - Ok(true) - } - false => Ok(false), - } - } -} |