weaveffi-core 0.7.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
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
//! Shared codegen primitives that every language generator can reuse.
//!
//! Until 0.4.0, every generator hand-rolled its own copy of the module
//! tree walker, the doc-comment emitter, and the "is this type a C
//! pointer at the ABI boundary?" predicate. Pulling them in here gives
//! the generators one source of truth and shrinks each crate by a few
//! dozen lines of near-identical helper code.
//!
//! Specialised flavours that exist in only one generator (Go's
//! godoc-style first-line symbol prefix, .NET's `<summary>` XML tags,
//! Python's triple-quoted docstring) stay generator-local because
//! their behaviour is non-uniform; this module deliberately covers
//! only the common 80%.

use weaveffi_ir::ir::{Module, TypeRef};

/// Doc-comment flavour used by [`emit_doc`].
///
/// Specialised flavours like Go's godoc-symbol prefix or .NET's
/// `<summary>` element are intentionally absent and remain in their
/// own generators because their first-line behaviour is non-uniform.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum DocCommentStyle {
    /// `/// ...` per line (Swift, Dart, Rust).
    TripleSlash,
    /// `# ...` per line (Python `#` comments, Ruby).
    Hash,
    /// `// ...` per line (Go base case; Go's symbol-prefixed
    /// godoc convention stays generator-local).
    DoubleSlash,
    /// `/** ... */` block; single-line collapses to `/** text */`
    /// (C, C++, Kotlin/KDoc, JSDoc, TypeScript .d.ts).
    Javadoc,
}

/// Emit a doc comment for `doc` at the given `indent`, using the given
/// `style`. No-op when `doc` is `None` or trims to empty.
///
/// The output always ends with a trailing newline so a generator can
/// follow it directly with the symbol declaration on the next line.
pub fn emit_doc(out: &mut String, doc: &Option<String>, indent: &str, style: DocCommentStyle) {
    let Some(doc) = doc else {
        return;
    };
    let doc = doc.trim();
    if doc.is_empty() {
        return;
    }
    match style {
        DocCommentStyle::TripleSlash => emit_line_doc(out, doc, indent, "///"),
        DocCommentStyle::Hash => emit_line_doc(out, doc, indent, "#"),
        DocCommentStyle::DoubleSlash => emit_line_doc(out, doc, indent, "//"),
        DocCommentStyle::Javadoc => emit_javadoc(out, doc, indent),
    }
}

fn emit_line_doc(out: &mut String, doc: &str, indent: &str, marker: &str) {
    for line in doc.lines() {
        out.push_str(indent);
        if line.is_empty() {
            out.push_str(marker);
            out.push('\n');
        } else {
            out.push_str(marker);
            out.push(' ');
            out.push_str(line);
            out.push('\n');
        }
    }
}

fn emit_javadoc(out: &mut String, doc: &str, indent: &str) {
    if doc.contains('\n') {
        out.push_str(indent);
        out.push_str("/**\n");
        for line in doc.lines() {
            out.push_str(indent);
            if line.is_empty() {
                out.push_str(" *\n");
            } else {
                out.push_str(" * ");
                out.push_str(line);
                out.push('\n');
            }
        }
        out.push_str(indent);
        out.push_str(" */\n");
    } else {
        out.push_str(indent);
        out.push_str("/** ");
        out.push_str(doc);
        out.push_str(" */\n");
    }
}

/// Iterate over every module in `roots` and its descendants in
/// depth-first pre-order: each module is yielded before its children,
/// and children are yielded left-to-right.
///
/// Equivalent to the recursive `collect_all_modules` helper that
/// every generator used to define locally.
pub fn walk_modules<'a>(roots: &'a [Module]) -> impl Iterator<Item = &'a Module> {
    let mut stack: Vec<&'a Module> = roots.iter().rev().collect();
    std::iter::from_fn(move || {
        let m = stack.pop()?;
        for child in m.modules.iter().rev() {
            stack.push(child);
        }
        Some(m)
    })
}

/// Like [`walk_modules`], but each module is paired with its
/// underscore-joined path (e.g. `parent_child_grandchild`). The path
/// matches the canonical C symbol prefix segment that the C generator
/// builds when emitting `{c_prefix}_{module_path}_{name}`.
pub fn walk_modules_with_path<'a>(
    roots: &'a [Module],
) -> impl Iterator<Item = (&'a Module, String)> {
    let mut stack: Vec<(&'a Module, String)> =
        roots.iter().rev().map(|m| (m, m.name.clone())).collect();
    std::iter::from_fn(move || {
        let (m, path) = stack.pop()?;
        for child in m.modules.iter().rev() {
            stack.push((child, format!("{path}_{}", child.name)));
        }
        Some((m, path))
    })
}

/// Predicate: returns `true` when the IR type is represented as a
/// pointer at the C ABI boundary.
///
/// String types, byte buffers, struct values, typed handles, lists,
/// maps, and iterators all cross the ABI as pointers. Scalars
/// (`i32`/`bool`/etc.), `Handle`, and `Enum(_)` cross by value.
///
/// `Optional(T)` is *not* automatically a pointer here: callers that
/// care about Optional pointer-ness (the C/C++ generators) recurse
/// into the inner type before consulting this predicate.
pub fn is_c_pointer_type(ty: &TypeRef) -> bool {
    matches!(
        ty,
        TypeRef::StringUtf8
            | TypeRef::BorrowedStr
            | TypeRef::Bytes
            | TypeRef::BorrowedBytes
            | TypeRef::Struct(_)
            | TypeRef::TypedHandle(_)
            | TypeRef::List(_)
            | TypeRef::Map(_, _)
            | TypeRef::Iterator(_)
    )
}

/// Convert a `snake_case` identifier to `PascalCase` by uppercasing the
/// first character of each `_`-separated segment and preserving the rest.
///
/// This deliberately splits on `_` only — it does **not** re-case interior
/// letters the way `heck::ToUpperCamelCase` does — so an acronym-bearing
/// name like `get_HTTP` becomes `GetHTTP`, not `GetHttp`. It is the single
/// source of truth for the `snake_to_pascal` / `to_pascal_case` helpers
/// that the Python, Android, and WASM generators each defined locally.
pub fn pascal_case(s: &str) -> String {
    s.split('_')
        .map(|part| {
            let mut chars = part.chars();
            match chars.next() {
                None => String::new(),
                Some(first) => first.to_uppercase().chain(chars).collect::<String>(),
            }
        })
        .collect()
}

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

    fn leaf(name: &str) -> Module {
        Module {
            name: name.to_string(),
            functions: vec![],
            structs: vec![],
            enums: vec![],
            callbacks: vec![],
            listeners: vec![],
            errors: None,
            modules: vec![],
        }
    }

    fn with_children(name: &str, children: Vec<Module>) -> Module {
        Module {
            modules: children,
            ..leaf(name)
        }
    }

    // --- walk_modules ---

    #[test]
    fn walk_modules_visits_pre_order() {
        let roots = vec![
            with_children("a", vec![leaf("a1"), leaf("a2")]),
            with_children("b", vec![leaf("b1")]),
        ];
        let names: Vec<&str> = walk_modules(&roots).map(|m| m.name.as_str()).collect();
        assert_eq!(names, vec!["a", "a1", "a2", "b", "b1"]);
    }

    #[test]
    fn walk_modules_descends_to_arbitrary_depth() {
        let roots = vec![with_children(
            "a",
            vec![with_children(
                "b",
                vec![with_children("c", vec![leaf("d")])],
            )],
        )];
        let names: Vec<&str> = walk_modules(&roots).map(|m| m.name.as_str()).collect();
        assert_eq!(names, vec!["a", "b", "c", "d"]);
    }

    #[test]
    fn walk_modules_empty_input_yields_nothing() {
        let roots: Vec<Module> = vec![];
        assert_eq!(walk_modules(&roots).count(), 0);
    }

    // --- walk_modules_with_path ---

    #[test]
    fn walk_modules_with_path_joins_with_underscore() {
        let roots = vec![with_children(
            "outer",
            vec![with_children("inner", vec![leaf("leaf")])],
        )];
        let pairs: Vec<(String, String)> = walk_modules_with_path(&roots)
            .map(|(m, p)| (m.name.clone(), p))
            .collect();
        assert_eq!(
            pairs,
            vec![
                ("outer".into(), "outer".into()),
                ("inner".into(), "outer_inner".into()),
                ("leaf".into(), "outer_inner_leaf".into()),
            ]
        );
    }

    #[test]
    fn walk_modules_with_path_independent_roots() {
        let roots = vec![
            with_children("a", vec![leaf("a1")]),
            with_children("b", vec![leaf("b1")]),
        ];
        let paths: Vec<String> = walk_modules_with_path(&roots).map(|(_, p)| p).collect();
        assert_eq!(paths, vec!["a", "a_a1", "b", "b_b1"]);
    }

    // --- emit_doc ---

    #[test]
    fn emit_doc_none_writes_nothing() {
        let mut out = String::new();
        emit_doc(&mut out, &None, "", DocCommentStyle::TripleSlash);
        assert!(out.is_empty());
    }

    #[test]
    fn emit_doc_empty_string_writes_nothing() {
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("   \n  ".into()),
            "",
            DocCommentStyle::TripleSlash,
        );
        assert!(out.is_empty());
    }

    #[test]
    fn emit_doc_triple_slash_single_line() {
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("Hello, world.".into()),
            "  ",
            DocCommentStyle::TripleSlash,
        );
        assert_eq!(out, "  /// Hello, world.\n");
    }

    #[test]
    fn emit_doc_triple_slash_multi_line_with_blank() {
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("First line.\n\nThird line.".into()),
            "",
            DocCommentStyle::TripleSlash,
        );
        assert_eq!(out, "/// First line.\n///\n/// Third line.\n");
    }

    #[test]
    fn emit_doc_hash_single_line() {
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("ruby/python style".into()),
            "",
            DocCommentStyle::Hash,
        );
        assert_eq!(out, "# ruby/python style\n");
    }

    #[test]
    fn emit_doc_double_slash_single_line() {
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("Go-style line comment.".into()),
            "",
            DocCommentStyle::DoubleSlash,
        );
        assert_eq!(out, "// Go-style line comment.\n");
    }

    #[test]
    fn emit_doc_double_slash_multi_line() {
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("first\n\nsecond".into()),
            "\t",
            DocCommentStyle::DoubleSlash,
        );
        assert_eq!(out, "\t// first\n\t//\n\t// second\n");
    }

    #[test]
    fn emit_doc_hash_multi_line() {
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("one\n\ntwo".into()),
            "    ",
            DocCommentStyle::Hash,
        );
        assert_eq!(out, "    # one\n    #\n    # two\n");
    }

    #[test]
    fn emit_doc_javadoc_single_line_collapses() {
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("short".into()),
            "",
            DocCommentStyle::Javadoc,
        );
        assert_eq!(out, "/** short */\n");
    }

    #[test]
    fn emit_doc_javadoc_multi_line_expands() {
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("line one\n\nline three".into()),
            "  ",
            DocCommentStyle::Javadoc,
        );
        assert_eq!(out, "  /**\n   * line one\n   *\n   * line three\n   */\n");
    }

    #[test]
    fn emit_doc_trims_outer_whitespace_before_decisions() {
        // A doc that's "single line" after trimming should still
        // collapse to `/** text */` even if it had surrounding blank
        // lines in the IR — the existing per-generator behaviour we
        // are replacing did the same.
        let mut out = String::new();
        emit_doc(
            &mut out,
            &Some("\n\nhello\n\n".into()),
            "",
            DocCommentStyle::Javadoc,
        );
        assert_eq!(out, "/** hello */\n");
    }

    // --- is_c_pointer_type ---

    #[test]
    fn is_c_pointer_for_pointer_carrying_types() {
        for ty in [
            TypeRef::StringUtf8,
            TypeRef::BorrowedStr,
            TypeRef::Bytes,
            TypeRef::BorrowedBytes,
            TypeRef::Struct("X".into()),
            TypeRef::TypedHandle("X".into()),
            TypeRef::List(Box::new(TypeRef::I32)),
            TypeRef::Map(Box::new(TypeRef::StringUtf8), Box::new(TypeRef::I32)),
            TypeRef::Iterator(Box::new(TypeRef::StringUtf8)),
        ] {
            assert!(is_c_pointer_type(&ty), "expected pointer: {ty:?}");
        }
    }

    #[test]
    fn is_c_pointer_for_value_types_is_false() {
        for ty in [
            TypeRef::I32,
            TypeRef::U32,
            TypeRef::I64,
            TypeRef::F64,
            TypeRef::Bool,
            TypeRef::Handle,
            TypeRef::Enum("E".into()),
        ] {
            assert!(!is_c_pointer_type(&ty), "expected non-pointer: {ty:?}");
        }
    }

    #[test]
    fn is_c_pointer_does_not_recurse_into_optional() {
        // Callers that care about Optional pointer-ness recurse first.
        // We document and enforce that contract: bare Optional is not
        // a pointer.
        assert!(!is_c_pointer_type(&TypeRef::Optional(Box::new(
            TypeRef::I32
        ))));
        assert!(!is_c_pointer_type(&TypeRef::Optional(Box::new(
            TypeRef::StringUtf8
        ))));
    }

    // --- pascal_case ---

    #[test]
    fn pascal_case_snake_segments() {
        assert_eq!(pascal_case("first_name"), "FirstName");
        assert_eq!(pascal_case("name"), "Name");
        assert_eq!(pascal_case("is_active"), "IsActive");
    }

    #[test]
    fn pascal_case_preserves_interior_casing() {
        // Unlike heck, interior letters keep their case (acronym-safe).
        assert_eq!(pascal_case("get_HTTP"), "GetHTTP");
        assert_eq!(pascal_case("toJSON"), "ToJSON");
    }

    #[test]
    fn pascal_case_empty_and_trailing_underscore() {
        assert_eq!(pascal_case(""), "");
        assert_eq!(pascal_case("a_"), "A");
    }
}