weaveffi-core 0.15.0

Generator trait, orchestrator, validation, and shared utilities for WeaveFFI
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
//! 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",
//! );
//! ```

use crate::codegen::common::{emit_doc, DocCommentStyle};

/// An indentation-aware string builder for generated source code.
///
/// Construct one with [`CodeWriter::new`], passing the per-target indent unit
/// (`"    "`, `"  "`, or `"\t"`). Emit lines with [`line`](Self::line), nest
/// with [`scope`](Self::scope) / [`block`](Self::block), splice pre-rendered
/// multi-line text with [`block_raw`](Self::block_raw), and finish with
/// [`finish`](Self::finish).
#[derive(Debug, Clone)]
pub struct CodeWriter {
    buf: String,
    depth: usize,
    unit: String,
}

impl CodeWriter {
    /// Create an empty writer whose one indent level is `unit` (commonly
    /// `"    "`, `"  "`, or `"\t"`).
    pub fn new(unit: impl Into<String>) -> Self {
        Self {
            buf: String::new(),
            depth: 0,
            unit: unit.into(),
        }
    }

    /// Create an empty writer with a four-space indent unit, the most common
    /// default across the generators.
    pub fn four_space() -> Self {
        Self::new("    ")
    }

    /// Create an empty writer with a two-space indent unit.
    pub fn two_space() -> Self {
        Self::new("  ")
    }

    /// Create an empty writer with a tab indent unit.
    pub fn tabs() -> Self {
        Self::new("\t")
    }

    /// The current indentation depth (number of `unit`s prepended to a line).
    pub fn depth(&self) -> usize {
        self.depth
    }

    /// Start the writer already nested `depth` levels deep. Useful when
    /// rendering a fragment that belongs inside an enclosing block whose
    /// indentation the caller tracks separately (for example a backend that
    /// still threads an explicit indent string through its render functions).
    #[must_use]
    pub fn with_depth(mut self, depth: usize) -> Self {
        self.depth = depth;
        self
    }

    /// The literal indentation prefix at the current depth. Useful when calling
    /// a helper that takes an explicit indent string.
    pub fn indent_str(&self) -> String {
        self.unit.repeat(self.depth)
    }

    /// Increase the indentation depth by one level.
    pub fn indent(&mut self) -> &mut Self {
        self.depth += 1;
        self
    }

    /// Decrease the indentation depth by one level. Saturates at zero so an
    /// unbalanced `dedent` can never panic mid-render.
    pub fn dedent(&mut self) -> &mut Self {
        self.depth = self.depth.saturating_sub(1);
        self
    }

    /// Write one line at the current indentation, followed by a newline.
    ///
    /// An empty (or whitespace-only-after-trim is *not* applied here; only a
    /// truly empty string) argument emits a bare newline with no trailing
    /// whitespace, so callers can use `line("")` interchangeably with
    /// [`blank`](Self::blank).
    pub fn line(&mut self, s: impl AsRef<str>) -> &mut Self {
        let s = s.ref_str();
        if s.is_empty() {
            self.buf.push('\n');
        } else {
            self.buf.push_str(&self.unit.repeat(self.depth));
            self.buf.push_str(s);
            self.buf.push('\n');
        }
        self
    }

    /// Write a blank line (a single newline, never trailing whitespace).
    pub fn blank(&mut self) -> &mut Self {
        self.buf.push('\n');
        self
    }

    /// Append text verbatim with no indentation and no trailing newline.
    ///
    /// Use this for already-fully-formatted fragments (a generated-file
    /// prelude, a precomputed block) that must be spliced in unchanged.
    pub fn raw(&mut self, s: impl AsRef<str>) -> &mut Self {
        self.buf.push_str(s.ref_str());
        self
    }

    /// Splice a multi-line fragment, re-indenting every non-empty line to the
    /// current depth while preserving the fragment's own *relative*
    /// indentation. A trailing newline on the fragment is honored; blank lines
    /// stay blank (no trailing whitespace).
    ///
    /// This is the migration workhorse: a backend can keep a large literal
    /// snippet as a raw string and let the writer place it at the right depth.
    pub fn block_raw(&mut self, s: impl AsRef<str>) -> &mut Self {
        let s = s.ref_str();
        if s.is_empty() {
            return self;
        }
        let prefix = self.unit.repeat(self.depth);
        // Split on '\n'; a trailing newline yields a final empty segment we
        // must not emit as its own indented line.
        let ends_with_newline = s.ends_with('\n');
        let mut lines = s.split('\n').peekable();
        while let Some(line) = lines.next() {
            let is_last = lines.peek().is_none();
            if is_last && line.is_empty() && ends_with_newline {
                // The empty tail produced by a trailing '\n': stop, the
                // previous iteration already wrote that newline.
                break;
            }
            if line.is_empty() {
                self.buf.push('\n');
            } else {
                self.buf.push_str(&prefix);
                self.buf.push_str(line);
                self.buf.push('\n');
            }
        }
        self
    }

    /// Run `f` with the indentation increased by one level, then restore it.
    pub fn scope(&mut self, f: impl FnOnce(&mut Self)) -> &mut Self {
        self.indent();
        f(self);
        self.dedent();
        self
    }

    /// Emit `open`, run `f` at one deeper indent, then emit `close` at the
    /// original indent. The canonical way to write a braced or `:`-introduced
    /// block.
    ///
    /// ```
    /// use weaveffi_core::codegen::writer::CodeWriter;
    /// let mut w = CodeWriter::four_space();
    /// w.block("fn main() {", "}", |w| {
    ///     w.line("println!(\"hi\");");
    /// });
    /// assert_eq!(w.finish(), "fn main() {\n    println!(\"hi\");\n}\n");
    /// ```
    pub fn block(
        &mut self,
        open: impl AsRef<str>,
        close: impl AsRef<str>,
        f: impl FnOnce(&mut Self),
    ) -> &mut Self {
        self.line(open);
        self.scope(f);
        self.line(close);
        self
    }

    /// Emit a doc comment for `doc` in `style` at the current indentation.
    /// No-op when `doc` is `None` or trims to empty. Mirrors [`emit_doc`],
    /// but indents from the writer's current depth instead of an explicit
    /// prefix argument.
    pub fn doc(&mut self, doc: &Option<String>, style: DocCommentStyle) -> &mut Self {
        let prefix = self.unit.repeat(self.depth);
        emit_doc(&mut self.buf, doc, &prefix, style);
        self
    }

    /// Borrow the accumulated text without consuming the writer.
    pub fn as_str(&self) -> &str {
        &self.buf
    }

    /// True when nothing has been written yet.
    pub fn is_empty(&self) -> bool {
        self.buf.is_empty()
    }

    /// Consume the writer and return the accumulated source text.
    pub fn finish(self) -> String {
        self.buf
    }
}

/// Tiny internal helper so `line`/`raw`/`block_raw` accept both `&str` and
/// `String` (and `&String`) without each method taking a turbofished generic
/// that the docs would have to explain. Not part of the public API.
trait RefStr {
    fn ref_str(&self) -> &str;
}

impl<T: AsRef<str>> RefStr for T {
    fn ref_str(&self) -> &str {
        self.as_ref()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn line_indents_and_newline_terminates() {
        let mut w = CodeWriter::four_space();
        w.line("a");
        w.indent();
        w.line("b");
        w.dedent();
        w.line("c");
        assert_eq!(w.finish(), "a\n    b\nc\n");
    }

    #[test]
    fn empty_line_is_bare_newline() {
        let mut w = CodeWriter::four_space();
        w.indent();
        w.line("");
        w.blank();
        w.line("x");
        assert_eq!(w.finish(), "\n\n    x\n");
    }

    #[test]
    fn scope_restores_depth() {
        let mut w = CodeWriter::two_space();
        w.line("outer");
        w.scope(|w| {
            w.line("inner");
            w.scope(|w| {
                w.line("deepest");
            });
            w.line("inner again");
        });
        w.line("outer again");
        assert_eq!(
            w.finish(),
            "outer\n  inner\n    deepest\n  inner again\nouter again\n"
        );
    }

    #[test]
    fn block_brackets_body() {
        let mut w = CodeWriter::four_space();
        w.block("if (x) {", "}", |w| {
            w.line("do_a();");
            w.line("do_b();");
        });
        assert_eq!(w.finish(), "if (x) {\n    do_a();\n    do_b();\n}\n");
    }

    #[test]
    fn nested_blocks() {
        let mut w = CodeWriter::four_space();
        w.block("class A:", "", |w| {
            w.block("def f(self):", "", |w| {
                w.line("pass");
            });
        });
        // Note: an empty close just emits a bare newline.
        assert_eq!(w.finish(), "class A:\n    def f(self):\n        pass\n\n\n");
    }

    #[test]
    fn raw_appends_verbatim() {
        let mut w = CodeWriter::four_space();
        w.indent();
        w.raw("no-indent");
        w.raw(" continues");
        assert_eq!(w.finish(), "no-indent continues");
    }

    #[test]
    fn block_raw_reindents_relative_structure() {
        let mut w = CodeWriter::four_space();
        w.indent();
        w.block_raw("def foo():\n    return 1\n");
        assert_eq!(w.finish(), "    def foo():\n        return 1\n");
    }

    #[test]
    fn block_raw_preserves_blank_lines_without_trailing_ws() {
        let mut w = CodeWriter::two_space();
        w.indent();
        w.block_raw("a\n\nb\n");
        assert_eq!(w.finish(), "  a\n\n  b\n");
    }

    #[test]
    fn block_raw_without_trailing_newline() {
        let mut w = CodeWriter::four_space();
        w.block_raw("one\ntwo");
        assert_eq!(w.finish(), "one\ntwo\n");
    }

    #[test]
    fn block_raw_empty_is_noop() {
        let mut w = CodeWriter::four_space();
        w.block_raw("");
        assert!(w.is_empty());
    }

    #[test]
    fn doc_uses_current_indent() {
        let mut w = CodeWriter::four_space();
        w.indent();
        w.doc(&Some("Hello.".to_string()), DocCommentStyle::TripleSlash);
        w.line("fn f() {}");
        assert_eq!(w.finish(), "    /// Hello.\n    fn f() {}\n");
    }

    #[test]
    fn doc_none_is_noop() {
        let mut w = CodeWriter::four_space();
        w.doc(&None, DocCommentStyle::Hash);
        assert!(w.is_empty());
    }

    #[test]
    fn accepts_string_and_str() {
        let mut w = CodeWriter::four_space();
        let owned = String::from("owned");
        w.line(&owned);
        w.line("borrowed");
        w.line(format!("fmt {}", 1));
        assert_eq!(w.finish(), "owned\nborrowed\nfmt 1\n");
    }

    #[test]
    fn indent_str_reflects_depth() {
        let mut w = CodeWriter::new("  ");
        assert_eq!(w.indent_str(), "");
        w.indent().indent();
        assert_eq!(w.indent_str(), "    ");
        assert_eq!(w.depth(), 2);
    }

    #[test]
    fn with_depth_seeds_initial_indentation() {
        let mut w = CodeWriter::four_space().with_depth(2);
        w.line("if x:");
        w.scope(|w| {
            w.line("pass");
        });
        assert_eq!(w.finish(), "        if x:\n            pass\n");
    }

    #[test]
    fn dedent_saturates_at_zero() {
        let mut w = CodeWriter::four_space();
        w.dedent().dedent();
        w.line("x");
        assert_eq!(w.finish(), "x\n");
    }
}