CodeWriter

Struct CodeWriter 

Source
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>

Source

pub fn new(writer: W, indent_string: String) -> Self

Create a new CodeWriter with the given writer and indent string (e.g., “ “ or “\t”)

Source

pub fn with_indent_spaces(writer: W, spaces: usize) -> Self

Create a new CodeWriter with 4-space indentation

Source

pub fn write(&mut self, text: &str) -> Result

Write text without a newline. Adds indentation if at line start.

Source

pub fn writeln(&mut self, text: &str) -> Result

Write text followed by a newline. Adds indentation if needed.

Source

pub fn blank_line(&mut self) -> Result

Write an empty line

Source

pub fn indent(&mut self) -> IndentGuard

Create an indentation guard. Indentation increases while the guard is alive.

Source

pub fn comment(&mut self, comment_prefix: &str, text: &str) -> Result

Write a single-line comment (e.g., “// comment”)

Source

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.

Source

pub fn begin_block(&mut self, header: &str) -> Result<IndentGuard, Error>

Begin a block with opening brace: writes “header {” and returns indent guard

Source

pub fn end_block(&mut self) -> Result

End a block with closing brace

Source

pub fn block<F>(&mut self, header: &str, body: F) -> Result
where F: FnOnce(&mut Self) -> Result,

Write a complete block with a closure for the body

Source

pub fn indent_level(&self) -> usize

Get the current indentation level

Source

pub fn into_inner(self) -> W

Consume the writer and return the inner writer

Source

pub fn inner(&self) -> &W

Get a reference to the inner writer

Source

pub fn inner_mut(&mut self) -> &mut W

Get a mutable reference to the inner writer

Source

pub fn write_separated<I, F>( &mut self, items: I, separator: &str, write_item: F, ) -> Result
where I: IntoIterator, F: FnMut(&mut Self, I::Item) -> Result,

Write items separated by a delimiter (e.g., comma-separated list)

Source

pub fn write_separated_lines<I, F>( &mut self, items: I, separator: &str, write_item: F, ) -> Result
where I: IntoIterator, F: FnMut(&mut Self, I::Item) -> Result,

Write items separated by delimiter with newlines (one item per line)

Source

pub fn write_if<F>(&mut self, condition: bool, f: F) -> Result
where F: FnOnce(&mut Self) -> Result,

Conditionally write content

Source

pub fn write_parens<F>(&mut self, f: F) -> Result
where F: FnOnce(&mut Self) -> Result,

Write a parenthesized list (e.g., “(a, b, c)”)

Source

pub fn write_brackets<F>(&mut self, f: F) -> Result
where F: FnOnce(&mut Self) -> Result,

Write a bracketed list (e.g., “[a, b, c]”)

Source

pub fn write_angles<F>(&mut self, f: F) -> Result
where F: FnOnce(&mut Self) -> Result,

Write an angle-bracketed list (e.g., “<T, U>”)

Auto Trait Implementations§

§

impl<W> !Freeze for CodeWriter<W>

§

impl<W> !RefUnwindSafe for CodeWriter<W>

§

impl<W> !Send for CodeWriter<W>

§

impl<W> !Sync for CodeWriter<W>

§

impl<W> Unpin for CodeWriter<W>
where W: Unpin,

§

impl<W> !UnwindSafe for CodeWriter<W>

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.