Buffer

Struct Buffer 

Source
pub struct Buffer<W, C> { /* private fields */ }
Expand description

Wrapper arround a writer element.

Implementations§

Source§

impl<W> Buffer<FmtWriter<W>, Body<'_>>

Source

pub fn into_inner(self) -> W

Source§

impl<W> Buffer<IoWriter<W>, Body<'_>>

Source

pub fn into_inner(self) -> W

Source§

impl Buffer<FmtWriter<String>, Body<'_>>

Source

pub fn inner(&self) -> &str

Source§

impl<W: WriterExt> Buffer<W, Body<'_>>

Source

pub fn doctype(self) -> Self

Appends the html doctype to the buffer

Source

pub fn try_doctype(self) -> Result<Self, W::Error>

Tries to append the html doctype to the buffer

Source§

impl<'a, W: WriterExt> Buffer<W, Body<'a>>

Source

pub fn cond<F>(self, condition: bool, children: F) -> Buffer<W, Body<'a>>
where F: FnOnce(Buffer<W, Body<'_>>) -> Buffer<W, Body<'_>>,

Conditionally apply some children to an element

let is_error = true;
let html = another_html_builder::Buffer::default()
    .cond(is_error, |buf| {
        buf.node("p").content(|buf| buf.text("ERROR!"))
    })
    .into_inner();
assert_eq!(html, "<p>ERROR!</p>");
Source

pub fn try_cond<F>( self, condition: bool, children: F, ) -> Result<Buffer<W, Body<'a>>, W::Error>
where F: FnOnce(Buffer<W, Body<'_>>) -> Result<Buffer<W, Body<'_>>, W::Error>,

Source

pub fn optional<V, F>( self, value: Option<V>, children: F, ) -> Buffer<W, Body<'a>>
where F: FnOnce(Buffer<W, Body<'_>>, V) -> Buffer<W, Body<'_>>,

Conditionally apply some children to an element depending on an optional

let value: Option<u8> = Some(42);
let html = another_html_builder::Buffer::default()
    .optional(value, |buf, answer| {
        buf.node("p")
            .content(|buf| buf.text("Answer: ").raw(answer))
    })
    .into_inner();
assert_eq!(html, "<p>Answer: 42</p>");
Source

pub fn try_optional<V, F>( self, value: Option<V>, children: F, ) -> Result<Buffer<W, Body<'a>>, W::Error>
where F: FnOnce(Buffer<W, Body<'_>>, V) -> Result<Buffer<W, Body<'_>>, W::Error>,

Source

pub fn node(self, tag: &'a str) -> Buffer<W, Element<'a>>

Starts a new node in the buffer

After calling this function, the buffer will only allow to add attributes, close the current node or add content to the node.

let html = another_html_builder::Buffer::default()
    .node("p")
    .attr(("foo", "bar"))
    .close()
    .into_inner();
assert_eq!(html, "<p foo=\"bar\" />");
let html = another_html_builder::Buffer::default()
    .node("p")
    .content(|buf| buf.text("hello"))
    .into_inner();
assert_eq!(html, "<p>hello</p>");
Source

pub fn try_node(self, tag: &'a str) -> Result<Buffer<W, Element<'a>>, W::Error>

Source

pub fn raw<V: Display>(self, value: V) -> Self

Appends some raw content implementing Display

This will not escape the provided value.

Source

pub fn try_raw<V: Display>(self, value: V) -> Result<Self, W::Error>

Source

pub fn text(self, input: &str) -> Self

Appends some text and escape it.

let html = another_html_builder::Buffer::default()
    .node("p")
    .content(|b| b.text("asd\"weiofew!/<>"))
    .into_inner();
assert_eq!(html, "<p>asd&quot;weiofew!&#x2F;&lt;&gt;</p>");
Source

pub fn try_text(self, input: &str) -> Result<Self, W::Error>

Source§

impl<'a, W: WriterExt> Buffer<W, Element<'a>>

Source

pub fn attr<T>(self, attr: T) -> Self
where Attribute<T>: Display,

Appends an attribute to the current node.

For more information about how to extend attributes, take a look at the crate::attribute::Attribute trait.

let html = another_html_builder::Buffer::default()
    .node("p")
    .attr("single")
    .attr(("hello", "world"))
    .attr(("number", 42))
    .attr(Some(("foo", "bar")))
    .attr(None::<(&str, &str)>)
    .attr(Some("here"))
    .attr(None::<&str>)
    .close()
    .into_inner();
assert_eq!(
    html,
    "<p single hello=\"world\" number=\"42\" foo=\"bar\" here />"
);
Source

pub fn try_attr<T>(self, attr: T) -> Result<Self, W::Error>
where Attribute<T>: Display,

Source

pub fn cond_attr<T>(self, condition: bool, attr: T) -> Self
where Attribute<T>: Display,

Conditionally appends some attributes

let html = another_html_builder::Buffer::default()
    .node("p")
    .cond_attr(true, ("foo", "bar"))
    .cond_attr(false, ("foo", "baz"))
    .cond_attr(true, "here")
    .cond_attr(false, "not-here")
    .close()
    .into_inner();
assert_eq!(html, "<p foo=\"bar\" here />");
Source

pub fn try_cond_attr<T>( self, condition: bool, attr: T, ) -> Result<Self, W::Error>
where Attribute<T>: Display,

Source

pub fn close(self) -> Buffer<W, Body<'a>>

Closes the current node without providing any content

let html = another_html_builder::Buffer::default()
    .node("p")
    .close()
    .into_inner();
assert_eq!(html, "<p />");
Source

pub fn try_close(self) -> Result<Buffer<W, Body<'a>>, W::Error>

Source

pub fn content<F>(self, children: F) -> Buffer<W, Body<'a>>
where F: FnOnce(Buffer<W, Body<'_>>) -> Buffer<W, Body<'_>>,

Closes the current node and start writing it’s content

When returning the inner callback, the closing element will be written to the buffer

let html = another_html_builder::Buffer::default()
    .node("div")
    .content(|buf| buf.node("p").close())
    .into_inner();
assert_eq!(html, "<div><p /></div>");
Source

pub fn try_content<F>( self, children: F, ) -> Result<Buffer<W, Body<'a>>, W::Error>
where F: FnOnce(Buffer<W, Body<'_>>) -> Result<Buffer<W, Body<'_>>, W::Error>,

Trait Implementations§

Source§

impl<W: Clone, C: Clone> Clone for Buffer<W, C>

Source§

fn clone(&self) -> Buffer<W, C>

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl<W: Debug, C: Debug> Debug for Buffer<W, C>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for Buffer<FmtWriter<String>, Body<'static>>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<W: Write> From<W> for Buffer<FmtWriter<W>, Body<'static>>

Source§

fn from(buffer: W) -> Self

Converts to this type from the input type.
Source§

impl<W: Write> From<W> for Buffer<IoWriter<W>, Body<'static>>

Source§

fn from(value: W) -> Self

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<W, C> Freeze for Buffer<W, C>
where W: Freeze, C: Freeze,

§

impl<W, C> RefUnwindSafe for Buffer<W, C>

§

impl<W, C> Send for Buffer<W, C>
where W: Send, C: Send,

§

impl<W, C> Sync for Buffer<W, C>
where W: Sync, C: Sync,

§

impl<W, C> Unpin for Buffer<W, C>
where W: Unpin, C: Unpin,

§

impl<W, C> UnwindSafe for Buffer<W, C>
where W: UnwindSafe, C: UnwindSafe,

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> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. 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> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
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.