aboutsummaryrefslogtreecommitdiff
path: root/src/emitter.rs
diff options
context:
space:
mode:
authorMartin Fischer <martin@push-f.com>2023-08-17 08:04:21 +0200
committerMartin Fischer <martin@push-f.com>2023-08-19 11:41:22 +0200
commit91074b6e7e6e8463f15ca26bc39e70b80f954227 (patch)
tree7db7159df67faed9c5a7954a07a064cadcb0497e /src/emitter.rs
parent0f2d667eb08762b744ef5a18d6c09f99c9c1b8bb (diff)
refactor!: make Position generic over offset type
Previously Span was generic over R just so that it could provide the method: fn from_reader(reader: &R) -> Self; and properly implementing that method again relied on R implementing the Position trait: impl<P: Position> Span<P> for Range<usize> { .. } which was a very roundabout and awkward way of doing things. It makes much more sense to make the Position trait generic over the return type of its method (which previously always had to be usize). Which lets us provide a blanket implementation: impl<R: Reader> Position<NoopOffset> for R { .. }
Diffstat (limited to 'src/emitter.rs')
-rw-r--r--src/emitter.rs19
1 files changed, 11 insertions, 8 deletions
diff --git a/src/emitter.rs b/src/emitter.rs
index e441eb7..18b2539 100644
--- a/src/emitter.rs
+++ b/src/emitter.rs
@@ -5,6 +5,7 @@ use std::collections::VecDeque;
use std::marker::PhantomData;
use std::mem;
+use crate::spans::Position;
use crate::spans::Span;
use crate::Error;
@@ -183,7 +184,7 @@ impl<R, S> Default for DefaultEmitter<R, S> {
}
}
-impl<R, S: Span<R>> DefaultEmitter<R, S> {
+impl<R, S: Span> DefaultEmitter<R, S> {
fn emit_token(&mut self, token: Token<S>) {
self.flush_current_characters();
self.emitted_tokens.push_front(token);
@@ -229,7 +230,7 @@ impl<R, S: Span<R>> DefaultEmitter<R, S> {
}
}
-impl<R, S: Span<R>> Emitter<R> for DefaultEmitter<R, S> {
+impl<R: Position<S::Offset>, S: Span> Emitter<R> for DefaultEmitter<R, S> {
type Token = Token<S>;
fn emit_eof(&mut self) {
@@ -237,7 +238,7 @@ impl<R, S: Span<R>> Emitter<R> for DefaultEmitter<R, S> {
}
fn emit_error(&mut self, error: Error, reader: &R) {
- self.push_error(error, S::from_reader(reader));
+ self.push_error(error, S::new(reader.position(), reader.position()));
}
fn pop_token(&mut self) -> Option<Self::Token> {
@@ -250,7 +251,7 @@ impl<R, S: Span<R>> Emitter<R> for DefaultEmitter<R, S> {
fn init_start_tag(&mut self, reader: &R) {
self.current_token = Some(Token::StartTag(StartTag {
- name_span: S::from_reader(reader),
+ name_span: S::new(reader.position(), reader.position()),
self_closing: false,
name: String::new(),
attributes: Default::default(),
@@ -258,7 +259,7 @@ impl<R, S: Span<R>> Emitter<R> for DefaultEmitter<R, S> {
}
fn init_end_tag(&mut self, reader: &R) {
self.current_token = Some(Token::EndTag(EndTag {
- name_span: S::from_reader(reader),
+ name_span: S::new(reader.position(), reader.position()),
name: String::new(),
}));
self.seen_attributes.clear();
@@ -367,15 +368,17 @@ impl<R, S: Span<R>> Emitter<R> for DefaultEmitter<R, S> {
self.current_attribute = Some((
String::new(),
Attribute {
- name_span: S::from_reader(reader),
+ name_span: S::new(reader.position(), reader.position()),
value: String::new(),
value_span: S::default(),
},
));
}
fn init_attribute_value(&mut self, reader: &R, quoted: bool) {
- self.current_attribute.as_mut().unwrap().1.value_span =
- S::from_reader_with_offset(reader, quoted as usize);
+ self.current_attribute.as_mut().unwrap().1.value_span = S::new(
+ reader.position() + quoted as usize,
+ reader.position() + quoted as usize,
+ );
}
fn push_attribute_name(&mut self, s: &str) {