pub struct CodeBlockBuilder { /* private fields */ }Expand description
Builder for constructing CodeBlock instances.
Provides methods for adding formatted code fragments, statements, control
flow blocks, and nested code blocks. Format strings use %T, %N, %S,
%L for type/name/string/literal substitution, and %W, %>, %< for
soft line breaks and indentation.
§Examples
use sigil_stitch::code_block::CodeBlock;
use sigil_stitch::lang::typescript::TypeScript;
let mut cb = CodeBlock::builder();
cb.begin_control_flow("if (x > 0)", ());
cb.add_statement("return x", ());
cb.next_control_flow("else", ());
cb.add_statement("return -x", ());
cb.end_control_flow();
let block = cb.build().unwrap();Implementations§
Source§impl CodeBlockBuilder
impl CodeBlockBuilder
Sourcepub fn add(&mut self, format: &str, args: impl IntoArgs) -> &mut Self
pub fn add(&mut self, format: &str, args: impl IntoArgs) -> &mut Self
Add a formatted code fragment.
Sourcepub fn add_statement(&mut self, format: &str, args: impl IntoArgs) -> &mut Self
pub fn add_statement(&mut self, format: &str, args: impl IntoArgs) -> &mut Self
Add a statement (wraps in %[…%] and appends language semicolon).
Sourcepub fn begin_control_flow(
&mut self,
format: &str,
args: impl IntoArgs,
) -> &mut Self
pub fn begin_control_flow( &mut self, format: &str, args: impl IntoArgs, ) -> &mut Self
Begin a control flow block (e.g., “if foo” -> “if foo {\n” + indent).
The raw format string (not the interpolated result) is stored as
the condition text and passed to block_open_for / block_close_for
at render time, enabling language backends to emit context-aware
delimiters (e.g., Bash then/fi for if, do/done for for).
Because backends pattern-match on the stored condition (e.g.,
condition.starts_with("if ")), avoid interpolating into the keyword
prefix — begin_control_flow("if %L", expr) works, but
begin_control_flow("%L x", some_keyword) would not be recognized.
Sourcepub fn next_control_flow(
&mut self,
format: &str,
args: impl IntoArgs,
) -> &mut Self
pub fn next_control_flow( &mut self, format: &str, args: impl IntoArgs, ) -> &mut Self
Add an else/else-if clause (e.g., “} else {” or “elif …:” for Python).
Sourcepub fn end_control_flow(&mut self) -> &mut Self
pub fn end_control_flow(&mut self) -> &mut Self
End a control flow block (emits language-specific closer + newline, decreases indent).
Sourcepub fn end_control_flow_no_newline(&mut self) -> &mut Self
pub fn end_control_flow_no_newline(&mut self) -> &mut Self
End a control flow block without a trailing newline.
Used when the block is nested inside a Statement::Statement via
%L (e.g., expression braces in format strings). The outer
add_statement provides both ; via StatementEnd and \n via
Newline.
Sourcepub fn end_control_flow_with_semicolon(&mut self) -> &mut Self
pub fn end_control_flow_with_semicolon(&mut self) -> &mut Self
End a control flow block with a trailing semicolon (for expression-level
control flow like match in PHP/Rust).
Sourcepub fn add_comment(&mut self, text: &str) -> &mut Self
pub fn add_comment(&mut self, text: &str) -> &mut Self
Add an inline comment.
Sourcepub fn add_attribute(&mut self, text: &str) -> &mut Self
pub fn add_attribute(&mut self, text: &str) -> &mut Self
Add a language-aware attribute / annotation.
Rendered with the language’s annotation prefix and suffix
(Rust: #[text], Java/Python: @text, C++: [[text]]).
Sourcepub fn add_fragment(&mut self, fragment: CodeFragment) -> &mut Self
pub fn add_fragment(&mut self, fragment: CodeFragment) -> &mut Self
Add a parsed fragment inline.
Sourcepub fn build(self) -> Result<CodeBlock, SigilStitchError>
pub fn build(self) -> Result<CodeBlock, SigilStitchError>
Build the immutable CodeBlock.
Returns an error if any format string had an argument count mismatch, or if indent depth is not balanced (unmatched begin_control_flow / end_control_flow).
Sourcepub fn build_unwrap(self) -> CodeBlock
pub fn build_unwrap(self) -> CodeBlock
Build the CodeBlock, panicking on error.