Skip to content

Commit

Permalink
Allow tokens to be consumed during serialization
Browse files Browse the repository at this point in the history
  • Loading branch information
kornelski committed Nov 8, 2024
1 parent a161bb3 commit 529921b
Show file tree
Hide file tree
Showing 5 changed files with 18 additions and 18 deletions.
10 changes: 5 additions & 5 deletions src/rewritable_units/tokens/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ impl<'i> Attribute<'i> {
}
}

impl Serialize for Attribute<'_> {
impl Serialize for &Attribute<'_> {
#[inline]
fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8])) {
fn into_bytes(self, output_handler: &mut dyn FnMut(&[u8])) {
match self.raw.as_ref() {
Some(raw) => output_handler(raw),
None => {
Expand Down Expand Up @@ -254,14 +254,14 @@ impl<'i> Deref for Attributes<'i> {
}
}

impl Serialize for Attributes<'_> {
impl Serialize for &Attributes<'_> {
#[inline]
fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8])) {
fn into_bytes(self, output_handler: &mut dyn FnMut(&[u8])) {
if !self.is_empty() {
let last = self.len() - 1;

for (idx, attr) in self.iter().enumerate() {
attr.to_bytes(output_handler);
attr.into_bytes(output_handler);

if idx != last {
output_handler(b" ");
Expand Down
4 changes: 2 additions & 2 deletions src/rewritable_units/tokens/doctype.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ impl<'i> Doctype<'i> {

impl_user_data!(Doctype<'_>);

impl Serialize for Doctype<'_> {
impl Serialize for &Doctype<'_> {
#[inline]
fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8])) {
fn into_bytes(self, output_handler: &mut dyn FnMut(&[u8])) {
if !self.removed() {
output_handler(&self.raw);
}
Expand Down
16 changes: 8 additions & 8 deletions src/rewritable_units/tokens/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ pub use self::capturer::*;

// Pub only for integration tests
pub trait Serialize {
fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8]));
fn into_bytes(self, output_handler: &mut dyn FnMut(&[u8]));
}

macro_rules! impl_serialize {
($Token:ident) => {
impl crate::rewritable_units::Serialize for $Token<'_> {
#[inline]
fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8])) {
fn into_bytes(self, output_handler: &mut dyn FnMut(&[u8])) {
let Mutations {
content_before,
replacement,
Expand Down Expand Up @@ -70,13 +70,13 @@ pub enum Token<'i> {

impl Serialize for Token<'_> {
#[inline]
fn to_bytes(&self, output_handler: &mut dyn FnMut(&[u8])) {
fn into_bytes(self, output_handler: &mut dyn FnMut(&[u8])) {
match self {
Token::TextChunk(t) => t.to_bytes(output_handler),
Token::Comment(t) => t.to_bytes(output_handler),
Token::StartTag(t) => t.to_bytes(output_handler),
Token::EndTag(t) => t.to_bytes(output_handler),
Token::Doctype(t) => t.to_bytes(output_handler),
Token::TextChunk(t) => t.into_bytes(output_handler),
Token::Comment(t) => t.into_bytes(output_handler),
Token::StartTag(t) => t.into_bytes(output_handler),
Token::EndTag(t) => t.into_bytes(output_handler),
Token::Doctype(t) => t.into_bytes(output_handler),
}
}
}
2 changes: 1 addition & 1 deletion src/rewritable_units/tokens/start_tag.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<'i> StartTag<'i> {
if !self.attributes.is_empty() {
output_handler(b" ");

self.attributes.to_bytes(output_handler);
self.attributes.into_bytes(output_handler);

// NOTE: attributes can be modified the way that
// last attribute has an unquoted value. We always
Expand Down
4 changes: 2 additions & 2 deletions src/transform_stream/dispatcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ where
transform_controller.handle_token(&mut token)?;

if emission_enabled {
token.to_bytes(&mut |c| output_sink.handle_chunk(c));
token.into_bytes(&mut |c| output_sink.handle_chunk(c));
}
}
}
Expand Down Expand Up @@ -278,7 +278,7 @@ where
transform_controller.handle_token(&mut token)?;

if emission_enabled {
token.to_bytes(&mut |c| output_sink.handle_chunk(c));
token.into_bytes(&mut |c| output_sink.handle_chunk(c));
}
}

Expand Down

0 comments on commit 529921b

Please sign in to comment.