pub struct CodeWriter<W> { /* private fields */ }Expand description
A code writer that tracks indentation and provides helpers for generating C-like syntax (used by Swift, TypeScript, Go, etc.)
Implementations§
Source§impl<W: Write> CodeWriter<W>
impl<W: Write> CodeWriter<W>
Sourcepub fn new(writer: W, indent_string: String) -> Self
pub fn new(writer: W, indent_string: String) -> Self
Create a new CodeWriter with the given writer and indent string (e.g., “ “ or “\t”)
Sourcepub fn with_indent_spaces(writer: W, spaces: usize) -> Self
pub fn with_indent_spaces(writer: W, spaces: usize) -> Self
Create a new CodeWriter with 4-space indentation
Sourcepub fn write(&mut self, text: &str) -> Result
pub fn write(&mut self, text: &str) -> Result
Write text without a newline. Adds indentation if at line start.
Sourcepub fn writeln(&mut self, text: &str) -> Result
pub fn writeln(&mut self, text: &str) -> Result
Write text followed by a newline. Adds indentation if needed.
Sourcepub fn blank_line(&mut self) -> Result
pub fn blank_line(&mut self) -> Result
Write an empty line
Sourcepub fn indent(&mut self) -> IndentGuard
pub fn indent(&mut self) -> IndentGuard
Create an indentation guard. Indentation increases while the guard is alive.
Sourcepub fn comment(&mut self, comment_prefix: &str, text: &str) -> Result
pub fn comment(&mut self, comment_prefix: &str, text: &str) -> Result
Write a single-line comment (e.g., “// comment”)
Sourcepub fn doc_comment(&mut self, comment_prefix: &str, text: &str) -> Result
pub fn doc_comment(&mut self, comment_prefix: &str, text: &str) -> Result
Write a doc comment block. Each line is prefixed with the comment marker.
Sourcepub fn begin_block(&mut self, header: &str) -> Result<IndentGuard, Error>
pub fn begin_block(&mut self, header: &str) -> Result<IndentGuard, Error>
Begin a block with opening brace: writes “header {” and returns indent guard
Sourcepub fn block<F>(&mut self, header: &str, body: F) -> Result
pub fn block<F>(&mut self, header: &str, body: F) -> Result
Write a complete block with a closure for the body
Sourcepub fn indent_level(&self) -> usize
pub fn indent_level(&self) -> usize
Get the current indentation level
Sourcepub fn into_inner(self) -> W
pub fn into_inner(self) -> W
Consume the writer and return the inner writer
Sourcepub fn write_separated<I, F>(
&mut self,
items: I,
separator: &str,
write_item: F,
) -> Result
pub fn write_separated<I, F>( &mut self, items: I, separator: &str, write_item: F, ) -> Result
Write items separated by a delimiter (e.g., comma-separated list)
Sourcepub fn write_separated_lines<I, F>(
&mut self,
items: I,
separator: &str,
write_item: F,
) -> Result
pub fn write_separated_lines<I, F>( &mut self, items: I, separator: &str, write_item: F, ) -> Result
Write items separated by delimiter with newlines (one item per line)
Sourcepub fn write_parens<F>(&mut self, f: F) -> Result
pub fn write_parens<F>(&mut self, f: F) -> Result
Write a parenthesized list (e.g., “(a, b, c)”)
Sourcepub fn write_brackets<F>(&mut self, f: F) -> Result
pub fn write_brackets<F>(&mut self, f: F) -> Result
Write a bracketed list (e.g., “[a, b, c]”)
Sourcepub fn write_angles<F>(&mut self, f: F) -> Result
pub fn write_angles<F>(&mut self, f: F) -> Result
Write an angle-bracketed list (e.g., “<T, U>”)