summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2021-11-30 17:44:17 +0100
committerMartin Fischer <martin@push-f.com>2021-12-05 02:52:36 +0100
commitbb24c73a06683c116eba3ad17aebdc87a5302529 (patch)
tree6af3f8ae038c2de987e6408293b043313fb10930
parenta22adb602c6eaed59256c8af0ae8d718a11ad5e3 (diff)
spans: introduce PosTracker
-rw-r--r--src/spans.rs37
1 files changed, 36 insertions, 1 deletions
diff --git a/src/spans.rs b/src/spans.rs
index 663bd59..1fc17c1 100644
--- a/src/spans.rs
+++ b/src/spans.rs
@@ -4,7 +4,7 @@ use std::{
mem,
};
-use crate::{Doctype, Emitter, EndTag, Error, StartTag, Token};
+use crate::{Doctype, Emitter, EndTag, Error, Reader, StartTag, Token};
type Span = std::ops::Range<usize>;
@@ -12,6 +12,41 @@ pub trait GetPos {
fn get_pos(&self) -> usize;
}
+struct PosTracker<R> {
+ reader: R,
+ position: usize,
+}
+
+impl<R> GetPos for PosTracker<R> {
+ fn get_pos(&self) -> usize {
+ self.position
+ }
+}
+
+impl<R: Reader> Reader for PosTracker<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),
+ }
+ }
+}
+
/// The default implementation of [`crate::Emitter`], used to produce ("emit") tokens.
pub struct SpanEmitter<R> {
current_characters: String,