Skip to main content

CodeBlockBuilder

Struct CodeBlockBuilder 

Source
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

Source

pub fn new() -> Self

Create a new empty code block builder.

Source

pub fn add(&mut self, format: &str, args: impl IntoArgs) -> &mut Self

Add a formatted code fragment.

Source

pub fn add_statement(&mut self, format: &str, args: impl IntoArgs) -> &mut Self

Add a statement (wraps in %[…%] and appends language semicolon).

Source

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.

Source

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).

Source

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

End a control flow block (emits language-specific closer + newline, decreases indent).

Source

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.

Source

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).

Source

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

Add a blank line.

Source

pub fn add_comment(&mut self, text: &str) -> &mut Self

Add an inline comment.

Source

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]]).

Source

pub fn add_code(&mut self, block: CodeBlock) -> &mut Self

Add a nested CodeBlock inline.

Source

pub fn add_fragment(&mut self, fragment: CodeFragment) -> &mut Self

Add a parsed fragment inline.

Source

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).

Source

pub fn build_unwrap(self) -> CodeBlock

Build the CodeBlock, panicking on error.

Trait Implementations§

Source§

impl Debug for CodeBlockBuilder

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Default for CodeBlockBuilder

Source§

fn default() -> Self

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.