facet_generate 0.17.0

Generate Swift, Kotlin and TypeScript from types annotated with `#[derive(Facet)]`
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
//! Indentation-aware writer used by [`Emitter`](crate::generation::Emitter)
//! implementations to produce correctly indented source code.
//!
//! Wrap any [`Write`] in an [`IndentedWriter`](crate::generation::indent::IndentedWriter) and use [`indent()`] /
//! [`unindent()`] to change the nesting level — every new line is
//! automatically prefixed with the right amount of whitespace. For `{ }`
//! blocks, [`block()`] returns an RAII [`Block`](crate::generation::indent::Block) guard that indents on
//! creation and writes the closing brace on drop.
//!
//! [`indent()`]: crate::generation::indent::IndentWrite::indent
//! [`unindent()`]: crate::generation::indent::IndentWrite::unindent
//! [`block()`]: crate::generation::indent::IndentWrite::block

use std::io::{Result, Write};

/// How a single indentation level is represented in the output.
#[derive(Clone, Copy, Debug)]
pub enum IndentConfig {
    Tab,
    Space(usize),
}

/// Controls where extra newlines are inserted around a `{ }` block opened
/// by [`IndentWrite::block`]. See the constants for examples.
#[derive(Clone, Copy, Default)]
pub struct Newlines {
    pub after_open: usize,
    pub after_close: usize,
}

impl Newlines {
    /// No extra newlines around the braces.
    ///
    /// Produces `{<content>}` — rarely used in practice.
    pub const NONE: Self = Self {
        after_open: 0,
        after_close: 0,
    };

    /// Newline after the opening brace only.
    ///
    /// Produces:
    /// ```text
    /// {
    ///     <content>}
    /// ```
    ///
    /// Good for inline closures / trailing lambdas where more code follows
    /// the closing brace on the same line, e.g. TypeScript:
    /// ```text
    /// serializeArray(value, serializer, (item, serializer) => {
    ///     serializer.serializeStr(item);
    /// });
    /// ```
    /// or C# object initializers:
    /// ```text
    /// return new MyClass {
    ///     Foo = foo,
    /// };
    /// ```
    pub const OPEN: Self = Self {
        after_open: 1,
        after_close: 0,
    };

    /// Newline after the closing brace only.
    ///
    /// Produces:
    /// ```text
    /// {<content>}
    /// ```
    ///
    /// Good for empty or minimal bodies where the opening `{` belongs to
    /// the preceding declaration, e.g. Kotlin:
    /// ```text
    /// data class UnitVariant() {
    /// }
    /// ```
    pub const CLOSE: Self = Self {
        after_open: 0,
        after_close: 1,
    };

    /// Newlines after both braces — standard block formatting.
    ///
    /// Produces:
    /// ```text
    /// {
    ///     <content>
    /// }
    /// ```
    ///
    /// The most common choice — used for class bodies, function bodies,
    /// enum declarations, namespace blocks, etc.
    pub const BOTH: Self = Self {
        after_open: 1,
        after_close: 1,
    };
}

/// Extension of [`Write`] that tracks indentation level.
///
/// Call [`indent()`](Self::indent) / [`unindent()`](Self::unindent) to
/// change nesting depth, or use [`block()`](Self::block) for RAII-managed
/// `{ }` pairs. The concrete implementation is [`IndentedWriter`].
pub trait IndentWrite: Write {
    /// Returns the indentation config this writer was created with.
    ///
    /// This is object-safe (no `Self: Sized` bound) so it can be called
    /// through a `&mut dyn IndentWrite` trait object.
    fn config(&self) -> IndentConfig;

    fn indent(&mut self) {}
    fn unindent(&mut self) {}

    /// Start a new block with RAII-style automatic closing.
    ///
    /// Writes a brace and indents, returning a [`Block`] guard that implements [`IndentWrite`].
    /// When the guard is dropped it unindents and writes a closing brace.
    /// Newlines are inserted according to the configured [`Newlines`].
    ///
    /// # Errors
    /// Returns an error if writing to the underlying writer fails.
    fn block(&mut self, newlines: Newlines) -> Result<Block<'_>>
    where
        Self: Sized,
    {
        write!(self, "{{")?;
        for _ in 0..newlines.after_open {
            writeln!(self)?;
        }
        self.indent();
        Ok(Block {
            writer: self,
            newlines,
        })
    }

    /// Create a child writer that writes to `out`, inheriting this writer's
    /// [`IndentConfig`].
    ///
    /// The child writes to its own separate `out` — the parent's buffer is
    /// unaffected.  Use this when output ordering forces you to buffer content
    /// before flushing it to the parent (e.g. writing feature helpers after
    /// imports).
    fn child<W: Write>(&self, out: W) -> IndentedWriter<W>
    where
        Self: Sized,
    {
        IndentedWriter::new(out, self.config())
    }

    /// Create a child writer that writes *through* this writer (i.e. uses the
    /// parent as its buffer), inheriting the [`IndentConfig`].
    ///
    /// The parent's current indentation becomes the baseline; the child's own
    /// [`indent`](Self::indent) / [`unindent`](Self::unindent) calls add on
    /// top of it.  The intermediate `Vec` + `write_all` pattern is eliminated
    /// entirely.
    fn child_writer(&mut self) -> IndentedWriter<&mut Self>
    where
        Self: Sized,
    {
        let config = self.config();
        IndentedWriter::new(self, config)
    }
}

/// RAII guard returned by [`IndentWrite::block`].
///
/// Implements [`Write`] and [`IndentWrite`] by delegating to the wrapped
/// writer. On [`Drop`], it unindents and writes the closing `}\n`.
pub struct Block<'a> {
    writer: &'a mut dyn IndentWrite,
    newlines: Newlines,
}

impl Write for Block<'_> {
    fn write(&mut self, buf: &[u8]) -> Result<usize> {
        self.writer.write(buf)
    }

    fn flush(&mut self) -> Result<()> {
        self.writer.flush()
    }
}

impl IndentWrite for Block<'_> {
    fn config(&self) -> IndentConfig {
        self.writer.config()
    }

    fn indent(&mut self) {
        self.writer.indent();
    }

    fn unindent(&mut self) {
        self.writer.unindent();
    }
}

impl Drop for Block<'_> {
    fn drop(&mut self) {
        self.writer.unindent();
        let _ = write!(self.writer, "}}");
        for _ in 0..self.newlines.after_close {
            let _ = writeln!(self.writer);
        }
    }
}

/// Wraps any [`Write`] and automatically prepends the current indentation
/// at the start of each new line.
pub struct IndentedWriter<T> {
    out: T,
    indentation: Vec<u8>,
    config: IndentConfig,
    at_beginning_of_line: bool,
}

impl<T> IndentedWriter<T> {
    pub const fn new(out: T, config: IndentConfig) -> Self {
        Self {
            out,
            indentation: Vec::new(),
            config,
            at_beginning_of_line: true,
        }
    }
}

impl<T: Write> IndentWrite for IndentedWriter<T> {
    fn config(&self) -> IndentConfig {
        self.config
    }

    fn indent(&mut self) {
        match self.config {
            IndentConfig::Tab => {
                self.indentation.push(b'\t');
            }
            IndentConfig::Space(n) => {
                self.indentation.resize(self.indentation.len() + n, b' ');
            }
        }
    }

    fn unindent(&mut self) {
        match self.config {
            IndentConfig::Tab => {
                self.indentation.pop();
            }
            IndentConfig::Space(n) => {
                self.indentation
                    .truncate(self.indentation.len().saturating_sub(n));
            }
        }
    }
}

impl<T: Write> Write for IndentedWriter<T> {
    fn write(&mut self, mut buf: &[u8]) -> Result<usize> {
        let mut bytes_written = 0;

        while !buf.is_empty() {
            let (before_newline, has_newline, after_newline) =
                buf.iter().position(|&b| b == b'\n').map_or_else(
                    || (buf, false, &buf[buf.len()..]),
                    |idx| (&buf[..idx], true, &buf[idx + 1..]),
                );

            if self.at_beginning_of_line && !before_newline.is_empty() {
                self.out.write_all(&self.indentation)?;
                self.at_beginning_of_line = false;
            }

            self.out.write_all(before_newline)?;
            bytes_written += before_newline.len();

            if has_newline {
                self.out.write_all(b"\n")?;
                bytes_written += 1;
                self.at_beginning_of_line = true;
            }

            buf = after_newline;
        }

        Ok(bytes_written)
    }

    fn flush(&mut self) -> Result<()> {
        self.out.flush()
    }
}

/// Creates a `{ }` block scope on a `dyn IndentWrite` writer.
///
/// This is the dynamic-dispatch equivalent of [`IndentWrite::block`].
/// Because `block()` requires `Self: Sized` (it stores `&mut dyn IndentWrite`
/// internally), it cannot be called through a trait object. This free function
/// fills that gap — it writes the opening brace, bumps indentation, invokes
/// the closure, then restores indentation and writes the closing brace.
///
/// # Example
///
/// ```rust,ignore
/// use facet_generate::generation::indent::{with_block, Newlines};
///
/// fn emit(w: &mut dyn IndentWrite) -> std::io::Result<()> {
///     write!(w, "fun serialize() ")?;
///     with_block(w, Newlines::BOTH, |w| {
///         writeln!(w, "// body")?;
///         Ok(())
///     })
/// }
/// ```
///
/// # Errors
///
/// Returns an error if writing the opening brace, invoking the closure, or
/// writing the closing brace fails.
pub fn with_block<F>(w: &mut dyn IndentWrite, newlines: Newlines, f: F) -> Result<()>
where
    F: FnOnce(&mut dyn IndentWrite) -> Result<()>,
{
    write!(w, "{{")?;
    for _ in 0..newlines.after_open {
        writeln!(w)?;
    }
    w.indent();
    let result = f(w);
    w.unindent();
    write!(w, "}}")?;
    for _ in 0..newlines.after_close {
        writeln!(w)?;
    }
    result
}

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

    #[test]
    fn basic() -> Result<()> {
        let mut buffer: Vec<u8> = Vec::new();

        let mut out = IndentedWriter::new(&mut buffer, IndentConfig::Space(2));

        writeln!(out, "foo")?;
        out.indent();
        writeln!(out, "bar")?;
        writeln!(out)?;
        writeln!(out, "bar")?;
        out.indent();
        writeln!(out, "foobar")?;
        writeln!(out)?;
        writeln!(out, "foobar")?;
        out.unindent();
        out.unindent();
        writeln!(out, "foo")?;

        insta::assert_snapshot!(String::from_utf8(buffer).unwrap(), @"
        foo
          bar

          bar
            foobar

            foobar
        foo
        ");

        Ok(())
    }

    #[test]
    fn function_block() -> Result<()> {
        let mut buffer: Vec<u8> = Vec::new();

        let mut w = IndentedWriter::new(&mut buffer, IndentConfig::Space(2));

        write!(w, "fn foo() ")?;
        {
            let mut w = w.block(Newlines::BOTH)?;
            writeln!(w, r#"let _ = "hello";"#)?;
        }

        insta::assert_snapshot!(String::from_utf8(buffer).unwrap(), @r#"
        fn foo() {
          let _ = "hello";
        }
        "#);

        Ok(())
    }

    #[test]
    fn closure_block() -> Result<()> {
        let mut buffer: Vec<u8> = Vec::new();

        let mut w = IndentedWriter::new(&mut buffer, IndentConfig::Space(2));

        write!(w, "fn foo() ")?;
        {
            let mut w = w.block(Newlines::OPEN)?;
            write!(w, r#"let _ = "hello";"#)?;
        }

        insta::assert_snapshot!(String::from_utf8(buffer).unwrap(), @r#"
        fn foo() {
          let _ = "hello";}
        "#);

        Ok(())
    }

    #[test]
    fn trailing_block() -> Result<()> {
        let mut buffer: Vec<u8> = Vec::new();

        let mut w = IndentedWriter::new(&mut buffer, IndentConfig::Space(2));

        write!(w, "fn foo() ")?;
        {
            let mut w = w.block(Newlines::CLOSE)?;
            write!(w, r#"let _ = "hello";"#)?;
        }

        insta::assert_snapshot!(String::from_utf8(buffer).unwrap(), @r#"fn foo() {let _ = "hello";}"#);

        Ok(())
    }

    // ----- child writer tests -----

    /// A child created with `parent.child(out)` writes to its own buffer but
    /// inherits the parent's `IndentConfig`, so callers never have to repeat
    /// the config at every call-site.
    #[test]
    fn child_inherits_config() -> Result<()> {
        // Use Space(3) – a deliberately unusual width so it is obvious the
        // child is not just picking up a hardcoded default.
        let mut parent_buf: Vec<u8> = Vec::new();
        let parent = IndentedWriter::new(&mut parent_buf, IndentConfig::Space(3));

        let mut child_buf: Vec<u8> = Vec::new();
        let mut child = parent.child(&mut child_buf);

        writeln!(child, "top")?;
        child.indent();
        writeln!(child, "indented")?;
        child.unindent();
        writeln!(child, "bottom")?;
        drop(child);

        // The parent's own buffer must be untouched – the child wrote to its
        // own separate buffer.
        assert!(parent_buf.is_empty());

        // The child used Space(3) inherited from the parent.
        insta::assert_snapshot!(String::from_utf8(child_buf).unwrap(), @"
top
   indented
bottom
");

        Ok(())
    }

    /// A child created with `parent.child_writer()` writes *through* the
    /// parent rather than to a separate buffer.  The parent's current
    /// indentation becomes the baseline, and the child's own indent/unindent
    /// calls add on top of it.
    #[test]
    fn child_writer_uses_parents_buffer() -> Result<()> {
        let mut buffer: Vec<u8> = Vec::new();
        let mut parent = IndentedWriter::new(&mut buffer, IndentConfig::Space(2));

        writeln!(parent, "outer")?;
        parent.indent(); // parent is now at one level (2 spaces)

        {
            let mut child = parent.child_writer();
            writeln!(child, "middle")?; // gets parent's 2-space base
            child.indent();
            writeln!(child, "inner")?; // gets parent's 2 + child's 2 = 4 spaces
        } // child dropped; parent borrow released

        parent.unindent();
        writeln!(parent, "end")?;

        insta::assert_snapshot!(String::from_utf8(buffer).unwrap(), @"
outer
  middle
    inner
end
");

        Ok(())
    }

    /// When the parent is only available as `&mut dyn IndentWrite`, callers
    /// obtain the config with `w.config()` and then construct the child
    /// manually.  This covers the pattern used in bincode/kotlin where the
    /// intermediate `Vec` + `write_all` can be eliminated.
    #[test]
    fn child_writer_through_dyn_indent_write() -> Result<()> {
        let mut buffer: Vec<u8> = Vec::new();
        let mut parent = IndentedWriter::new(&mut buffer, IndentConfig::Space(4));
        parent.indent(); // simulate being one level deep (e.g., inside a class body)

        let w: &mut dyn IndentWrite = &mut parent;
        {
            let config = w.config();
            let mut child = IndentedWriter::new(&mut *w, config);

            writeln!(child, "content")?; // parent's 4-space base
            child.indent();
            writeln!(child, "nested")?; // parent's 4 + child's 4 = 8 spaces
        }

        insta::assert_snapshot!(String::from_utf8(buffer).unwrap(), @"
    content
        nested
");

        Ok(())
    }
}