pub struct Buffer<W, C> { /* private fields */ }
Expand description
Wrapper arround a writer element.
Implementations§
Source§impl<'a, W: Write> Buffer<W, Body<'a>>
impl<'a, W: Write> Buffer<W, Body<'a>>
Sourcepub fn cond<F>(self, condition: bool, children: F) -> Buffer<W, Body<'a>>
pub fn cond<F>(self, condition: bool, children: F) -> Buffer<W, Body<'a>>
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>");
pub fn try_cond<F>( self, condition: bool, children: F, ) -> Result<Buffer<W, Body<'a>>, Error>
Sourcepub fn optional<V, F>(
self,
value: Option<V>,
children: F,
) -> Buffer<W, Body<'a>>
pub fn optional<V, F>( self, value: Option<V>, children: F, ) -> Buffer<W, Body<'a>>
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>");
pub fn try_optional<V, F>( self, value: Option<V>, children: F, ) -> Result<Buffer<W, Body<'a>>, Error>
Sourcepub fn node(self, tag: &'a str) -> Buffer<W, Element<'a>>
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>");
pub fn try_node(self, tag: &'a str) -> Result<Buffer<W, Element<'a>>, Error>
Sourcepub fn raw<V: Display>(self, value: V) -> Self
pub fn raw<V: Display>(self, value: V) -> Self
Appends some raw content implementing Display
This will not escape the provided value.
pub fn try_raw<V: Display>(self, value: V) -> Result<Self, Error>
Sourcepub fn text(self, content: &str) -> Self
pub fn text(self, content: &str) -> Self
Appends some text and escape it.
let html = another_html_builder::Buffer::new()
.node("p")
.content(|b| b.text("asd\"weiofew!/<>"))
.into_inner();
assert_eq!(html, "<p>asd"weiofew!/<></p>");
pub fn try_text(self, content: &str) -> Result<Self, Error>
Source§impl<'a, W: Write> Buffer<W, Element<'a>>
impl<'a, W: Write> Buffer<W, Element<'a>>
Sourcepub fn attr<T>(self, attr: T) -> Self
pub fn attr<T>(self, attr: T) -> Self
Appends an attribute to the current node.
For more information about how to extend attributes, take a look at the Attribute trait.
let html = another_html_builder::Buffer::new()
.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 />"
);
pub fn try_attr<T>(self, attr: T) -> Result<Self, Error>
Sourcepub fn cond_attr<T>(self, condition: bool, attr: T) -> Self
pub fn cond_attr<T>(self, condition: bool, attr: T) -> Self
Conditionally appends some attributes
let html = another_html_builder::Buffer::new()
.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 />");
pub fn try_cond_attr<T>(self, condition: bool, attr: T) -> Result<Self, Error>
Sourcepub fn close(self) -> Buffer<W, Body<'a>>
pub fn close(self) -> Buffer<W, Body<'a>>
Closes the current node without providing any content
let html = another_html_builder::Buffer::new()
.node("p")
.close()
.into_inner();
assert_eq!(html, "<p />");
pub fn try_close(self) -> Result<Buffer<W, Body<'a>>, Error>
Sourcepub fn content<F>(self, children: F) -> Buffer<W, Body<'a>>
pub fn content<F>(self, children: F) -> Buffer<W, Body<'a>>
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::new()
.node("div")
.content(|buf| buf.node("p").close())
.into_inner();
assert_eq!(html, "<div><p /></div>");