aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--src/spans.rs16
-rw-r--r--tests/test_spans.rs11
2 files changed, 16 insertions, 11 deletions
diff --git a/src/spans.rs b/src/spans.rs
index 4333bd1..c3d269d 100644
--- a/src/spans.rs
+++ b/src/spans.rs
@@ -12,7 +12,7 @@
use std::ops::Range;
-use crate::Reader;
+use crate::{IntoReader, Reader};
/// A trait to be implemented by readers that track their own position.
pub trait GetPos {
@@ -23,9 +23,19 @@ pub trait GetPos {
/// Wraps a [`Reader`] so that it implements [`GetPos`].
pub struct PosTrackingReader<R> {
/// The wrapped reader.
- pub reader: R,
+ reader: R,
/// The current position.
- pub position: usize,
+ position: usize,
+}
+
+impl<R> PosTrackingReader<R> {
+ /// Wraps the given [`Reader`] so that it implements [`GetPos`] 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> GetPos for PosTrackingReader<R> {
diff --git a/tests/test_spans.rs b/tests/test_spans.rs
index da3f09c..64a64cf 100644
--- a/tests/test_spans.rs
+++ b/tests/test_spans.rs
@@ -6,9 +6,7 @@ use codespan_reporting::{
files::SimpleFiles,
term::{self, termcolor::Buffer},
};
-use html5tokenizer::{
- spans::PosTrackingReader, DefaultEmitter, IntoReader, StringReader, Token, Tokenizer,
-};
+use html5tokenizer::{spans::PosTrackingReader, DefaultEmitter, Token, Tokenizer};
#[test]
fn test() {
@@ -19,11 +17,8 @@ fn test() {
let mut labels = Vec::new();
for token in Tokenizer::new_with_emitter(
- PosTrackingReader {
- reader: html.into_reader(),
- position: 0,
- },
- DefaultEmitter::<PosTrackingReader<StringReader>, Range<usize>>::default(),
+ PosTrackingReader::new(html),
+ DefaultEmitter::<_, Range<usize>>::default(),
)
.flatten()
{