fidius-wit 0.3.0

WIT generation for the Fidius WASM backend: maps Rust interface types (incl. #[derive(WitType)] structs/enums) to WIT records/variants, and emits the generated↔author conversions. Shared by fidius-macro, the build helper, and the CLI.
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
// Copyright 2026 Colliery, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

//! WIT generation for the Fidius WASM backend (FIDIUS-I-0023).
//!
//! Maps the Rust types in a `#[plugin_interface]` to WIT, per
//! `docs/explanation/wasm-component-abi.md`. Primitives, `String`, `Vec<T>`,
//! `Option<T>`, and `Result<T, PluginError>` map directly; user types that
//! carry `#[derive(WitType)]` map to WIT `record`s (structs) and `variant`s
//! (enums), referenced by their kebab-case name.
//!
//! This is a plain library (not a proc-macro), so `fidius-macro`, the `build.rs`
//! helper, and the `fidius wit` CLI can all share one implementation.

use std::collections::BTreeSet;

use syn::{Fields, GenericArgument, ItemEnum, ItemStruct, PathArguments, Type};

mod generate;
pub use generate::{contains_user_type, conv_expr, generate, generate_from_path, Generated};

/// Convert a Rust identifier (CamelCase or snake_case) to kebab-case, the WIT
/// naming convention. `BytePipe` → `byte-pipe`, `echo_bytes` → `echo-bytes`.
pub fn to_kebab_case(s: &str) -> String {
    let mut out = String::new();
    for (i, ch) in s.chars().enumerate() {
        if ch == '_' {
            out.push('-');
        } else if ch.is_uppercase() {
            if i != 0 {
                out.push('-');
            }
            out.extend(ch.to_lowercase());
        } else {
            out.push(ch);
        }
    }
    out
}

/// Extract the `T` from `Result<T, _>`, if `ty` is a `Result`.
pub fn result_ok_type(ty: &Type) -> Option<&Type> {
    if let Type::Path(p) = ty {
        if let Some(seg) = p.path.segments.last() {
            if seg.ident == "Result" {
                return first_generic(seg);
            }
        }
    }
    None
}

/// One method projected to WIT (already-mapped strings).
pub struct WitMethod {
    /// kebab-case export name.
    pub name: String,
    /// `(param-name, wit-type)` pairs, in order.
    pub params: Vec<(String, String)>,
    /// WIT return type, or `None` for no return.
    pub ret: Option<String>,
}

/// Map a Rust argument/return type to its WIT spelling, where `known` holds the
/// Rust identifiers of `#[derive(WitType)]` user types (mapped to their
/// kebab-case record/variant name). Returns `Err(msg)` for unsupported types.
pub fn wit_type_with(ty: &Type, known: &BTreeSet<String>) -> Result<String, String> {
    match ty {
        // Peel references: `&str` → string, `&[u8]` → list<u8>, `&T` → wit(T).
        Type::Reference(r) => {
            if let Type::Path(p) = r.elem.as_ref() {
                if path_is(p, "str") {
                    return Ok("string".to_string());
                }
            }
            if let Type::Slice(s) = r.elem.as_ref() {
                let inner = wit_type_with(&s.elem, known)?;
                return Ok(format!("list<{inner}>"));
            }
            wit_type_with(&r.elem, known)
        }
        Type::Path(p) => {
            let seg = p
                .path
                .segments
                .last()
                .ok_or_else(|| "empty type path".to_string())?;
            let ident = seg.ident.to_string();
            match ident.as_str() {
                "bool" => Ok("bool".into()),
                "i8" => Ok("s8".into()),
                "i16" => Ok("s16".into()),
                "i32" => Ok("s32".into()),
                "i64" => Ok("s64".into()),
                "u8" => Ok("u8".into()),
                "u16" => Ok("u16".into()),
                "u32" => Ok("u32".into()),
                "u64" => Ok("u64".into()),
                "f32" => Ok("f32".into()),
                "f64" => Ok("f64".into()),
                "char" => Ok("char".into()),
                "String" => Ok("string".into()),
                "Vec" => {
                    let inner = single_generic(seg, "Vec")?;
                    Ok(format!("list<{}>", wit_type_with(inner, known)?))
                }
                "Option" => {
                    let inner = single_generic(seg, "Option")?;
                    Ok(format!("option<{}>", wit_type_with(inner, known)?))
                }
                // A user type with `#[derive(WitType)]` → its kebab record/variant name.
                other if known.contains(other) => Ok(to_kebab_case(other)),
                other => Err(format!(
                    "type `{other}` is not supported in a WASM fidius interface \
                     (supported: bool, i8..i64, u8..u64, f32/f64, char, String, Vec<T>, \
                     Option<T>, Result<T, PluginError>, and #[derive(WitType)] structs/enums)"
                )),
            }
        }
        Type::Tuple(t) if t.elems.is_empty() => {
            Err("unit `()` is not a valid argument type".to_string())
        }
        _ => Err("unsupported type in a WASM fidius interface".to_string()),
    }
}

/// Primitive/std-only mapping (no user types) — the form `fidius-macro` uses for
/// the descriptor/method tables.
pub fn rust_type_to_wit(ty: &Type) -> Result<String, String> {
    wit_type_with(ty, &BTreeSet::new())
}

/// Map a method's return type to an optional WIT return, with user types in
/// `known`. `Result<T, PluginError>` → `result<T, plugin-error>` (or
/// `result<_, plugin-error>` for `T = ()`); `()`/none → no return.
pub fn return_to_wit_with(
    ret: Option<&Type>,
    known: &BTreeSet<String>,
) -> Result<Option<String>, String> {
    let Some(ty) = ret else { return Ok(None) };
    if is_unit(ty) {
        return Ok(None);
    }
    if let Type::Path(p) = ty {
        if let Some(seg) = p.path.segments.last() {
            if seg.ident == "Result" {
                let ok = first_generic(seg).ok_or_else(|| "Result needs type args".to_string())?;
                let ok_wit = if is_unit(ok) {
                    "_".to_string()
                } else {
                    wit_type_with(ok, known)?
                };
                return Ok(Some(format!("result<{ok_wit}, plugin-error>")));
            }
        }
    }
    Ok(Some(wit_type_with(ty, known)?))
}

/// Primitive/std-only return mapping (no user types).
pub fn return_to_wit(ret: Option<&Type>) -> Result<Option<String>, String> {
    return_to_wit_with(ret, &BTreeSet::new())
}

/// Render a `record <name> { ... }` WIT block from a Rust struct (named fields
/// only). Field types are mapped with `known` so they may reference other user
/// types.
pub fn struct_to_record(item: &ItemStruct, known: &BTreeSet<String>) -> Result<String, String> {
    let name = to_kebab_case(&item.ident.to_string());
    let Fields::Named(fields) = &item.fields else {
        return Err(format!(
            "WitType struct `{}` must have named fields (tuple/unit structs are not supported)",
            item.ident
        ));
    };
    let mut out = format!("    record {name} {{\n");
    for f in &fields.named {
        let fname = to_kebab_case(&f.ident.as_ref().unwrap().to_string());
        let fty = wit_type_with(&f.ty, known)
            .map_err(|e| format!("field `{}` of `{}`: {e}", fname, item.ident))?;
        out.push_str(&format!("        {fname}: {fty},\n"));
    }
    out.push_str("    }\n");
    Ok(out)
}

/// Render a Rust enum to WIT: a `variant <name> { ... }` plus any **synthetic
/// records** for struct-shaped cases. Returns `(synthetic_records, variant)`.
///
/// Case shapes: unit → `case`; single tuple field → `case(type)`; named fields
/// (`Case { .. }`) → a synthesized `record <enum>-<case>` and `case(<enum>-<case>)`.
/// A multi-field *tuple* case is rejected: WIT cases take one payload, and a
/// multi-field tuple serializes as a sequence (not a record) via serde, so it
/// can't round-trip — use a struct variant `Case { .. }` instead.
pub fn enum_to_wit(
    item: &ItemEnum,
    known: &BTreeSet<String>,
) -> Result<(Vec<String>, String), String> {
    let ename = item.ident.to_string();
    let name = to_kebab_case(&ename);
    let mut records = Vec::new();
    let mut out = format!("    variant {name} {{\n");
    for v in &item.variants {
        let case = to_kebab_case(&v.ident.to_string());
        match &v.fields {
            Fields::Unit => out.push_str(&format!("        {case},\n")),
            Fields::Unnamed(u) if u.unnamed.len() == 1 => {
                let payload = wit_type_with(&u.unnamed[0].ty, known)
                    .map_err(|e| format!("variant `{ename}::{}`: {e}", v.ident))?;
                out.push_str(&format!("        {case}({payload}),\n"));
            }
            Fields::Named(f) => {
                // Synthesize `record <enum>-<case> { .. }` for the case payload.
                let rec_name = format!("{name}-{case}");
                let mut rec = format!("    record {rec_name} {{\n");
                for fl in &f.named {
                    let fname = to_kebab_case(&fl.ident.as_ref().unwrap().to_string());
                    let fty = wit_type_with(&fl.ty, known)
                        .map_err(|e| format!("field `{fname}` of `{ename}::{}`: {e}", v.ident))?;
                    rec.push_str(&format!("        {fname}: {fty},\n"));
                }
                rec.push_str("    }\n");
                records.push(rec);
                out.push_str(&format!("        {case}({rec_name}),\n"));
            }
            Fields::Unnamed(_) => {
                return Err(format!(
                    "WitType enum `{ename}` variant `{}` has multiple tuple fields; \
                     a WIT variant case takes one payload — use a struct variant \
                     `{} {{ .. }}` (or a single field)",
                    v.ident, v.ident
                ));
            }
        }
    }
    out.push_str("    }\n");
    Ok((records, out))
}

/// Render a complete `.wit` document: package + interface (the `plugin-error`
/// record, any user `type_defs` (records/variants), the funcs, and the
/// `fidius-interface-hash` carrier) + the `<iface>-plugin` world. `type_defs`
/// are pre-rendered (see [`struct_to_record`] / [`enum_to_variant`]).
pub fn render_wit_full(iface_kebab: &str, type_defs: &[String], methods: &[WitMethod]) -> String {
    let mut s = String::new();
    s.push_str(&format!("package fidius:{iface_kebab}@0.1.0;\n\n"));
    s.push_str(&format!("interface {iface_kebab} {{\n"));
    s.push_str("    record plugin-error {\n");
    s.push_str("        code: string,\n");
    s.push_str("        message: string,\n");
    s.push_str("        details: option<string>,\n");
    s.push_str("    }\n");
    for def in type_defs {
        s.push_str(def);
    }
    for m in methods {
        let params = m
            .params
            .iter()
            .map(|(n, t)| format!("{n}: {t}"))
            .collect::<Vec<_>>()
            .join(", ");
        match &m.ret {
            Some(r) => s.push_str(&format!("    {}: func({params}) -> {r};\n", m.name)),
            None => s.push_str(&format!("    {}: func({params});\n", m.name)),
        }
    }
    s.push_str("    fidius-interface-hash: func() -> u64;\n");
    s.push_str("}\n\n");
    s.push_str(&format!(
        "world {iface_kebab}-plugin {{\n    export {iface_kebab};\n}}\n"
    ));
    s
}

/// Convenience: render a WIT document with no user type defs (the primitives-only
/// form `fidius-macro` emits inline today).
pub fn render_wit(iface_kebab: &str, methods: &[WitMethod]) -> String {
    render_wit_full(iface_kebab, &[], methods)
}

// ── helpers ─────────────────────────────────────────────────────────────────

fn is_unit(ty: &Type) -> bool {
    matches!(ty, Type::Tuple(t) if t.elems.is_empty())
}

fn path_is(p: &syn::TypePath, name: &str) -> bool {
    p.path
        .segments
        .last()
        .map(|s| s.ident == name)
        .unwrap_or(false)
}

fn single_generic<'a>(seg: &'a syn::PathSegment, what: &str) -> Result<&'a Type, String> {
    first_generic(seg).ok_or_else(|| format!("`{what}` needs one type argument"))
}

fn first_generic(seg: &syn::PathSegment) -> Option<&Type> {
    if let PathArguments::AngleBracketed(ab) = &seg.arguments {
        for a in &ab.args {
            if let GenericArgument::Type(t) = a {
                return Some(t);
            }
        }
    }
    None
}

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

    fn known(names: &[&str]) -> BTreeSet<String> {
        names.iter().map(|s| s.to_string()).collect()
    }
    fn wit(s: &str) -> String {
        rust_type_to_wit(&syn::parse_str::<Type>(s).unwrap()).unwrap()
    }

    #[test]
    fn primitives_strings_containers() {
        assert_eq!(wit("i64"), "s64");
        assert_eq!(wit("u32"), "u32");
        assert_eq!(wit("String"), "string");
        assert_eq!(wit("& str"), "string");
        assert_eq!(wit("Vec<u8>"), "list<u8>");
        assert_eq!(wit("Option<i32>"), "option<s32>");
        assert_eq!(wit("&[u8]"), "list<u8>");
    }

    #[test]
    fn returns() {
        let ret = |s: &str| return_to_wit(Some(&syn::parse_str::<Type>(s).unwrap())).unwrap();
        assert_eq!(ret("()"), None);
        assert_eq!(
            ret("Result<i64, PluginError>"),
            Some("result<s64, plugin-error>".into())
        );
        assert_eq!(
            ret("Result<(), PluginError>"),
            Some("result<_, plugin-error>".into())
        );
    }

    #[test]
    fn user_types_need_the_known_set() {
        let ty: Type = syn::parse_str("Point").unwrap();
        assert!(rust_type_to_wit(&ty).is_err()); // unknown without the set
        let k = known(&["Point"]);
        assert_eq!(wit_type_with(&ty, &k).unwrap(), "point");
        // nested in containers
        let v: Type = syn::parse_str("Vec<Point>").unwrap();
        assert_eq!(wit_type_with(&v, &k).unwrap(), "list<point>");
        let o: Type = syn::parse_str("Option<BytePipe>").unwrap();
        assert_eq!(
            wit_type_with(&o, &known(&["BytePipe"])).unwrap(),
            "option<byte-pipe>"
        );
    }

    #[test]
    fn struct_renders_to_record() {
        let item: ItemStruct = syn::parse_str("struct Point { x: i32, y_pos: u64 }").unwrap();
        let rec = struct_to_record(&item, &BTreeSet::new()).unwrap();
        assert!(rec.contains("record point {"));
        assert!(rec.contains("x: s32,"));
        assert!(rec.contains("y-pos: u64,"));
    }

    #[test]
    fn struct_with_nested_user_type() {
        let item: ItemStruct = syn::parse_str("struct Line { from: Point, to: Point }").unwrap();
        let rec = struct_to_record(&item, &known(&["Point"])).unwrap();
        assert!(rec.contains("from: point,"));
        assert!(rec.contains("to: point,"));
    }

    #[test]
    fn enum_renders_to_variant() {
        let item: ItemEnum =
            syn::parse_str("enum Shape { Circle(u32), Rect(Point), Dot }").unwrap();
        let (records, var) = enum_to_wit(&item, &known(&["Point"])).unwrap();
        assert!(records.is_empty());
        assert!(var.contains("variant shape {"));
        assert!(var.contains("circle(u32),"));
        assert!(var.contains("rect(point),"));
        assert!(var.contains("dot,"));
    }

    #[test]
    fn struct_variant_synthesizes_a_record() {
        let item: ItemEnum = syn::parse_str("enum Shape { Rect { w: u32, h: u32 }, Dot }").unwrap();
        let (records, var) = enum_to_wit(&item, &BTreeSet::new()).unwrap();
        assert_eq!(records.len(), 1);
        assert!(records[0].contains("record shape-rect {"));
        assert!(records[0].contains("w: u32,"));
        assert!(records[0].contains("h: u32,"));
        assert!(var.contains("rect(shape-rect),"));
        assert!(var.contains("dot,"));
    }

    #[test]
    fn multifield_tuple_variant_is_rejected() {
        let item: ItemEnum = syn::parse_str("enum E { Pair(u32, u32) }").unwrap();
        assert!(enum_to_wit(&item, &BTreeSet::new()).is_err());
    }

    #[test]
    fn full_document_places_type_defs_before_funcs() {
        let recs = vec![struct_to_record(
            &syn::parse_str("struct Point { x: i32, y: i32 }").unwrap(),
            &BTreeSet::new(),
        )
        .unwrap()];
        let methods = vec![WitMethod {
            name: "midpoint".into(),
            params: vec![("a".into(), "point".into()), ("b".into(), "point".into())],
            ret: Some("point".into()),
        }];
        let doc = render_wit_full("geo", &recs, &methods);
        assert!(doc.contains("package fidius:geo@0.1.0;"));
        let rec_at = doc.find("record point {").unwrap();
        let fn_at = doc.find("midpoint: func").unwrap();
        assert!(
            rec_at < fn_at,
            "records must precede funcs in the interface"
        );
        assert!(doc.contains("midpoint: func(a: point, b: point) -> point;"));
        assert!(doc.contains("fidius-interface-hash: func() -> u64;"));
        assert!(doc.contains("world geo-plugin {"));
    }
}