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

Source

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

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 “}” or nothing for Python, and decreases indent).

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_code(&mut self, block: CodeBlock) -> &mut Self

Add a nested CodeBlock 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.