Skip to main content

Module writer

Module writer 

Source
Expand description

A small, deterministic code-emission toolkit shared by every generator.

Before this module existed, all eleven generators built their output by hand with thousands of out.push_str(&format!(...)) calls, threading the current indentation through every call site as literal spaces. That made the shape of the emitted code invisible in the Rust source and turned indentation and block nesting into a manual, bug-prone bookkeeping chore.

CodeWriter owns the indentation and block scoping so a backend writes intent (line, block, scope) instead of whitespace. It is intentionally tiny and unopinionated: it does not reflow or pretty-print, so a backend stays in full control of the exact text it emits while losing the manual \n/indent bookkeeping. Output is byte-deterministic: blank lines never carry trailing whitespace, and the indent unit is fixed per writer.

use weaveffi_core::codegen::writer::CodeWriter;

let mut w = CodeWriter::new("    ");
w.line("class Greeter:");
w.scope(|w| {
    w.line("def hello(self):");
    w.scope(|w| {
        w.line("return \"hi\"");
    });
});
assert_eq!(
    w.finish(),
    "class Greeter:\n    def hello(self):\n        return \"hi\"\n",
);

Structsยง

CodeWriter
An indentation-aware string builder for generated source code.