pub struct Builder { /* private fields */ }
Expand description
Constructor for Doc
objects.
Implementations§
Source§impl Builder
impl Builder
Sourcepub fn scope<L: ToString, K: ToString>(
&mut self,
begin_line: L,
term_line: Option<K>,
) -> &mut Self
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.
Sourcepub fn autoscope<F, L: ToString, K: ToString>(
&mut self,
begin_line: L,
term_line: Option<K>,
f: F,
) -> &mut Selfwhere
F: FnOnce(&mut Self),
pub fn autoscope<F, L: ToString, K: ToString>(
&mut self,
begin_line: L,
term_line: Option<K>,
f: F,
) -> &mut Selfwhere
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"
);
Sourcepub fn autoscope_if<F, L: ToString, K: ToString>(
&mut self,
pred: bool,
begin_line: L,
term_line: Option<K>,
f: F,
) -> &mut Selfwhere
F: FnOnce(&mut Self),
pub fn autoscope_if<F, L: ToString, K: ToString>(
&mut self,
pred: bool,
begin_line: L,
term_line: Option<K>,
f: F,
) -> &mut Selfwhere
F: FnOnce(&mut Self),
Same as Builder::autoscope()
, but only init scope and call closure if
predicate is true.
Sourcepub fn autoscope_opt<F, T, L: ToString, K: ToString>(
&mut self,
opt: Option<T>,
begin_line: L,
term_line: Option<K>,
f: F,
) -> &mut Selfwhere
F: FnOnce(&mut Self, T),
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 Selfwhere
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.
Sourcepub fn exit(&mut self) -> &mut Self
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.
Sourcepub fn exit_line<L: ToString>(&mut self, line: L) -> &mut Self
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.
Sourcepub fn optref<N: ToString>(&mut self, name: N) -> &mut Self
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.