Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Streaming handlers #229

Merged
merged 15 commits into from
Nov 29, 2024
Prev Previous commit
Next Next commit
Avoid unnecessary From when constructing StringChunks
kornelski committed Nov 27, 2024
commit 9cac48c43a2e6e29f6882cfebe583286137f0a6e
24 changes: 12 additions & 12 deletions src/rewritable_units/element.rs
Original file line number Diff line number Diff line change
@@ -241,7 +241,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
.mutations
.mutate()
.content_before
.push_back((content, content_type).into());
.push_back(StringChunk::from_str(content, content_type));
}

/// Inserts content from a [`StreamingHandler`] before the element.
@@ -254,7 +254,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
.mutations
.mutate()
.content_before
.push_back(string_writer.into());
.push_back(StringChunk::Stream(string_writer));
}

/// Inserts `content` after the element.
@@ -287,7 +287,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
/// ```
#[inline]
pub fn after(&mut self, content: &str, content_type: ContentType) {
self.after_chunk((content, content_type).into());
self.after_chunk(StringChunk::from_str(content, content_type));
}

fn after_chunk(&mut self, chunk: StringChunk) {
@@ -306,7 +306,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
///
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
pub fn streaming_after(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.after_chunk(string_writer.into());
self.after_chunk(StringChunk::Stream(string_writer));
}

/// Prepends `content` to the element's inner content, i.e. inserts content right after
@@ -346,7 +346,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
/// ```
#[inline]
pub fn prepend(&mut self, content: &str, content_type: ContentType) {
self.prepend_chunk((content, content_type).into());
self.prepend_chunk(StringChunk::from_str(content, content_type));
}

fn prepend_chunk(&mut self, chunk: StringChunk) {
@@ -370,7 +370,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
///
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
pub fn streaming_prepend(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.prepend_chunk(string_writer.into());
self.prepend_chunk(StringChunk::Stream(string_writer));
}

/// Appends `content` to the element's inner content, i.e. inserts content right before
@@ -410,7 +410,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
/// ```
#[inline]
pub fn append(&mut self, content: &str, content_type: ContentType) {
self.append_chunk((content, content_type).into());
self.append_chunk(StringChunk::from_str(content, content_type));
}

fn append_chunk(&mut self, chunk: StringChunk) {
@@ -429,7 +429,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
///
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
pub fn streaming_append(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.append_chunk(string_writer.into());
self.append_chunk(StringChunk::Stream(string_writer));
}

/// Replaces inner content of the element with `content`.
@@ -468,7 +468,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
/// ```
#[inline]
pub fn set_inner_content(&mut self, content: &str, content_type: ContentType) {
self.set_inner_content_chunk((content, content_type).into());
self.set_inner_content_chunk(StringChunk::from_str(content, content_type));
}

fn set_inner_content_chunk(&mut self, chunk: StringChunk) {
@@ -492,7 +492,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
///
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
pub fn streaming_set_inner_content(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.set_inner_content_chunk(string_writer.into());
self.set_inner_content_chunk(StringChunk::Stream(string_writer));
}

/// Replaces the element and its inner content with `content`.
@@ -524,7 +524,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
/// ```
#[inline]
pub fn replace(&mut self, content: &str, content_type: ContentType) {
self.replace_chunk((content, content_type).into());
self.replace_chunk(StringChunk::from_str(content, content_type));
}

fn replace_chunk(&mut self, chunk: StringChunk) {
@@ -543,7 +543,7 @@ impl<'r, 't, H: HandlerTypes> Element<'r, 't, H> {
///
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
pub fn streaming_replace(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.replace_chunk(string_writer.into());
self.replace_chunk(StringChunk::Stream(string_writer));
}

/// Removes the element and its inner content.
20 changes: 6 additions & 14 deletions src/rewritable_units/mutations.rs
Original file line number Diff line number Diff line change
@@ -84,18 +84,17 @@ impl Mutations {
}
}

impl From<(&str, ContentType)> for StringChunk {
#[inline]
fn from((content, content_type): (&str, ContentType)) -> Self {
Self::Buffer(Box::from(content), content_type)
}
}

pub(crate) enum StringChunk {
Buffer(Box<str>, ContentType),
Stream(Box<dyn StreamingHandler>),
}

impl StringChunk {
pub(crate) fn from_str(content: impl Into<Box<str>>, content_type: ContentType) -> Self {
Self::Buffer(content.into(), content_type)
}
}

#[derive(Default)]
pub(crate) struct DynamicString {
chunks: Vec<StringChunk>,
@@ -172,10 +171,3 @@ where
(self)(sink)
}
}

impl From<Box<dyn StreamingHandler>> for StringChunk {
#[inline]
fn from(writer: Box<dyn StreamingHandler>) -> Self {
Self::Stream(writer)
}
}
21 changes: 12 additions & 9 deletions src/rewritable_units/tokens/comment.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ use super::{Mutations, Token};
use crate::base::Bytes;
use crate::errors::RewritingError;
use crate::html_content::StreamingHandler;
use crate::rewritable_units::StringChunk;
use encoding_rs::Encoding;
use std::any::Any;
use std::fmt::{self, Debug};
@@ -110,7 +111,7 @@ impl<'i> Comment<'i> {
self.mutations
.mutate()
.content_before
.push_back((content, content_type).into());
.push_back(StringChunk::from_str(content, content_type));
}

/// Inserts content from a [`StreamingHandler`] before the comment.
@@ -119,11 +120,11 @@ impl<'i> Comment<'i> {
///
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
#[inline]
pub fn streaming_before(&mut self, handler: Box<dyn StreamingHandler>) {
pub fn streaming_before(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.mutations
.mutate()
.content_before
.push_back(handler.into());
.push_back(StringChunk::Stream(string_writer));
}

/// Inserts `content` after the comment.
@@ -158,7 +159,7 @@ impl<'i> Comment<'i> {
self.mutations
.mutate()
.content_after
.push_front((content, content_type).into());
.push_front(StringChunk::from_str(content, content_type));
}

/// Inserts content from a [`StreamingHandler`] after the comment.
@@ -167,11 +168,11 @@ impl<'i> Comment<'i> {
///
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
#[inline]
pub fn streaming_after(&mut self, handler: Box<dyn StreamingHandler>) {
pub fn streaming_after(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.mutations
.mutate()
.content_after
.push_front(handler.into());
.push_front(StringChunk::Stream(string_writer));
}

/// Replaces the comment with the `content`.
@@ -205,7 +206,7 @@ impl<'i> Comment<'i> {
pub fn replace(&mut self, content: &str, content_type: crate::rewritable_units::ContentType) {
self.mutations
.mutate()
.replace((content, content_type).into());
.replace(StringChunk::from_str(content, content_type));
}

/// Replaces the comment with the content from a [`StreamingHandler`].
@@ -214,8 +215,10 @@ impl<'i> Comment<'i> {
///
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
#[inline]
pub fn streaming_replace(&mut self, handler: Box<dyn StreamingHandler>) {
self.mutations.mutate().replace(handler.into());
pub fn streaming_replace(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.mutations
.mutate()
.replace(StringChunk::Stream(string_writer));
}

/// Removes the comment.
15 changes: 9 additions & 6 deletions src/rewritable_units/tokens/end_tag.rs
Original file line number Diff line number Diff line change
@@ -2,6 +2,7 @@ use super::{Mutations, Token};
use crate::base::Bytes;
use crate::errors::RewritingError;
use crate::html_content::{ContentType, StreamingHandler};
use crate::rewritable_units::StringChunk;
use encoding_rs::Encoding;
use std::fmt::{self, Debug};

@@ -72,7 +73,7 @@ impl<'i> EndTag<'i> {
self.mutations
.mutate()
.content_before
.push_back((content, content_type).into());
.push_back(StringChunk::from_str(content, content_type));
}

/// Inserts `content` after the end tag.
@@ -83,7 +84,7 @@ impl<'i> EndTag<'i> {
self.mutations
.mutate()
.content_after
.push_front((content, content_type).into());
.push_front(StringChunk::from_str(content, content_type));
}

/// Replaces the end tag with `content`.
@@ -93,7 +94,7 @@ impl<'i> EndTag<'i> {
pub fn replace(&mut self, content: &str, content_type: ContentType) {
self.mutations
.mutate()
.replace((content, content_type).into());
.replace(StringChunk::from_str(content, content_type));
}

/// Inserts content from a [`StreamingHandler`] before the end tag.
@@ -106,7 +107,7 @@ impl<'i> EndTag<'i> {
self.mutations
.mutate()
.content_before
.push_back(string_writer.into());
.push_back(StringChunk::Stream(string_writer));
}

/// Inserts content from a [`StreamingHandler`] after the end tag.
@@ -119,7 +120,7 @@ impl<'i> EndTag<'i> {
self.mutations
.mutate()
.content_after
.push_front(string_writer.into());
.push_front(StringChunk::Stream(string_writer));
}

/// Replaces the end tag with content from a [`StreamingHandler`].
@@ -129,7 +130,9 @@ impl<'i> EndTag<'i> {
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
#[inline]
pub fn streaming_replace(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.mutations.mutate().replace(string_writer.into());
self.mutations
.mutate()
.replace(StringChunk::Stream(string_writer));
}

/// Removes the end tag.
15 changes: 9 additions & 6 deletions src/rewritable_units/tokens/start_tag.rs
Original file line number Diff line number Diff line change
@@ -4,6 +4,7 @@ use crate::base::Bytes;
use crate::errors::RewritingError;
use crate::html::Namespace;
use crate::html_content::{ContentType, StreamingHandler};
use crate::rewritable_units::StringChunk;
use encoding_rs::Encoding;
use std::fmt::{self, Debug};

@@ -115,7 +116,7 @@ impl<'i> StartTag<'i> {
self.mutations
.mutate()
.content_before
.push_back((content, content_type).into());
.push_back(StringChunk::from_str(content, content_type));
}

/// Inserts `content` after the start tag.
@@ -126,7 +127,7 @@ impl<'i> StartTag<'i> {
self.mutations
.mutate()
.content_after
.push_front((content, content_type).into());
.push_front(StringChunk::from_str(content, content_type));
}

/// Replaces the start tag with `content`.
@@ -136,7 +137,7 @@ impl<'i> StartTag<'i> {
pub fn replace(&mut self, content: &str, content_type: ContentType) {
self.mutations
.mutate()
.replace((content, content_type).into());
.replace(StringChunk::from_str(content, content_type));
}

/// Inserts content from a [`StreamingHandler`] before the start tag.
@@ -148,7 +149,7 @@ impl<'i> StartTag<'i> {
self.mutations
.mutate()
.content_before
.push_back(string_writer.into());
.push_back(StringChunk::Stream(string_writer));
}

/// Inserts content from a [`StreamingHandler`] after the start tag.
@@ -160,7 +161,7 @@ impl<'i> StartTag<'i> {
self.mutations
.mutate()
.content_after
.push_front(string_writer.into());
.push_front(StringChunk::Stream(string_writer));
}

/// Replaces the start tag with the content from a [`StreamingHandler`].
@@ -169,7 +170,9 @@ impl<'i> StartTag<'i> {
///
/// Use the [`streaming!`] macro to make a `StreamingHandler` from a closure.
pub fn streaming_replace(&mut self, string_writer: Box<dyn StreamingHandler>) {
self.mutations.mutate().replace(string_writer.into());
self.mutations
.mutate()
.replace(StringChunk::Stream(string_writer));
}

/// Removes the start tag.
Loading