Struct rstml_component::HtmlFormatter

source ·
pub struct HtmlFormatter<'a> { /* private fields */ }
Expand description

A formatter for serializing HTML nodes and content.

The HtmlFormatter struct provides a versatile way to serialize HTML nodes and content, ensuring proper spacing, indentation, and formatting. It’s designed to handle various types of HTML content and produce well-structured and readable HTML output.

NOTE: Currently, no indentation/readibility is supported. The plan is to implement that later.

Implementations§

source§

impl<'a> HtmlFormatter<'a>

source

pub fn new(buffer: &'a mut BytesMut) -> Self

Creates a new HtmlFormatter instance with the provided buffer.

§Arguments
  • buffer: A mutable reference to the BytesMut buffer where the formatted content will be written.
§Returns

A new HtmlFormatter instance associated with the provided buffer.

source

pub fn write_bytes(&mut self, raw: &[u8])

Writes raw bytes to the formatter’s buffer without escaping.

This method appends the specified raw bytes to the formatter’s buffer without performing any additional escaping or modification. It provides a low-level, raw API for directly writing content to the buffer, which can be useful for situations where the content is already properly formatted and safe to include as-is.

§Arguments
  • raw: A reference to the raw byte slice that will be written to the buffer.
source

pub fn write(&mut self, value: &[u8])

Writes escaped bytes to the formatter’s buffer, ensuring valid HTML characters.

This method accepts a reference to a byte slice containing the content to be written to the formatter’s buffer. The provided value is escaped to ensure that it only contains valid HTML characters, preventing any unintentional issues with the produced HTML content.

§Arguments
  • value: A reference to the raw byte slice containing the content to be escaped and written.
source

pub fn write_doctype(&mut self, value: &[u8])

This method appends a DOCTYPE declaration to the formatter’s buffer. The provided value is escaped to ensure that it only contains valid characters for a DOCTYPE declaration. The resulting DOCTYPE declaration is properly formatted and follows the standard syntax of “”.

§Arguments
  • value: A reference to the raw byte slice containing the content for the DOCTYPE declaration.
source

pub fn write_open_tag_start(&mut self, tag: &[u8])

Writes the start of an opening HTML tag to the formatter’s buffer.

This method appends the start of an opening HTML tag to the formatter’s buffer. The provided tag is used as the tag name, and the tag is not closed. is commonly followed by either write_attribute_name, write_self_close_tag, or write_open_tag_end.

§Arguments
  • tag: A reference to the raw byte slice containing the tag name for the opening tag.
source

pub fn write_attribute_name(&mut self, name: &[u8])

Writes an HTML attribute name to the formatter’s buffer.

This method appends an HTML attribute name to the formatter’s buffer. The provided name is used as the attribute name.

§Arguments
  • name: A reference to the raw byte slice containing the attribute name.
source

pub fn write_attribute_value( &mut self, value: impl HtmlAttributeValue, ) -> Result

Writes an HTML attribute value to the formatter’s buffer.

This method appends an HTML attribute value to the formatter’s buffer. The provided value is an instance of a type implementing the HtmlAttributeValue trait. The value is written to the buffer, ensuring proper formatting and escaping if required.

§Arguments
  • value: An instance implementing the HtmlAttributeValue trait, representing the attribute value.
§Returns

A std::fmt::Result indicating the success or failure of the writing operation.

source

pub fn write_attributes(&mut self, values: impl HtmlAttributes) -> Result

source

pub fn write_self_close_tag(&mut self)

Writes a self-closing indicator to the formatter’s buffer.

This method appends a self-closing indicator “ />“ to the formatter’s buffer. It’s commonly used after writing an opening tag to indicate that the tag is self-closing and has no associated content.

source

pub fn write_open_tag_end(&mut self)

Writes the end of an opening HTML tag to the formatter’s buffer.

This method appends the end of an opening HTML tag “>” to the formatter’s buffer. It’s commonly used after writing the tag name and its attributes to indicate the completion of the tag’s opening.

source

pub fn write_end_tag(&mut self, tag: &[u8])

Writes an HTML end tag to the formatter’s buffer.

This method appends an HTML end tag “</tag>” to the formatter’s buffer. The provided tag is used as the tag name for the end tag.

§Arguments
  • tag: A reference to the raw byte slice containing the tag name for the end tag.
source

pub fn write_content(&mut self, content: impl HtmlContent) -> Result

Writes HTML content to the formatter’s buffer.

This method appends HTML content to the formatter’s buffer. The provided content is an instance of a type implementing the HtmlContent trait. The content is formatted and written to the buffer according to the implementation of the HtmlContent trait.

§Arguments
  • content: An instance implementing the HtmlContent trait, representing the HTML content to write.
§Returns

A std::fmt::Result indicating the success or failure of the writing operation.

source

pub fn write_comment(&mut self, comment: &[u8])

Writes an HTML comment to the formatter’s buffer.

This method appends an HTML comment to the formatter’s buffer. The provided comment is escaped to ensure that it only contains valid characters for an HTML comment.

§Arguments
  • comment: A reference to the raw byte slice containing the content for the HTML comment.
source

pub fn reserve(&mut self, additional: usize)

Reserves space in the buffer for writing additional bytes without reallocation.

This method ensures that enough space is reserved in the formatter’s buffer to accommodate the writing of additional bytes without needing to reallocate memory. It’s useful to call this method before writing a significant amount of content, as it can help prevent frequent reallocations and improve performance.

§Arguments
  • additional: The number of additional bytes to reserve space for in the buffer.

Trait Implementations§

source§

impl<'a> AsMut<HtmlFormatter<'a>> for HtmlFormatter<'a>

source§

fn as_mut(&mut self) -> &mut HtmlFormatter<'a>

Converts this type into a mutable reference of the (usually inferred) input type.

Auto Trait Implementations§

§

impl<'a> Freeze for HtmlFormatter<'a>

§

impl<'a> RefUnwindSafe for HtmlFormatter<'a>

§

impl<'a> Send for HtmlFormatter<'a>

§

impl<'a> Sync for HtmlFormatter<'a>

§

impl<'a> Unpin for HtmlFormatter<'a>

§

impl<'a> !UnwindSafe for HtmlFormatter<'a>

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

source§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.