weaveffi-model 0.24.0

IR, parsers, Rust extractor, validation, and the resolved binding model for WeaveFFI API definitions
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
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
//! Extract a WeaveFFI [`Api`] from annotated Rust source.
//!
//! This module is the single Rust-to-IR bridge shared by two callers:
//!
//! * the `#[weaveffi::module]` proc-macro, which lowers a module to its IR to
//!   build the C ABI scaffolding at compile time; and
//! * the `weaveffi` CLI, which reads the same annotated source to drive
//!   `generate`/`extract`.
//!
//! Because both paths call into the *same* extraction, the IDL the CLI emits
//! and the symbols the macro produces cannot drift: they are two views of one
//! parse. Annotated Rust is therefore the single source of truth.
//!
//! # The annotation scheme
//!
//! A `#[weaveffi::module]` on an inline `mod` marks an exported namespace.
//! Inside it:
//!
//! * `#[weaveffi::export]` on a `fn` exports a function. An `async fn` lowers to
//!   an asynchronous symbol; a `fn -> Result<T, E>` is fallible (`throws` in
//!   the IDL; the return type is `T` and errors are reported through the ABI's
//!   `out_err`).
//! * `#[weaveffi::interface]` on a `struct` declares an interface (opaque,
//!   reference-counted object type). Its `impl` block's `pub fn`s become the
//!   interface's members: an associated function returning `Self` (or
//!   `Arc<Self>`) is a constructor, a `&self` or `self: Arc<Self>` function is
//!   a method, and any other associated function is a static.
//! * `#[weaveffi::record]` on a `struct` declares a by-value record.
//! * `#[weaveffi::enumeration]` on a `#[repr(i32)]` `enum` declares a C-style
//!   enum; an enum with data-carrying variants declares a rich enum.
//! * `#[weaveffi::error]` on an enum declares the module's error domain.
//! * `#[weaveffi::callback_interface]` on a `trait` declares a callback
//!   interface: a method set the consumer implements. Producers accept one as
//!   `Arc<dyn Trait>`.
//!
//! The type mapping mirrors the IDL: `String` (or `&str`) is a string, `Vec<u8>`
//! (or `&[u8]`) is a byte buffer, every integer primitive (including `u64`) is
//! the matching scalar, `Vec<T>`, `Option<T>`, and `HashMap`/`BTreeMap` are the
//! list, optional, and map types, `weaveffi::Iter<T>` is an `iter<T>` return,
//! `Arc<T>` names the interface `T` (an object reference), `Arc<dyn Trait>`
//! names the callback interface `Trait`, and any other named path is a record,
//! enum, or interface resolved later.

use crate::ir::{
    Api, CallbackInterfaceDef, EnumDef, EnumVariant, ErrorCode, ErrorDomain, Function,
    InterfaceDef, Module, Param, StructDef, StructField, TypeRef, CURRENT_SCHEMA_VERSION,
};
use syn::spanned::Spanned;

/// Match a WeaveFFI marker attribute by its final path segment.
///
/// Accepts both the namespaced form (`#[weaveffi::record]`) and the bare form
/// (`#[record]`), so the macro's stripped re-emit and a hand-written attribute
/// resolve identically.
fn is_marker(attr: &syn::Attribute, name: &str) -> bool {
    attr.path().segments.last().is_some_and(|s| s.ident == name)
}

/// Whether any attribute in `attrs` is the WeaveFFI marker `name`.
pub fn has_marker(attrs: &[syn::Attribute], name: &str) -> bool {
    attrs.iter().any(|a| is_marker(a, name))
}

fn has_repr_i32(attrs: &[syn::Attribute]) -> bool {
    attrs.iter().any(|a| {
        a.path().is_ident("repr") && a.parse_args::<syn::Ident>().is_ok_and(|id| id == "i32")
    })
}

/// Parse `#[deprecated(since = "...", note = "...")]` into `(since, note)`.
fn parse_deprecated(attrs: &[syn::Attribute]) -> (Option<String>, Option<String>) {
    let Some(attr) = attrs.iter().find(|a| a.path().is_ident("deprecated")) else {
        return (None, None);
    };
    let mut since = None;
    let mut note = None;
    if matches!(attr.meta, syn::Meta::Path(_)) {
        return (None, Some("deprecated".to_string()));
    }
    let _ = attr.parse_nested_meta(|meta| {
        let Some(ident) = meta.path.get_ident() else {
            return Ok(());
        };
        let value = meta.value()?;
        let lit: syn::LitStr = value.parse()?;
        match ident.to_string().as_str() {
            "since" => since = Some(lit.value()),
            "note" => note = Some(lit.value()),
            _ => {}
        }
        Ok(())
    });
    if note.is_none() && since.is_none() {
        note = Some("deprecated".to_string());
    }
    (since, note)
}

fn extract_doc(attrs: &[syn::Attribute]) -> Option<String> {
    let lines: Vec<String> = attrs
        .iter()
        .filter_map(|attr| {
            let syn::Meta::NameValue(nv) = &attr.meta else {
                return None;
            };
            if !nv.path.is_ident("doc") {
                return None;
            }
            let syn::Expr::Lit(syn::ExprLit {
                lit: syn::Lit::Str(s),
                ..
            }) = &nv.value
            else {
                return None;
            };
            let val = s.value();
            Some(match val.strip_prefix(' ') {
                Some(stripped) => stripped.to_string(),
                None => val,
            })
        })
        .collect();
    (!lines.is_empty()).then(|| lines.join("\n"))
}

fn is_ident(ty: &syn::Type, name: &str) -> bool {
    matches!(ty, syn::Type::Path(p) if p.path.is_ident(name))
}

fn single_generic_arg(seg: &syn::PathSegment) -> syn::Result<&syn::Type> {
    let syn::PathArguments::AngleBracketed(args) = &seg.arguments else {
        return Err(syn::Error::new(seg.span(), "expected generic arguments"));
    };
    if args.args.len() != 1 {
        return Err(syn::Error::new(
            seg.span(),
            "expected exactly 1 generic argument",
        ));
    }
    let syn::GenericArgument::Type(ty) = &args.args[0] else {
        return Err(syn::Error::new(seg.span(), "expected a type argument"));
    };
    Ok(ty)
}

fn two_generic_args(seg: &syn::PathSegment) -> syn::Result<(&syn::Type, &syn::Type)> {
    let syn::PathArguments::AngleBracketed(args) = &seg.arguments else {
        return Err(syn::Error::new(seg.span(), "expected generic arguments"));
    };
    if args.args.len() != 2 {
        return Err(syn::Error::new(
            seg.span(),
            "expected exactly 2 generic arguments",
        ));
    }
    let syn::GenericArgument::Type(k) = &args.args[0] else {
        return Err(syn::Error::new(seg.span(), "expected a key type argument"));
    };
    let syn::GenericArgument::Type(v) = &args.args[1] else {
        return Err(syn::Error::new(
            seg.span(),
            "expected a value type argument",
        ));
    };
    Ok((k, v))
}

fn type_path_ident(ty: &syn::Type) -> Option<String> {
    let syn::Type::Path(p) = ty else { return None };
    p.path.segments.last().map(|s| s.ident.to_string())
}

/// The bare name of the single trait a `dyn Trait` object names, ignoring
/// auto-trait and lifetime bounds (`dyn Listener + Send + Sync + 'static`).
fn trait_object_name(ty: &syn::Type) -> Option<String> {
    let syn::Type::TraitObject(obj) = ty else {
        return None;
    };
    let mut names = obj.bounds.iter().filter_map(|b| match b {
        syn::TypeParamBound::Trait(t) => {
            let name = t.path.segments.last()?.ident.to_string();
            (!matches!(name.as_str(), "Send" | "Sync" | "Unpin")).then_some(name)
        }
        _ => None,
    });
    let first = names.next()?;
    names.next().is_none().then_some(first)
}

/// Peel `Arc<T>` to its `T`, returning any other type unchanged.
///
/// `Arc` is how a producer spells an interface object it holds or returns
/// (`Arc<Store>`, `Arc<Self>`) and a callback interface it accepts
/// (`Arc<dyn Listener>`); the IR names the pointee type in both cases.
pub fn peel_arc(ty: &syn::Type) -> &syn::Type {
    if let syn::Type::Path(p) = ty {
        if let Some(seg) = p.path.segments.last() {
            if seg.ident == "Arc" {
                if let Ok(inner) = single_generic_arg(seg) {
                    return inner;
                }
            }
        }
    }
    ty
}

/// Whether a parameter's type is the reserved `weaveffi::CancelToken`.
///
/// A `#[weaveffi::cancellable]` `async fn` accepts a `CancelToken` as its final
/// parameter, but the token is part of the async *calling convention* (the
/// launcher's `cancel_token` slot), not the function's logical signature, so it
/// is filtered out of the extracted parameter list. Matching on the final path
/// segment accepts both the bare and `weaveffi::`-qualified spellings.
fn is_cancel_token(ty: &syn::Type) -> bool {
    type_path_ident(ty).as_deref() == Some("CancelToken")
}

/// Map a Rust [`syn::Type`] onto the WeaveFFI [`TypeRef`] it represents.
///
/// This is the canonical mapping every caller shares. Notable conventions:
///
/// * `String` and `&str` are both the `string` type; `Vec<u8>` and `&[u8]`
///   are both `bytes`. A reference is a producer-side calling convention (the
///   thunk lifts an owned value and lends it), not an IDL distinction.
/// * every integer primitive maps to its matching scalar (`u64` included).
/// * `Vec<T>` and `&[T]` are lists, `Option<T>` is an optional, and
///   `HashMap`/`BTreeMap` are maps.
/// * `weaveffi::Iter<T>` is an `iter<T>` return.
/// * `Arc<T>` and `&T` name the interface `T`; `Arc<dyn Trait>` names the
///   callback interface `Trait`. Any other named path is a record, enum,
///   interface, or callback interface reference resolved by the validator.
///
/// # Errors
///
/// Returns a spanned error for type syntax WeaveFFI cannot express across the
/// FFI boundary (raw pointers, `Box<dyn Trait>`, tuples, a generic with the
/// wrong arity, and so on).
pub fn type_ref_from_syn(ty: &syn::Type) -> syn::Result<TypeRef> {
    match ty {
        syn::Type::Reference(r) => {
            if let syn::Type::Path(p) = r.elem.as_ref() {
                if p.path.is_ident("str") {
                    return Ok(TypeRef::StringUtf8);
                }
            }
            if let syn::Type::Slice(slice) = r.elem.as_ref() {
                if is_ident(&slice.elem, "u8") {
                    return Ok(TypeRef::Bytes);
                }
                return Ok(TypeRef::List(Box::new(type_ref_from_syn(&slice.elem)?)));
            }
            type_ref_from_syn(&r.elem)
        }
        syn::Type::Ptr(_) => Err(syn::Error::new(
            ty.span(),
            "weaveffi: raw pointers cannot cross the FFI boundary; declare the pointee as a \
             #[weaveffi::interface] and pass it as `&T` or `Arc<T>`",
        )),
        syn::Type::TraitObject(_) => {
            let name = trait_object_name(ty).ok_or_else(|| {
                syn::Error::new(
                    ty.span(),
                    "weaveffi: a trait object must name exactly one #[weaveffi::callback_interface] \
                     trait (plus optional `Send`/`Sync` bounds)",
                )
            })?;
            Ok(TypeRef::Named(name))
        }
        syn::Type::Path(type_path) => {
            let seg = type_path
                .path
                .segments
                .last()
                .ok_or_else(|| syn::Error::new(ty.span(), "empty type path"))?;
            let ident = seg.ident.to_string();
            match ident.as_str() {
                "i8" => Ok(TypeRef::I8),
                "i16" => Ok(TypeRef::I16),
                "i32" => Ok(TypeRef::I32),
                "i64" => Ok(TypeRef::I64),
                "u8" => Ok(TypeRef::U8),
                "u16" => Ok(TypeRef::U16),
                "u32" => Ok(TypeRef::U32),
                "f32" => Ok(TypeRef::F32),
                "f64" => Ok(TypeRef::F64),
                "bool" => Ok(TypeRef::Bool),
                "String" => Ok(TypeRef::StringUtf8),
                "u64" => Ok(TypeRef::U64),
                // `Arc<T>` is a reference to the interface object `T`, and
                // `Arc<dyn Trait>` a consumer-implemented callback interface;
                // the IR names the pointee in both cases.
                "Arc" => type_ref_from_syn(single_generic_arg(seg)?),
                "Box" | "Rc" => Err(syn::Error::new(
                    ty.span(),
                    format!(
                        "weaveffi: `{ident}` cannot cross the FFI boundary; objects and callback \
                         interfaces are shared, so spell them as `Arc<T>` / `Arc<dyn Trait>`"
                    ),
                )),
                "Vec" => {
                    let inner = single_generic_arg(seg)?;
                    if is_ident(inner, "u8") {
                        return Ok(TypeRef::Bytes);
                    }
                    Ok(TypeRef::List(Box::new(type_ref_from_syn(inner)?)))
                }
                "Option" => {
                    let inner = single_generic_arg(seg)?;
                    Ok(TypeRef::Optional(Box::new(type_ref_from_syn(inner)?)))
                }
                // `weaveffi::Iter<T>` is the producer spelling of an `iter<T>`
                // return: a lazily-pulled stream rather than a materialized list.
                "Iter" => {
                    let inner = single_generic_arg(seg)?;
                    Ok(TypeRef::Iterator(Box::new(type_ref_from_syn(inner)?)))
                }
                "HashMap" | "BTreeMap" => {
                    let (k, v) = two_generic_args(seg)?;
                    Ok(TypeRef::Map(
                        Box::new(type_ref_from_syn(k)?),
                        Box::new(type_ref_from_syn(v)?),
                    ))
                }
                other => Ok(TypeRef::Named(other.to_string())),
            }
        }
        _ => Err(syn::Error::new(ty.span(), "unsupported type syntax")),
    }
}

/// Peel `Result<T, E>` to its `T`, returning any other type unchanged. A
/// `Result` return marks the function `throws` in the IDL; the error type
/// itself carries no extra IR shape (it reports through the ABI's `out_err`).
pub fn peel_result(ty: &syn::Type) -> &syn::Type {
    if let syn::Type::Path(p) = ty {
        if let Some(seg) = p.path.segments.last() {
            if seg.ident == "Result" {
                if let syn::PathArguments::AngleBracketed(args) = &seg.arguments {
                    if let Some(syn::GenericArgument::Type(ok)) = args.args.first() {
                        return ok;
                    }
                }
            }
        }
    }
    ty
}

/// Whether a function's return type is a `Result<_, _>`, which marks the
/// function as `throws` in the IDL.
pub fn output_is_result(output: &syn::ReturnType) -> bool {
    let syn::ReturnType::Type(_, ty) = output else {
        return false;
    };
    matches!(ty.as_ref(), syn::Type::Path(p)
        if p.path.segments.last().is_some_and(|s| s.ident == "Result"))
}

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

/// Map a function's return type to its IDL return [`TypeRef`], peeling
/// `Result<T, E>` and treating `()` (and `Result<(), E>`) as no return.
///
/// # Errors
///
/// Propagates any error from [`type_ref_from_syn`] on the (peeled) return type.
pub fn return_type_from_syn(output: &syn::ReturnType) -> syn::Result<Option<TypeRef>> {
    match output {
        syn::ReturnType::Default => Ok(None),
        syn::ReturnType::Type(_, ty) => {
            let inner = peel_result(ty);
            if is_unit(inner) {
                Ok(None)
            } else {
                Ok(Some(type_ref_from_syn(inner)?))
            }
        }
    }
}

fn parse_discriminant(expr: &syn::Expr) -> syn::Result<i32> {
    match expr {
        syn::Expr::Lit(lit) => {
            let syn::Lit::Int(int_lit) = &lit.lit else {
                return Err(syn::Error::new(
                    expr.span(),
                    "expected an integer literal discriminant",
                ));
            };
            int_lit.base10_parse::<i32>()
        }
        syn::Expr::Unary(unary) if matches!(unary.op, syn::UnOp::Neg(_)) => {
            Ok(-parse_discriminant(&unary.expr)?)
        }
        _ => Err(syn::Error::new(
            expr.span(),
            "unsupported discriminant expression",
        )),
    }
}

fn extract_params(
    inputs: &syn::punctuated::Punctuated<syn::FnArg, syn::Token![,]>,
) -> syn::Result<Vec<Param>> {
    inputs
        .iter()
        .filter_map(|arg| match arg {
            syn::FnArg::Typed(pt) => Some(pt),
            syn::FnArg::Receiver(_) => None,
        })
        // The cancellation token is part of the async calling convention, not a
        // logical parameter, so it never appears in the IDL.
        .filter(|pt| !is_cancel_token(&pt.ty))
        .map(|pt| {
            let param_name = match pt.pat.as_ref() {
                syn::Pat::Ident(id) => id.ident.to_string(),
                _ => return Err(syn::Error::new(pt.span(), "unsupported parameter pattern")),
            };
            if matches!(pt.ty.as_ref(), syn::Type::Reference(r) if r.mutability.is_some()) {
                return Err(syn::Error::new(
                    pt.ty.span(),
                    "weaveffi: `&mut` parameters cannot cross the FFI boundary; take the value \
                     by `&T` or by value and return the updated result",
                ));
            }
            let ty = type_ref_from_syn(&pt.ty)?;
            // The proc macro path skips `validate_api` (see the macro crate),
            // so the one type shape lowering can't represent as a parameter
            // must be caught here with a span instead of panicking later.
            let mut has_iterator = false;
            ty.walk(&mut |t| has_iterator |= matches!(t, TypeRef::Iterator(_)));
            if has_iterator {
                return Err(syn::Error::new(
                    pt.ty.span(),
                    "weaveffi: `Iter<T>` is a pull handle the consumer drives and is only \
                     valid as a function's outermost return type, not as a parameter",
                ));
            }
            Ok(Param {
                name: param_name,
                ty,
                doc: extract_doc(&pt.attrs),
            })
        })
        .collect()
}

/// Map one `fn` signature (free function, interface member, or callback
/// method) to the IR [`Function`]. `returns` is supplied by the caller because
/// constructors and `Self` returns need interface-aware handling.
fn function_from_sig(
    sig: &syn::Signature,
    attrs: &[syn::Attribute],
    returns: Option<TypeRef>,
) -> syn::Result<Function> {
    if let Some(ret) = &returns {
        let nested_iterator = |t: &TypeRef| {
            let mut found = false;
            t.walk(&mut |inner| found |= matches!(inner, TypeRef::Iterator(_)));
            found
        };
        let invalid = match ret {
            TypeRef::Iterator(elem) => nested_iterator(elem),
            other => nested_iterator(other),
        };
        if invalid {
            let span = match &sig.output {
                syn::ReturnType::Type(_, ty) => ty.span(),
                other => other.span(),
            };
            return Err(syn::Error::new(
                span,
                "weaveffi: `Iter<T>` is only valid as the outermost return type; it can't be \
                 nested in an `Option`, `Vec`, map, or another `Iter`",
            ));
        }
    }
    Ok(Function {
        name: sig.ident.to_string(),
        params: extract_params(&sig.inputs)?,
        returns,
        doc: extract_doc(attrs),
        throws: output_is_result(&sig.output),
        r#async: sig.asyncness.is_some(),
        cancellable: has_marker(attrs, "cancellable"),
        deprecated: parse_deprecated(attrs).1,
    })
}

fn extract_function(item: &syn::ItemFn) -> syn::Result<Function> {
    let returns = return_type_from_syn(&item.sig.output)?;
    function_from_sig(&item.sig, &item.attrs, returns)
}

/// Whether the (peeled) return type names the interface itself (`Self`,
/// `Arc<Self>`, or the interface's own name), which classifies an associated
/// function as a constructor.
fn returns_self(output: &syn::ReturnType, iface: &str) -> bool {
    let syn::ReturnType::Type(_, ty) = output else {
        return false;
    };
    match type_path_ident(peel_arc(peel_result(ty))) {
        Some(name) => name == "Self" || name == iface,
        None => false,
    }
}

/// Extract one interface member from an `impl` block function.
///
/// The receiver decides the member kind at the call site (see
/// [`members_from_impl`]); this helper maps the signature. A `Self` (or
/// interface-named) return on a constructor is dropped: the IR leaves a
/// constructor's `return` empty because the instance is implicit.
fn extract_member(item: &syn::ImplItemFn, iface: &str, is_ctor: bool) -> syn::Result<Function> {
    let returns = if is_ctor {
        None
    } else {
        match return_type_from_syn(&item.sig.output)? {
            // `fn hand(&self) -> Arc<Self>` style returns name the interface.
            Some(TypeRef::Named(name)) if name == "Self" => Some(TypeRef::Named(iface.to_string())),
            other => other,
        }
    };
    function_from_sig(&item.sig, &item.attrs, returns)
}

/// Whether a method receiver is one WeaveFFI can lift: `&self` (a borrow for
/// the call) or `self: Arc<Self>` (a retained reference).
///
/// `&mut self` and by-value `self` are rejected: the object is shared across
/// the FFI boundary (and may be in use on other threads), so mutable state
/// needs interior mutability.
pub fn receiver_is_supported(recv: &syn::Receiver) -> bool {
    if recv.mutability.is_some() {
        return false;
    }
    if recv.reference.is_some() {
        return true;
    }
    recv.colon_token.is_some() && type_path_ident(&recv.ty).as_deref() == Some("Arc")
}

/// Classify and extract every `pub fn` of an interface's `impl` block into
/// the interface's constructors, methods, and statics.
///
/// * a function with a `&self` or `self: Arc<Self>` receiver is a **method**;
/// * an associated function returning `Self` or `Arc<Self>` (or the interface
///   type, optionally inside `Result`) is a **constructor**;
/// * any other associated function is a **static**.
///
/// Non-`pub` items are private helpers and stay unexported.
fn members_from_impl(item_impl: &syn::ItemImpl, iface: &mut InterfaceDef) -> syn::Result<()> {
    for impl_item in &item_impl.items {
        let syn::ImplItem::Fn(f) = impl_item else {
            continue;
        };
        if !matches!(f.vis, syn::Visibility::Public(_)) {
            continue;
        }
        match f.sig.receiver() {
            Some(recv) => {
                if !receiver_is_supported(recv) {
                    return Err(syn::Error::new(
                        recv.span(),
                        "weaveffi: interface methods must take `&self` or `self: Arc<Self>`; \
                         use interior mutability (Mutex, RwLock, atomics) for mutable state, \
                         because the object is shared across the FFI boundary",
                    ));
                }
                iface.methods.push(extract_member(f, &iface.name, false)?);
            }
            None if returns_self(&f.sig.output, &iface.name) => {
                iface
                    .constructors
                    .push(extract_member(f, &iface.name, true)?);
            }
            None => {
                iface.statics.push(extract_member(f, &iface.name, false)?);
            }
        }
    }
    Ok(())
}

/// Whether a return type is `Result<T, ForeignError>`: a callback method's
/// way of receiving the consumer's failure as a value. The `Result` is a
/// producer-side convention (the IDL method still returns `T` and doesn't
/// throw), so it's peeled without marking the method `throws`.
pub fn returns_foreign_result(output: &syn::ReturnType) -> bool {
    let syn::ReturnType::Type(_, ty) = output else {
        return false;
    };
    let syn::Type::Path(p) = ty.as_ref() else {
        return false;
    };
    let Some(seg) = p.path.segments.last() else {
        return false;
    };
    if seg.ident != "Result" {
        return false;
    }
    let syn::PathArguments::AngleBracketed(args) = &seg.arguments else {
        return false;
    };
    match args.args.iter().nth(1) {
        Some(syn::GenericArgument::Type(err)) => {
            type_path_ident(err).as_deref() == Some("ForeignError")
        }
        _ => false,
    }
}

/// Extract a `#[weaveffi::callback_interface]` trait into a
/// [`CallbackInterfaceDef`].
///
/// Every trait method is a callback method the consumer implements. Methods
/// must take `&self`. A method may return `Result<T, weaveffi::ForeignError>`
/// to receive the consumer's failure as a value; that maps to a plain `T`
/// return. The validator rejects `async`, other `Result` returns, and
/// non-direct return types, so those are mapped faithfully here and reported
/// with the IDL-level diagnostics.
fn extract_callback_interface(item: &syn::ItemTrait) -> syn::Result<CallbackInterfaceDef> {
    let mut methods = Vec::new();
    for trait_item in &item.items {
        let syn::TraitItem::Fn(f) = trait_item else {
            continue;
        };
        match f.sig.receiver() {
            Some(recv) if recv.reference.is_some() && recv.mutability.is_none() => {}
            Some(recv) => {
                return Err(syn::Error::new(
                    recv.span(),
                    "weaveffi: callback interface methods must take `&self`",
                ));
            }
            None => {
                return Err(syn::Error::new(
                    f.sig.span(),
                    "weaveffi: callback interface methods must take `&self` (associated \
                     functions can't be implemented by the consumer)",
                ));
            }
        }
        let returns = return_type_from_syn(&f.sig.output)?;
        let mut method = function_from_sig(&f.sig, &f.attrs, returns)?;
        if returns_foreign_result(&f.sig.output) {
            method.throws = false;
        }
        methods.push(method);
    }
    Ok(CallbackInterfaceDef {
        name: item.ident.to_string(),
        doc: extract_doc(&item.attrs),
        deprecated: parse_deprecated(&item.attrs).1,
        methods,
    })
}

fn extract_struct(item: &syn::ItemStruct) -> syn::Result<StructDef> {
    let name = item.ident.to_string();
    let fields = match &item.fields {
        syn::Fields::Named(named) => named
            .named
            .iter()
            .map(|f| {
                let field_name = f
                    .ident
                    .as_ref()
                    .ok_or_else(|| syn::Error::new(f.span(), "unnamed field in record"))?
                    .to_string();
                Ok(StructField {
                    name: field_name,
                    ty: type_ref_from_syn(&f.ty)?,
                    doc: extract_doc(&f.attrs),
                })
            })
            .collect::<syn::Result<_>>()?,
        _ => {
            return Err(syn::Error::new(
                item.span(),
                "only named fields are supported for #[weaveffi::record]",
            ))
        }
    };
    Ok(StructDef {
        name,
        doc: extract_doc(&item.attrs),
        deprecated: parse_deprecated(&item.attrs).1,
        fields,
    })
}

/// Extract the named fields of one rich (algebraic) enum variant into the IR's
/// [`StructField`] list, the same shape a record's fields take.
fn extract_variant_fields(fields: &syn::FieldsNamed) -> syn::Result<Vec<StructField>> {
    fields
        .named
        .iter()
        .map(|f| {
            let field_name = f
                .ident
                .as_ref()
                .ok_or_else(|| syn::Error::new(f.span(), "unnamed field in enum variant"))?
                .to_string();
            Ok(StructField {
                name: field_name,
                ty: type_ref_from_syn(&f.ty)?,
                doc: extract_doc(&f.attrs),
            })
        })
        .collect()
}

fn extract_enum(item: &syn::ItemEnum) -> syn::Result<EnumDef> {
    let name = item.ident.to_string();

    // A *rich* (algebraic) enum has at least one variant carrying data. Rust
    // forbids explicit discriminants on such an enum, so its tags are the
    // declaration-order positions (0, 1, 2, …) - exactly what the IDL records.
    // A *C-style* enum (every variant fieldless) keeps the stricter contract:
    // it must be `#[repr(i32)]` with an explicit discriminant on each variant.
    let is_rich = item
        .variants
        .iter()
        .any(|v| !matches!(v.fields, syn::Fields::Unit));

    if !is_rich && !has_repr_i32(&item.attrs) {
        return Err(syn::Error::new(
            item.span(),
            format!(
                "enum `{}` must have #[repr(i32)] to be a #[weaveffi::enumeration]",
                item.ident
            ),
        ));
    }

    let mut next_value: i32 = 0;
    let variants = item
        .variants
        .iter()
        .map(|v| {
            let value = match v.discriminant.as_ref() {
                Some((_, expr)) => parse_discriminant(expr)?,
                None if is_rich => next_value,
                None => {
                    return Err(syn::Error::new(
                        v.span(),
                        format!(
                            "enum `{name}` variant `{}` must have an explicit discriminant",
                            v.ident
                        ),
                    ))
                }
            };
            next_value = value.wrapping_add(1);

            let fields = match &v.fields {
                syn::Fields::Unit => vec![],
                syn::Fields::Named(named) => extract_variant_fields(named)?,
                syn::Fields::Unnamed(_) => {
                    return Err(syn::Error::new(
                        v.span(),
                        format!(
                            "enum `{name}` variant `{}`: tuple-style variants are not supported; \
                             use named fields",
                            v.ident
                        ),
                    ))
                }
            };

            Ok(EnumVariant {
                name: v.ident.to_string(),
                value,
                doc: extract_doc(&v.attrs),
                fields,
            })
        })
        .collect::<syn::Result<_>>()?;
    Ok(EnumDef {
        name,
        doc: extract_doc(&item.attrs),
        deprecated: parse_deprecated(&item.attrs).1,
        variants,
    })
}

/// Extract a `#[weaveffi::error]` enum into the module's [`ErrorDomain`].
///
/// Every variant needs an explicit integer discriminant: the code's stable
/// ABI value. A variant may be a unit variant or carry named fields, which
/// become the code's structured payload (serialized in the value-buffer
/// format alongside the `(code, message)` pair). Note that Rust requires a
/// primitive representation such as `#[repr(i32)]` on an enum that mixes
/// explicit discriminants with data-carrying variants. A variant's doc
/// comment becomes the code's default message (falling back to the variant
/// name). The enum's name is the domain name, and the macro generates a
/// matching `ErrorReport` implementation so `Err(KvError::...)` reports its
/// declared code and payload.
fn extract_error_domain(item: &syn::ItemEnum) -> syn::Result<ErrorDomain> {
    let codes = item
        .variants
        .iter()
        .map(|v| {
            let fields = match &v.fields {
                syn::Fields::Unit => vec![],
                syn::Fields::Named(named) => extract_variant_fields(named)?,
                syn::Fields::Unnamed(_) => {
                    return Err(syn::Error::new(
                        v.span(),
                        "weaveffi: #[weaveffi::error] payload variants must use named \
                         fields (tuple-style variants are not supported)",
                    ));
                }
            };
            let Some((_, expr)) = v.discriminant.as_ref() else {
                return Err(syn::Error::new(
                    v.span(),
                    format!(
                        "weaveffi: error variant `{}` must have an explicit discriminant \
                         (its stable ABI error code)",
                        v.ident
                    ),
                ));
            };
            let doc = extract_doc(&v.attrs);
            let message = doc
                .as_deref()
                .and_then(|d| d.lines().next())
                .unwrap_or(&v.ident.to_string())
                .to_string();
            Ok(ErrorCode {
                name: v.ident.to_string(),
                code: parse_discriminant(expr)?,
                message,
                doc,
                fields,
            })
        })
        .collect::<syn::Result<Vec<_>>>()?;
    Ok(ErrorDomain {
        name: item.ident.to_string(),
        codes,
    })
}

/// Extract a single [`Module`] from a `#[weaveffi::module]`-annotated `mod`.
///
/// Only items carrying a WeaveFFI marker are exported; everything else (private
/// helpers, `use` items, free functions without `#[weaveffi::export]`) is
/// ignored, so a module can freely mix exported surface and implementation.
///
/// # Errors
///
/// Returns a spanned error when an annotated item cannot be mapped to the IR
/// (an unsupported type, an enum without `#[repr(i32)]`, a callback interface
/// method without `&self`, and so on).
pub fn module_from_item_mod(item_mod: &syn::ItemMod) -> syn::Result<Module> {
    let name = item_mod.ident.to_string();
    let mut functions = Vec::new();
    let mut interfaces: Vec<InterfaceDef> = Vec::new();
    let mut structs = Vec::new();
    let mut enums = Vec::new();
    let mut callback_interfaces = Vec::new();
    let mut modules = Vec::new();
    let mut errors: Option<ErrorDomain> = None;

    if let Some((_, items)) = &item_mod.content {
        for item in items {
            match item {
                syn::Item::Trait(t) if has_marker(&t.attrs, "callback_interface") => {
                    callback_interfaces.push(extract_callback_interface(t)?);
                }
                syn::Item::Fn(f) if has_marker(&f.attrs, "export") => {
                    functions.push(extract_function(f)?);
                }
                syn::Item::Struct(s) if has_marker(&s.attrs, "interface") => {
                    interfaces.push(InterfaceDef {
                        name: s.ident.to_string(),
                        doc: extract_doc(&s.attrs),
                        deprecated: parse_deprecated(&s.attrs).1,
                        constructors: vec![],
                        methods: vec![],
                        statics: vec![],
                    });
                }
                syn::Item::Struct(s) if has_marker(&s.attrs, "record") => {
                    structs.push(extract_struct(s)?);
                }
                syn::Item::Enum(e) if has_marker(&e.attrs, "error") => {
                    if errors.is_some() {
                        return Err(syn::Error::new(
                            e.span(),
                            "weaveffi: a module may declare at most one #[weaveffi::error] \
                             domain",
                        ));
                    }
                    errors = Some(extract_error_domain(e)?);
                }
                syn::Item::Enum(e) if has_marker(&e.attrs, "enumeration") => {
                    enums.push(extract_enum(e)?);
                }
                syn::Item::Mod(m) if has_marker(&m.attrs, "module") && m.content.is_some() => {
                    modules.push(module_from_item_mod(m)?);
                }
                _ => {}
            }
        }

        // Second pass: attach `impl` block members to their interfaces. The
        // interface struct may be declared after its impl block, so members
        // are collected only once every interface name is known.
        for item in items {
            let syn::Item::Impl(item_impl) = item else {
                continue;
            };
            if item_impl.trait_.is_some() {
                continue;
            }
            let Some(self_name) = type_path_ident(&item_impl.self_ty) else {
                continue;
            };
            if let Some(iface) = interfaces.iter_mut().find(|i| i.name == self_name) {
                members_from_impl(item_impl, iface)?;
            }
        }
    }

    Ok(Module {
        name,
        doc: extract_doc(&item_mod.attrs),
        functions,
        interfaces,
        structs,
        enums,
        callback_interfaces,
        errors,
        modules,
    })
}

/// Extract a full [`Api`] from a parsed Rust [`syn::File`], collecting every
/// top-level `#[weaveffi::module]`.
///
/// # Errors
///
/// Propagates any error from [`module_from_item_mod`].
pub fn api_from_file(file: &syn::File) -> syn::Result<Api> {
    let mut modules = Vec::new();
    for item in &file.items {
        if let syn::Item::Mod(item_mod) = item {
            if has_marker(&item_mod.attrs, "module") && item_mod.content.is_some() {
                modules.push(module_from_item_mod(item_mod)?);
            }
        }
    }
    Ok(Api {
        version: CURRENT_SCHEMA_VERSION.to_string(),
        modules,
    })
}

/// Parse Rust source text and extract its [`Api`].
///
/// # Errors
///
/// Returns a (line/column aware) error when the source does not parse as Rust,
/// or when an annotated item cannot be mapped to the IR.
pub fn api_from_src(src: &str) -> syn::Result<Api> {
    let file = syn::parse_file(src)?;
    api_from_file(&file)
}

/// A convenience wrapper around [`api_from_src`] that renders any error to a
/// plain string, for non-macro callers (e.g. the CLI).
///
/// # Errors
///
/// Returns the formatted error message when extraction fails.
pub fn api_from_src_stringly(src: &str) -> Result<Api, String> {
    api_from_src(src).map_err(|e| e.to_string())
}

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

    fn one_module(src: &str) -> Module {
        let api = api_from_src(src).unwrap();
        assert_eq!(api.modules.len(), 1, "expected exactly one module");
        api.modules.into_iter().next().unwrap()
    }

    #[test]
    fn extracts_exported_function() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod math {
                #[weaveffi::export]
                pub fn add(a: i32, b: i32) -> i32 { a + b }
            }
        "#,
        );
        assert_eq!(m.name, "math");
        assert_eq!(m.functions.len(), 1);
        let f = &m.functions[0];
        assert_eq!(f.name, "add");
        assert_eq!(f.params[0].ty, TypeRef::I32);
        assert_eq!(f.returns, Some(TypeRef::I32));
        assert!(!f.r#async);
    }

    #[test]
    fn unmarked_module_is_ignored() {
        let api = api_from_src(
            r#"
            mod plain {
                #[weaveffi::export]
                pub fn add(a: i32) -> i32 { a }
            }
        "#,
        )
        .unwrap();
        assert!(api.modules.is_empty());
    }

    #[test]
    fn result_return_peels_to_ok_type() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::export]
                pub fn get(id: u64) -> Result<Contact, MyError> { todo!() }
            }
        "#,
        );
        assert_eq!(
            m.functions[0].returns,
            Some(TypeRef::Named("Contact".into()))
        );
        assert_eq!(m.functions[0].params[0].ty, TypeRef::U64);
    }

    #[test]
    fn result_unit_return_is_none() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::export]
                pub fn run() -> Result<(), MyError> { Ok(()) }
            }
        "#,
        );
        assert_eq!(m.functions[0].returns, None);
    }

    #[test]
    fn async_fn_is_async() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::export]
                pub async fn fetch(url: String) -> String { url }
            }
        "#,
        );
        assert!(m.functions[0].r#async);
    }

    #[test]
    fn record_and_enumeration() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod contacts {
                #[weaveffi::enumeration]
                #[repr(i32)]
                pub enum ContactType { Personal = 0, Work = 1 }

                #[weaveffi::record]
                pub struct Contact {
                    pub id: i64,
                    pub email: Option<String>,
                    pub kind: ContactType,
                }
            }
        "#,
        );
        assert_eq!(m.enums.len(), 1);
        assert_eq!(m.enums[0].variants.len(), 2);
        assert_eq!(m.structs.len(), 1);
        let s = &m.structs[0];
        assert_eq!(
            s.fields[1].ty,
            TypeRef::Optional(Box::new(TypeRef::StringUtf8))
        );
        assert_eq!(s.fields[2].ty, TypeRef::Named("ContactType".into()));
    }

    #[test]
    fn enumeration_requires_repr_i32() {
        let err = api_from_src(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::enumeration]
                pub enum Bad { A = 0 }
            }
        "#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("repr(i32)"));
    }

    #[test]
    fn references_and_arcs_name_the_pointee() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::export]
                pub fn open(name: &str, data: &[u8], tags: &[String]) -> Arc<Store> { todo!() }
                #[weaveffi::export]
                pub fn peek(store: &Store, other: Option<Arc<Store>>) -> Option<Arc<Store>> { None }
            }
        "#,
        );
        let open = &m.functions[0];
        assert_eq!(open.params[0].ty, TypeRef::StringUtf8);
        assert_eq!(open.params[1].ty, TypeRef::Bytes);
        assert_eq!(
            open.params[2].ty,
            TypeRef::List(Box::new(TypeRef::StringUtf8))
        );
        assert_eq!(open.returns, Some(TypeRef::Named("Store".into())));
        let peek = &m.functions[1];
        assert_eq!(peek.params[0].ty, TypeRef::Named("Store".into()));
        let opt_store = TypeRef::Optional(Box::new(TypeRef::Named("Store".into())));
        assert_eq!(peek.params[1].ty, opt_store);
        assert_eq!(peek.returns, Some(opt_store));
    }

    #[test]
    fn raw_pointers_and_boxes_are_rejected() {
        let err = api_from_src(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::export]
                pub fn close(token: *mut Token) {}
            }
        "#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("raw pointers"));
        let err = api_from_src(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::export]
                pub fn listen(l: Box<dyn Listener>) {}
            }
        "#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("Arc<dyn Trait>"));
        let err = api_from_src(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::export]
                pub fn bump(counter: &mut i32) {}
            }
        "#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("&mut"));
    }

    #[test]
    fn cancel_token_param_is_skipped() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::export]
                #[weaveffi::cancellable]
                pub async fn compact(store: Arc<Store>, cancel: weaveffi::CancelToken) -> i64 { 0 }
            }
        "#,
        );
        let f = &m.functions[0];
        assert!(f.r#async);
        assert!(f.cancellable);
        // Only `store` survives; the `CancelToken` is dropped from the IDL.
        assert_eq!(f.params.len(), 1);
        assert_eq!(f.params[0].name, "store");
        assert_eq!(f.params[0].ty, TypeRef::Named("Store".into()));
    }

    #[test]
    fn interface_members_and_receivers() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::interface]
                pub struct Store;
                impl Store {
                    pub fn open(path: String) -> Result<Arc<Self>, StoreError> { todo!() }
                    pub fn new() -> Self { Store }
                    pub fn len(&self) -> u64 { 0 }
                    pub fn share(self: Arc<Self>) -> Arc<Self> { self }
                    pub fn default_path() -> String { String::new() }
                    fn private(&self) {}
                }
            }
        "#,
        );
        let s = &m.interfaces[0];
        assert_eq!(s.constructors.len(), 2);
        assert!(s.constructors[0].throws);
        assert_eq!(s.constructors[0].returns, None);
        assert_eq!(s.methods.len(), 2);
        assert_eq!(s.methods[1].returns, Some(TypeRef::Named("Store".into())));
        assert_eq!(s.statics.len(), 1);
    }

    #[test]
    fn mut_self_receiver_is_rejected() {
        let err = api_from_src(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::interface]
                pub struct Store;
                impl Store {
                    pub fn bump(&mut self) {}
                }
            }
        "#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("&self"));
    }

    #[test]
    fn callback_interface_trait() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod m {
                /// Receives messages.
                #[weaveffi::callback_interface]
                pub trait Listener: Send + Sync {
                    fn on_message(&self, text: String, meta: Option<Meta>);
                    fn level(&self) -> i32;
                }
                #[weaveffi::export]
                pub fn subscribe(listener: Arc<dyn Listener>) {}
                #[weaveffi::export]
                pub fn subscribe_bounded(listener: Arc<dyn Listener + Send + Sync>) {}
            }
        "#,
        );
        assert_eq!(m.callback_interfaces.len(), 1);
        let cb = &m.callback_interfaces[0];
        assert_eq!(cb.name, "Listener");
        assert_eq!(cb.doc.as_deref(), Some("Receives messages."));
        assert_eq!(cb.methods.len(), 2);
        assert_eq!(cb.methods[0].params.len(), 2);
        assert_eq!(cb.methods[1].returns, Some(TypeRef::I32));
        assert_eq!(
            m.functions[0].params[0].ty,
            TypeRef::Named("Listener".into())
        );
        assert_eq!(
            m.functions[1].params[0].ty,
            TypeRef::Named("Listener".into())
        );
    }

    #[test]
    fn callback_methods_may_return_foreign_results() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::callback_interface]
                pub trait Listener: Send + Sync {
                    fn level(&self) -> Result<i32, weaveffi::ForeignError>;
                    fn ping(&self) -> Result<(), ForeignError>;
                    fn strict(&self) -> Result<i32, String>;
                }
            }
        "#,
        );
        let methods = &m.callback_interfaces[0].methods;
        assert_eq!(methods[0].returns, Some(TypeRef::I32));
        assert!(!methods[0].throws);
        assert_eq!(methods[1].returns, None);
        assert!(!methods[1].throws);
        assert!(methods[2].throws, "other error types still throw");
    }

    #[test]
    fn callback_interface_methods_need_self() {
        let err = api_from_src(
            r#"
            #[weaveffi::module]
            mod m {
                #[weaveffi::callback_interface]
                pub trait Listener {
                    fn make() -> i32;
                }
            }
        "#,
        )
        .unwrap_err();
        assert!(err.to_string().contains("&self"));
    }

    #[test]
    fn nested_modules() {
        let m = one_module(
            r#"
            #[weaveffi::module]
            mod outer {
                #[weaveffi::export]
                pub fn top() -> i32 { 0 }
                #[weaveffi::module]
                mod inner {
                    #[weaveffi::export]
                    pub fn deep(x: bool) -> bool { x }
                }
            }
        "#,
        );
        assert_eq!(m.modules.len(), 1);
        assert_eq!(m.modules[0].name, "inner");
        assert_eq!(m.modules[0].functions[0].name, "deep");
    }
}