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).
Sourcepub fn begin_control_flow_with_open(
&mut self,
format: &str,
args: impl IntoArgs,
custom_open: &str,
) -> &mut Self
pub fn begin_control_flow_with_open( &mut self, format: &str, args: impl IntoArgs, custom_open: &str, ) -> &mut Self
Begin a control flow block with a custom block-open string.
Like begin_control_flow, but uses
custom_open instead of the language’s block_open(). Pass ""
to suppress the block opener entirely (e.g., OCaml match x with).
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 “}” or nothing for Python, and decreases indent).
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 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.