Struct Builder

Source
pub struct Builder { /* private fields */ }
Expand description

Constructor for Doc objects.

Implementations§

Source§

impl Builder

Source

pub fn new() -> Self

Create a new Doc builder context.

Source

pub fn scope<L: ToString, K: ToString>( &mut self, begin_line: L, term_line: Option<K>, ) -> &mut Self

Begin a scope, pushing an optional scope terminator to the internal scope stack.

If the scope generated using a terminator line, that line will appended to the document when the scope is closed using the exit() method.

Source

pub fn autoscope<F, L: ToString, K: ToString>( &mut self, begin_line: L, term_line: Option<K>, f: F, ) -> &mut Self
where F: FnOnce(&mut Self),

Wrap Builder::scope() and Builder::exit().

Initialize a new scope, call caller-supplied closure, and automatically exit scope before returning.

use std::sync::Arc;
use sidoc::{Builder, RenderContext};

let mut bldr = Builder::new();

bldr
  .line("<!DOCTYPE html>")
  .autoscope("<html>", Some("</html>"), |bldr| {
    bldr.autoscope("<head>", Some("</head>"), |bldr| {
      bldr.line("<title>hello</title>");
    });
  });

let doc = bldr.build().unwrap();
let mut r = RenderContext::new();
r.doc("root", Arc::new(doc));
let buf = r.render("root").unwrap();

assert_eq!(
  buf,
  "<!DOCTYPE html>\n<html>\n  <head>\n    <title>hello</title>\n  </head>\n</html>\n"
);
Source

pub fn autoscope_if<F, L: ToString, K: ToString>( &mut self, pred: bool, begin_line: L, term_line: Option<K>, f: F, ) -> &mut Self
where F: FnOnce(&mut Self),

Same as Builder::autoscope(), but only init scope and call closure if predicate is true.

Source

pub fn autoscope_opt<F, T, L: ToString, K: ToString>( &mut self, opt: Option<T>, begin_line: L, term_line: Option<K>, f: F, ) -> &mut Self
where F: FnOnce(&mut Self, T),

Same as Builder::autoscope(), but only init scope and call closure if opt is Some(T). T will be passed to the closure.

Source

pub fn exit(&mut self) -> &mut Self

Leave a previously entered scope.

If the scope() call that created the current scope

§Panics

The scope stack must not be empty.

Source

pub fn exit_line<L: ToString>(&mut self, line: L) -> &mut Self

Leave previously entered scope, adding a line passed by the caller rather than the scope stack.

§Panics

The scope stack must not be empty.

Source

pub fn line<L: ToString>(&mut self, line: L) -> &mut Self

Add a new line at current scope.

Source

pub fn optref<N: ToString>(&mut self, name: N) -> &mut Self

Add a named optional reference.

References are placeholders for other documents. An optional reference means that this reference does not need to be resolved by the renderer.

Source

pub fn reqref<N: ToString>(&mut self, name: N) -> &mut Self

Add a named required reference.

References are placeholders for other documents. A required reference must be resolved by the renderer or it will return an error to its caller.

Source

pub fn build(self) -> Result<Doc, Error>

Generate a Doc object from this document.

The document must be properly nested before calling this function, meaning all scopes it opened must be closed.

§Errors

Error::BadNesting means one or more scopes are still open.

Trait Implementations§

Source§

impl Default for Builder

Source§

fn default() -> Builder

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

Auto Trait Implementations§

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.