oopsie 0.1.0-rc.20

Ergonomic, structured error handling: context selectors, traced errors, and rich colorized reports
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
#![cfg_attr(
    feature = "unstable-error-generic-member-access",
    feature(error_generic_member_access)
)]
#![allow(
    unused,
    clippy::all,
    reason = "derive-macro test fixtures intentionally trip style lints"
)]

use oopsie::{Contextual as _, Oopsie, ResultExt as _};
use std::error::Error as _;
use std::io;

// ---- Enum for source tests ----

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum MyError {
    WithSource { source: io::Error, info: String },

    Leaf { message: String },
}

// ---- Structs for source tests ----

#[derive(Debug, Oopsie)]
#[oopsie(suffix)]
struct StructWithSource {
    source: io::Error,
    detail: String,
}

#[derive(Debug, Oopsie)]
#[oopsie(suffix)]
struct StructNoSource {
    reason: String,
}

// ---- Source tests ----

#[test]
fn source_returns_some() {
    let io_err = io::Error::new(io::ErrorKind::BrokenPipe, "pipe broke");
    let err: MyError = WithSource { info: "ctx" }.build_error(io_err);
    let src = err.source();
    assert!(src.is_some());
    assert_eq!(src.unwrap().to_string(), "pipe broke");
}

#[test]
fn source_returns_none_leaf() {
    let err = Leaf { message: "oops" }.build();
    assert!(err.source().is_none());
}

#[test]
fn struct_source_some() {
    let io_err = io::Error::new(io::ErrorKind::AddrInUse, "in use");
    let err: StructWithSource = StructWithSourceOopsie { detail: "binding" }.build_error(io_err);
    let src = err.source();
    assert!(src.is_some());
    assert_eq!(src.unwrap().to_string(), "in use");
}

#[test]
fn struct_source_none() {
    let err = StructNoSourceOopsie {
        reason: "just because",
    }
    .build();
    assert!(err.source().is_none());
}

#[test]
fn error_is_send_sync() {
    fn assert_send_sync<T: Send + Sync>() {}
    assert_send_sync::<MyError>();
    assert_send_sync::<StructWithSource>();
    assert_send_sync::<StructNoSource>();
}

// ---- Unstable provide tests ----

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum ProvideError {
    #[oopsie("bt error")]
    #[oopsie(provide(ref, oopsie::Backtrace => bt.as_ref()))]
    WithBacktrace {
        msg: String,
        #[oopsie(capture)]
        bt: Box<oopsie::Backtrace>,
    },

    #[oopsie("help error")]
    #[oopsie(provide(::oopsie::HelpText => ::oopsie::HelpText::from_static("try rebooting")))]
    WithHelp { msg: String },

    #[oopsie("coded error")]
    #[oopsie(provide(::oopsie::ErrorCode => ::oopsie::ErrorCode::from("app::parse")))]
    WithCode { msg: String },

    // Regression: a variant-level `provide` expr that references a bare user
    // field must bind that field in the generated `provide()` match arm.
    #[oopsie("field help error")]
    #[oopsie(provide(::oopsie::HelpText => ::oopsie::HelpText::from(msg.clone())))]
    WithFieldHelp { msg: String },
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn provide_backtrace_ref() {
    let err = WithBacktrace { msg: "boom" }.build();
    let bt = core::error::request_ref::<oopsie::Backtrace>(&err);
    assert!(bt.is_some(), "should provide a Backtrace ref");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn provide_help_text() {
    let err = WithHelp { msg: "broken" }.build();
    let help = core::error::request_value::<oopsie::HelpText>(&err);
    assert!(help.is_some(), "should provide HelpText");
    assert_eq!(&*help.unwrap(), "try rebooting");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn provide_error_code() {
    let err = WithCode { msg: "bad input" }.build();
    let code = core::error::request_value::<oopsie::ErrorCode>(&err);
    assert!(code.is_some(), "should provide ErrorCode");
    assert_eq!(&*code.unwrap(), "app::parse");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn provide_help_text_from_user_field() {
    let err = WithFieldHelp {
        msg: "needs attention",
    }
    .build();
    let help = core::error::request_value::<oopsie::HelpText>(&err);
    assert!(
        help.is_some(),
        "should provide HelpText built from a user field"
    );
    assert_eq!(&*help.unwrap(), "needs attention");
}

// ---- Nested error propagation tests ----
//
// These test that backtrace, spantrace, help text, and error codes from inner
// errors are accessible through outer errors via the provide() chain.

/// Inner error that carries a backtrace and help text.
#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum InnerError {
    #[oopsie("inner: {detail}")]
    #[oopsie(provide(ref, oopsie::Backtrace => bt.as_ref()))]
    #[oopsie(provide(::oopsie::HelpText => ::oopsie::HelpText::from_static("fix the inner thing")))]
    #[oopsie(provide(::oopsie::ErrorCode => ::oopsie::ErrorCode::from("inner::code")))]
    Root {
        detail: String,
        #[oopsie(capture)]
        bt: Box<oopsie::Backtrace>,
    },
}

/// Mid-level error that wraps InnerError.
#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum MiddleError {
    #[oopsie("middle: {context}")]
    Wrapped { source: InnerError, context: String },
}

/// Outer error that wraps MiddleError.
#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum OuterError {
    #[oopsie("outer: {label}")]
    Top { source: MiddleError, label: String },
}

#[test]
fn nested_source_chain_two_levels() {
    let inner = Root {
        detail: "db failed",
    }
    .build();
    let middle: MiddleError = Wrapped { context: "query" }.build_error(inner);
    let outer: OuterError = Top { label: "request" }.build_error(middle);

    // Display
    assert_eq!(outer.to_string(), "outer: request");

    // source() chain: outer -> middle -> inner
    let mid = outer.source().expect("outer should have source");
    assert_eq!(mid.to_string(), "middle: query");

    let inn = mid.source().expect("middle should have source");
    assert_eq!(inn.to_string(), "inner: db failed");

    assert!(inn.source().is_none(), "inner is a leaf error");
}

#[test]
fn nested_source_chain_via_context() {
    // Build inner error, then wrap using ResultExt::context()
    let inner = Root { detail: "timeout" }.build();
    let result: Result<(), InnerError> = Err(inner);
    let middle: Result<(), MiddleError> = result.context(Wrapped {
        context: "connecting",
    });
    let result2: Result<(), OuterError> = middle.context(Top { label: "service" });

    let outer = result2.unwrap_err();
    assert_eq!(outer.to_string(), "outer: service");

    let mid = outer.source().unwrap();
    assert_eq!(mid.to_string(), "middle: connecting");

    let inn = mid.source().unwrap();
    assert_eq!(inn.to_string(), "inner: timeout");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn nested_backtrace_propagated_through_source_chain() {
    // Inner error has a backtrace. Wrap it two levels deep.
    let inner = Root { detail: "crash" }.build();
    let middle: MiddleError = Wrapped {
        context: "processing",
    }
    .build_error(inner);
    let outer: OuterError = Top { label: "handler" }.build_error(middle);

    // The backtrace should be accessible from the inner error directly
    let inner_ref = outer.source().unwrap().source().unwrap();
    let bt_direct = core::error::request_ref::<oopsie::Backtrace>(inner_ref);
    assert!(
        bt_direct.is_some(),
        "inner error should provide backtrace directly"
    );

    // The backtrace should also be accessible from the outer error,
    // because provide() forwards to source.provide() at each level
    let bt_outer = core::error::request_ref::<oopsie::Backtrace>(&outer);
    assert!(
        bt_outer.is_some(),
        "backtrace should propagate through source chain to outer"
    );
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn nested_help_text_propagated_through_source_chain() {
    let inner = Root { detail: "issue" }.build();
    let middle: MiddleError = Wrapped {
        context: "handling",
    }
    .build_error(inner);
    let outer: OuterError = Top { label: "api" }.build_error(middle);

    // HelpText from inner should be accessible from outer via provide chain
    let help = core::error::request_value::<oopsie::HelpText>(&outer);
    assert!(
        help.is_some(),
        "HelpText should propagate through source chain"
    );
    assert_eq!(&*help.unwrap(), "fix the inner thing");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn nested_error_code_propagated_through_source_chain() {
    let inner = Root {
        detail: "parse fail",
    }
    .build();
    let middle: MiddleError = Wrapped {
        context: "decoding",
    }
    .build_error(inner);
    let outer: OuterError = Top { label: "ingest" }.build_error(middle);

    // ErrorCode from inner should be accessible from outer via provide chain
    let code = core::error::request_value::<oopsie::ErrorCode>(&outer);
    assert!(
        code.is_some(),
        "ErrorCode should propagate through source chain"
    );
    assert_eq!(&*code.unwrap(), "inner::code");
}

/// Test that `#[oopsie(traced)]` properly propagates
/// backtrace through nested errors.
#[oopsie::oopsie(traced)]
pub enum AttrInnerError {
    #[oopsie("attr inner: {msg}")]
    Boom { msg: String },
}

#[oopsie::oopsie(traced)]
pub enum AttrOuterError {
    #[oopsie("attr outer: {ctx}")]
    Wrapper { source: AttrInnerError, ctx: String },
}

#[test]
fn nested_attr_macro_source_chain() {
    use attr_inner_oopsies::Boom;
    use attr_outer_oopsies::Wrapper;

    let inner = Boom { msg: "exploded" }.build();
    let outer: AttrOuterError = Wrapper { ctx: "defusing" }.build_error(inner);

    assert_eq!(outer.to_string(), "attr outer: defusing");
    let src = outer.source().unwrap();
    assert_eq!(src.to_string(), "attr inner: exploded");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn nested_attr_macro_backtrace_propagated() {
    use attr_inner_oopsies::Boom;
    use attr_outer_oopsies::Wrapper;

    // Empty traces are withheld from provide(), so capture must succeed.
    oopsie_core::test_utils::force_backtrace();
    let inner = Boom { msg: "kaboom" }.build();
    let outer: AttrOuterError = Wrapper { ctx: "handling" }.build_error(inner);

    // #[oopsie(traced)] injects backtrace with provide(ref, Backtrace => ...)
    // on each variant. The outer error's provide() forwards to source.provide(),
    // so the inner's backtrace should be reachable from the outer.
    let bt = core::error::request_ref::<oopsie::Backtrace>(&outer);
    assert!(
        bt.is_some(),
        "backtrace from #[oopsie(traced)] inner should propagate to outer"
    );
}

#[cfg(all(feature = "unstable-error-generic-member-access", feature = "tracing"))]
#[test]
fn stable_accessor_surfaces_deepest_trace() {
    use attr_inner_oopsies::Boom;
    use attr_outer_oopsies::Wrapper;
    use oopsie::Diagnostic as _;
    use tracing::instrument;

    #[instrument(target = "test")]
    fn make_inner() -> AttrInnerError {
        attr_inner_oopsies::Boom { msg: "deep" }.build()
    }

    // Empty traces are withheld from provide(), so both captures must
    // succeed: force backtraces and build inside an instrumented span.
    oopsie_core::test_utils::force_backtrace();
    let _guard = oopsie_core::test_utils::init_test_subscriber();

    let inner = make_inner();
    let outer: AttrOuterError = Wrapper { ctx: "shallow" }.build_error(inner);

    // The unstable provider path walks source-first under std's first-wins
    // `Request`, so it surfaces the deepest (inner, origin-most) trace.
    let bt_provide = core::error::request_ref::<oopsie::Backtrace>(&outer)
        .expect("provider path yields a backtrace");
    let st_provide = core::error::request_ref::<oopsie::SpanTrace>(&outer)
        .expect("provider path yields a span trace");

    // The stable accessor must agree: the same deepest trace, not the outer
    // wrapper's shallow wrap-site one. Pointer identity proves we surfaced the
    // very same `Backtrace`/`SpanTrace` the provider path filled the slot with.
    let bt_stable = outer
        .oopsie_backtrace()
        .expect("stable accessor yields a backtrace");
    let st_stable = outer
        .oopsie_spantrace()
        .expect("stable accessor yields a span trace");

    assert!(
        std::ptr::eq(bt_provide, bt_stable),
        "oopsie_backtrace() must surface the same deepest backtrace as the provider API, \
         not the outer wrapper's wrap-site one"
    );
    assert!(
        std::ptr::eq(st_provide, st_stable),
        "oopsie_spantrace() must surface the same deepest span trace as the provider API"
    );
}

// ---- Bug fix: help/code with bare #[derive(Oopsie)] using = syntax ----

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum BareHelpError {
    #[oopsie(display("need help"), help = "Try rebooting")]
    NeedHelp { detail: String },
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn provide_help_text_bare_derive() {
    let err = NeedHelp { detail: "stuck" }.build();
    let help = core::error::request_value::<oopsie::HelpText>(&err);
    assert!(help.is_some(), "should provide HelpText");
    assert_eq!(&*help.unwrap(), "Try rebooting");
}

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum BareCodeError {
    #[oopsie(display("coded error"), code = "bare::code")]
    Coded { msg: String },
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn provide_error_code_bare_derive() {
    let err = Coded { msg: "fail" }.build();
    let code = core::error::request_value::<oopsie::ErrorCode>(&err);
    assert!(code.is_some(), "should provide ErrorCode");
    assert_eq!(&*code.unwrap(), "bare::code");
}

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum BareHelpCodeError {
    #[oopsie(display("both"), help = "help text", code = "app::both")]
    Both { info: String },
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn provide_help_and_code_combined() {
    let err = Both { info: "combo" }.build();
    let help = core::error::request_value::<oopsie::HelpText>(&err);
    assert!(help.is_some(), "should provide HelpText");
    assert_eq!(&*help.unwrap(), "help text");
    let code = core::error::request_value::<oopsie::ErrorCode>(&err);
    assert!(code.is_some(), "should provide ErrorCode");
    assert_eq!(&*code.unwrap(), "app::both");
}

// ---- Bug fix: struct provide() works correctly ----

#[oopsie::oopsie(traced)]
pub struct AttrStructWithBt {
    msg: String,
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn struct_provide_backtrace() {
    // Empty traces are withheld from provide(), so capture must succeed.
    oopsie_core::test_utils::force_backtrace();
    let err = AttrStructWithBtOopsie { msg: "test" }.build();
    let bt = core::error::request_ref::<oopsie::Backtrace>(&err);
    assert!(
        bt.is_some(),
        "struct provide() should correctly provide backtrace"
    );
}

// ---- Dynamic help field annotation ----

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum DynamicHelpError {
    #[oopsie("validation failed")]
    WithDynamicHelp {
        #[oopsie(help)]
        suggestion: String,
    },

    #[oopsie("other error")]
    NoHelp { msg: String },
}

#[test]
fn dynamic_help_field_returns_value() {
    use oopsie::Diagnostic as _;
    let err = WithDynamicHelp {
        suggestion: "try a shorter name",
    }
    .build();
    let help = err.oopsie_help_text();
    assert!(help.is_some(), "should return dynamic help text");
    assert_eq!(&*help.unwrap(), "try a shorter name");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn dynamic_help_field_provider_matches_accessor() {
    use oopsie::Diagnostic as _;
    let err = WithDynamicHelp {
        suggestion: "try a shorter name",
    }
    .build();
    let provided = core::error::request_value::<oopsie::HelpText>(&err);
    assert!(provided.is_some(), "provide() should yield dynamic help");
    assert_eq!(provided, err.oopsie_help_text());
}

#[test]
fn no_help_field_returns_none() {
    use oopsie::Diagnostic as _;
    let err = NoHelp { msg: "boom" }.build();
    assert!(
        err.oopsie_help_text().is_none(),
        "variant without help should return None"
    );
}

// Struct parity: a struct with only a `#[oopsie(help)]` field must agree
// between `oopsie_help_text()` and `request_value::<HelpText>`.

#[derive(Debug, Oopsie)]
#[oopsie(suffix)]
struct StructDynamicHelp {
    #[oopsie(help)]
    suggestion: String,
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn struct_dynamic_help_field_provider_matches_accessor() {
    use oopsie::Diagnostic as _;
    let err = StructDynamicHelpOopsie {
        suggestion: "try a shorter name".to_owned(),
    }
    .build();
    let provided = core::error::request_value::<oopsie::HelpText>(&err);
    assert!(provided.is_some(), "provide() should yield dynamic help");
    assert_eq!(provided, err.oopsie_help_text());
}

// ---- help() format-string interpolation referencing variant fields ----
//
// `help("fmt {}", expr)` parses as a DisplayAttr and renders through `::std::format!`,
// exactly like `display(...)`. The generated `oopsie_help_text()` accessor binds the
// variant's fields (mirroring the display arm), so positional args may reference them.
// (Inline `{field}` capture without an explicit arg is covered separately below.
// `code(...)` / `code = "..."` support the same format-string syntax as `help`.)

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum HelpInterpError {
    // Single positional arg referencing the `host` field.
    #[oopsie("connect failed")]
    #[oopsie(help("Retry connecting to {}", host))]
    SingleField { host: String },

    // Multiple positional args, each referencing a variant field.
    #[oopsie("ingest failed")]
    #[oopsie(help("got {} errors in {}", count, module))]
    MultiField { count: u32, module: String },
}

#[test]
fn help_single_field_interpolation_renders_value() {
    use oopsie::Diagnostic as _;
    let err = SingleField {
        host: "db.local".to_owned(),
    }
    .build();
    let help = err.oopsie_help_text();
    assert!(help.is_some(), "should render interpolated help text");
    assert_eq!(&*help.unwrap(), "Retry connecting to db.local");
}

#[test]
fn help_multi_field_interpolation_renders_value() {
    use oopsie::Diagnostic as _;
    let err = MultiField {
        count: 3u32,
        module: "ingest".to_owned(),
    }
    .build();
    let help = err.oopsie_help_text();
    assert!(help.is_some(), "should render interpolated help text");
    assert_eq!(&*help.unwrap(), "got 3 errors in ingest");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn help_field_interpolation_via_provider() {
    let err = SingleField {
        host: "db.local".to_owned(),
    }
    .build();
    let help = core::error::request_value::<oopsie::HelpText>(&err);
    assert!(
        help.is_some(),
        "provider path should yield interpolated help"
    );
    assert_eq!(&*help.unwrap(), "Retry connecting to db.local");
}

// ---- help inline-capture interpolation (`{field}` with no explicit arg) ----
//
// `help = "{field}"` / `help("{field}")` carry no trailing args, but the format
// string references a field by inline capture (like `format!("{x}")`). The macro
// must detect the placeholder and render through `format!`, binding the fields —
// not store the literal verbatim via `from_static`. A brace-free or escaped-only
// string still takes the cheap static path.

#[derive(Debug, Oopsie)]
#[oopsie(suffix, help = "fix the file at {path}")]
struct StructInlineHelp {
    path: String,
}

#[test]
fn struct_inline_capture_help_renders_field() {
    use oopsie::Diagnostic as _;
    let err = StructInlineHelpOopsie {
        path: "/etc/hosts".to_owned(),
    }
    .build();
    let help = err.oopsie_help_text();
    assert!(help.is_some(), "inline-capture help should render");
    assert_eq!(&*help.unwrap(), "fix the file at /etc/hosts");
}

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum HelpInlineError {
    // List form, inline capture, no explicit arg.
    #[oopsie("connect failed")]
    #[oopsie(help("retry connecting to {host}"))]
    ListForm { host: String },

    // Name-value form, inline capture, no explicit arg.
    #[oopsie("restart needed")]
    #[oopsie(help = "restart {service}")]
    NameValueForm { service: String },

    // Escaped braces only, no real placeholder: stays on the static path but
    // must unescape `{{`/`}}` to match format-string semantics.
    #[oopsie("templating")]
    #[oopsie(help = "wrap names in {{braces}}")]
    EscapedOnly { unused: String },
}

#[test]
fn enum_inline_capture_help_list_form_renders_field() {
    use oopsie::Diagnostic as _;
    let err = ListForm {
        host: "db.local".to_owned(),
    }
    .build();
    let help = err.oopsie_help_text();
    assert!(help.is_some(), "inline-capture help should render");
    assert_eq!(&*help.unwrap(), "retry connecting to db.local");
}

#[test]
fn enum_inline_capture_help_name_value_form_renders_field() {
    use oopsie::Diagnostic as _;
    let err = NameValueForm {
        service: "nginx".to_owned(),
    }
    .build();
    let help = err.oopsie_help_text();
    assert!(help.is_some(), "inline-capture help should render");
    assert_eq!(&*help.unwrap(), "restart nginx");
}

#[test]
fn help_escaped_braces_unescape_on_static_path() {
    use oopsie::Diagnostic as _;
    let err = EscapedOnly {
        unused: "x".to_owned(),
    }
    .build();
    let help = err.oopsie_help_text();
    assert!(help.is_some(), "escaped-brace help should render");
    assert_eq!(&*help.unwrap(), "wrap names in {braces}");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn help_inline_capture_via_provider() {
    let err = ListForm {
        host: "db.local".to_owned(),
    }
    .build();
    let help = core::error::request_value::<oopsie::HelpText>(&err);
    assert!(
        help.is_some(),
        "provider path should render interpolated help"
    );
    assert_eq!(&*help.unwrap(), "retry connecting to db.local");
}

// ---- oopsie_error_code() stable accessor (static + dynamic code) ----
//
// `code = "..."` is a plain string with no interpolation. The stable
// `oopsie_error_code()` accessor must surface it on stable toolchains; the
// Provider API (`request_value::<ErrorCode>`) reaches parity on nightly.

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum CodeAccessorError {
    #[oopsie("static coded")]
    #[oopsie(code = "test::code")]
    StaticCode { msg: String },

    #[oopsie("dynamic coded")]
    #[oopsie(provide(::oopsie::ErrorCode => ::oopsie::ErrorCode::from("dyn::code")))]
    DynamicCode { msg: String },

    #[oopsie("uncoded")]
    NoCode { msg: String },
}

#[test]
fn error_code_accessor_returns_static_code() {
    use oopsie::Diagnostic as _;
    let err = StaticCode {
        msg: "boom".to_owned(),
    }
    .build();
    let code = err.oopsie_error_code();
    assert!(code.is_some(), "static code should be accessible on stable");
    assert_eq!(code.unwrap().as_str(), "test::code");
}

#[test]
fn error_code_accessor_returns_none_when_absent() {
    use oopsie::Diagnostic as _;
    let err = NoCode {
        msg: "boom".to_owned(),
    }
    .build();
    assert!(
        err.oopsie_error_code().is_none(),
        "variant without code should return None"
    );
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn error_code_accessor_and_provider_agree() {
    use oopsie::Diagnostic as _;
    let err = StaticCode {
        msg: "boom".to_owned(),
    }
    .build();
    let via_accessor = err.oopsie_error_code().expect("accessor yields code");
    let via_provider =
        core::error::request_value::<oopsie::ErrorCode>(&err).expect("provider yields code");
    assert_eq!(via_accessor.as_str(), via_provider.as_str());
    assert_eq!(via_accessor.as_str(), "test::code");
}

#[test]
fn error_code_accessor_surfaces_dynamic_provide() {
    use oopsie::Diagnostic as _;
    let err = DynamicCode {
        msg: "boom".to_owned(),
    }
    .build();
    // A variant-level `provide(ErrorCode => ...)` is also surfaced through the
    // stable accessor (gen_error.rs folds it into the same `code_arms`).
    let code = err.oopsie_error_code();
    assert!(
        code.is_some(),
        "dynamic provide(ErrorCode) reaches accessor"
    );
    assert_eq!(code.unwrap().as_str(), "dyn::code");
}

#[test]
fn error_code_renders_via_display_end_to_end() {
    use oopsie::Diagnostic as _;
    // End-to-end: a built error's code reaches the accessor and renders through
    // `ErrorCode`'s Display, which is the surface `Report` uses for
    // `Error[<code>]:`.
    let err = StaticCode {
        msg: "boom".to_owned(),
    }
    .build();
    let code = err.oopsie_error_code().expect("code present");
    assert_eq!(format!("{code}"), "test::code");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn error_code_dynamic_accessor_and_provider_agree() {
    use oopsie::Diagnostic as _;
    let err = DynamicCode {
        msg: "boom".to_owned(),
    }
    .build();
    let via_accessor = err
        .oopsie_error_code()
        .expect("accessor yields dynamic code");
    let via_provider =
        core::error::request_value::<oopsie::ErrorCode>(&err).expect("provider yields code");
    assert_eq!(via_accessor.as_str(), "dyn::code");
    assert_eq!(via_accessor.as_str(), via_provider.as_str());
}

// ---- field-level provide(ErrorCode) must reach the stable accessor ----
//
// An `ErrorCode` provide placed on a FIELD (not the container/variant) is
// surfaced by the Provider API; `oopsie_error_code()` must agree on stable,
// or the code is silently dropped from rendered reports.

#[derive(Debug, Oopsie)]
#[oopsie(suffix)]
struct FieldCodeStruct {
    #[oopsie(provide(::oopsie::ErrorCode => ::oopsie::ErrorCode::from("field::code")))]
    msg: String,
}

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum FieldCodeEnum {
    #[oopsie("coded by field")]
    FieldCoded {
        #[oopsie(provide(::oopsie::ErrorCode => ::oopsie::ErrorCode::from("variant::field::code")))]
        msg: String,
    },
}

#[test]
fn error_code_accessor_surfaces_field_level_provide_struct() {
    use oopsie::Diagnostic as _;
    let err = FieldCodeStructOopsie {
        msg: "boom".to_owned(),
    }
    .build();
    let code = err.oopsie_error_code();
    assert!(
        code.is_some(),
        "field-level provide(ErrorCode) must reach the stable accessor"
    );
    assert_eq!(code.unwrap().as_str(), "field::code");
}

#[test]
fn error_code_accessor_surfaces_field_level_provide_enum() {
    use oopsie::Diagnostic as _;
    let err = FieldCoded {
        msg: "boom".to_owned(),
    }
    .build();
    let code = err.oopsie_error_code();
    assert!(
        code.is_some(),
        "field-level provide(ErrorCode) on a variant field must reach the stable accessor"
    );
    assert_eq!(code.unwrap().as_str(), "variant::field::code");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn error_code_field_level_accessor_and_provider_agree() {
    use oopsie::Diagnostic as _;
    let err = FieldCodeStructOopsie {
        msg: "boom".to_owned(),
    }
    .build();
    let via_accessor = err.oopsie_error_code().expect("accessor yields field code");
    let via_provider =
        core::error::request_value::<oopsie::ErrorCode>(&err).expect("provider yields code");
    assert_eq!(via_accessor.as_str(), "field::code");
    assert_eq!(via_accessor.as_str(), via_provider.as_str());
}

// ---- code() format-string interpolation referencing variant/struct fields ----
//
// `code("fmt {}", expr)` and `code = "{field}"` (inline capture) mirror the
// same syntax as `help`. The generated `oopsie_error_code()` accessor and
// the Provider path must both render the interpolated value.

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum CodeInterpError {
    // Positional arg form.
    #[oopsie("connect failed")]
    #[oopsie(code("app::{}", kind))]
    Positional { kind: String },

    // Inline-capture form (no explicit arg).
    #[oopsie("ingest failed")]
    #[oopsie(code = "svc::{module}")]
    InlineCapture { module: String },
}

#[test]
fn code_positional_arg_interpolation_renders_value() {
    use oopsie::Diagnostic as _;
    let err = Positional {
        kind: "db".to_owned(),
    }
    .build();
    let code = err.oopsie_error_code();
    assert!(code.is_some(), "should render interpolated code");
    assert_eq!(code.unwrap().as_str(), "app::db");
}

#[test]
fn code_inline_capture_interpolation_renders_value() {
    use oopsie::Diagnostic as _;
    let err = InlineCapture {
        module: "ingest".to_owned(),
    }
    .build();
    let code = err.oopsie_error_code();
    assert!(code.is_some(), "should render interpolated code");
    assert_eq!(code.unwrap().as_str(), "svc::ingest");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn code_interpolation_accessor_and_provider_agree() {
    use oopsie::Diagnostic as _;
    let err = Positional {
        kind: "net".to_owned(),
    }
    .build();
    let via_accessor = err.oopsie_error_code().expect("accessor yields code");
    let via_provider =
        core::error::request_value::<oopsie::ErrorCode>(&err).expect("provider yields code");
    assert_eq!(via_accessor.as_str(), via_provider.as_str());
    assert_eq!(via_accessor.as_str(), "app::net");
}

// Struct parity: `code = "{field}"` inline capture on a struct.
#[derive(Debug, Oopsie)]
#[oopsie(suffix)]
#[oopsie("struct failed")]
#[oopsie(code = "svc::{kind}")]
struct StructCodeInterp {
    kind: String,
}

#[test]
fn struct_code_inline_capture_renders_value() {
    use oopsie::Diagnostic as _;
    let err = StructCodeInterpOopsie {
        kind: "fs".to_owned(),
    }
    .build();
    let code = err.oopsie_error_code();
    assert!(code.is_some(), "should render interpolated code");
    assert_eq!(code.unwrap().as_str(), "svc::fs");
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn struct_code_inline_capture_accessor_and_provider_agree() {
    use oopsie::Diagnostic as _;
    let err = StructCodeInterpOopsie {
        kind: "mem".to_owned(),
    }
    .build();
    let via_accessor = err.oopsie_error_code().expect("accessor yields code");
    let via_provider =
        core::error::request_value::<oopsie::ErrorCode>(&err).expect("provider yields code");
    assert_eq!(via_accessor.as_str(), via_provider.as_str());
    assert_eq!(via_accessor.as_str(), "svc::mem");
}

// ---- provide(ErrorCode => ...) exprs that reference fields / ref-form ----

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum FieldCodeError {
    #[oopsie("boom")]
    #[oopsie(provide(::oopsie::ErrorCode => ::oopsie::ErrorCode::from(format!("app::{kind}"))))]
    FieldCode { kind: String },

    #[oopsie("ref boom")]
    #[oopsie(provide(ref, ::oopsie::ErrorCode => code))]
    RefCode { code: oopsie::ErrorCode },
}

#[derive(Debug, Oopsie)]
#[oopsie(suffix)]
#[oopsie("struct boom")]
#[oopsie(provide(::oopsie::ErrorCode => ::oopsie::ErrorCode::from(format!("app::{kind}"))))]
struct StructFieldCode {
    kind: String,
}

#[derive(Debug, Oopsie)]
#[oopsie(suffix)]
#[oopsie("struct ref boom")]
#[oopsie(provide(ref, ::oopsie::ErrorCode => code))]
struct StructRefCode {
    code: oopsie::ErrorCode,
}

#[test]
fn error_code_provide_can_reference_fields() {
    use oopsie::Diagnostic as _;
    let err = FieldCode {
        kind: "db".to_owned(),
    }
    .build();
    let code = err.oopsie_error_code().expect("accessor yields code");
    assert_eq!(code.as_str(), "app::db");
}

#[test]
fn error_code_ref_provide_returns_owned_clone() {
    use oopsie::Diagnostic as _;
    let err = RefCode {
        code: oopsie::ErrorCode::from("ref::code"),
    }
    .build();
    let code = err.oopsie_error_code().expect("accessor yields code");
    assert_eq!(code.as_str(), "ref::code");
}

#[test]
fn struct_error_code_provide_can_reference_fields() {
    use oopsie::Diagnostic as _;
    let err = StructFieldCodeOopsie {
        kind: "fs".to_owned(),
    }
    .build();
    let code = err.oopsie_error_code().expect("accessor yields code");
    assert_eq!(code.as_str(), "app::fs");
}

#[test]
fn struct_error_code_ref_provide_returns_owned_clone() {
    use oopsie::Diagnostic as _;
    let err = StructRefCodeOopsie {
        code: oopsie::ErrorCode::from("ref::struct"),
    }
    .build();
    let code = err.oopsie_error_code().expect("accessor yields code");
    assert_eq!(code.as_str(), "ref::struct");
}

// ---- oopsie_exit_code() stable accessor ----
//
// `exit_code = N` is honored on stable through the `oopsie_exit_code()`
// accessor; the Provider path (`request_value::<NonZeroU8>`) reaches parity on
// nightly. A container-level value is the default for every variant, overridden
// per variant.

#[derive(Debug, Oopsie)]
#[oopsie(module(false), exit_code = 3)]
enum ExitCodeError {
    #[oopsie("default coded")]
    UsesDefault { msg: String },

    #[oopsie("override coded")]
    #[oopsie(exit_code = 78)]
    Overrides { msg: String },
}

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum NoContainerExitError {
    #[oopsie("only this one")]
    #[oopsie(exit_code = 42)]
    SoloCoded { msg: String },

    #[oopsie("uncoded")]
    Uncoded { msg: String },
}

#[test]
fn exit_code_variant_value_surfaces() {
    use oopsie::Diagnostic as _;
    let err = SoloCoded {
        msg: "boom".to_owned(),
    }
    .build();
    assert_eq!(
        err.oopsie_exit_code().map(core::num::NonZeroU8::get),
        Some(42)
    );
}

#[test]
fn exit_code_absent_returns_none() {
    use oopsie::Diagnostic as _;
    let err = Uncoded {
        msg: "boom".to_owned(),
    }
    .build();
    assert!(err.oopsie_exit_code().is_none());
}

#[test]
fn exit_code_container_default_applies() {
    use oopsie::Diagnostic as _;
    let err = UsesDefault {
        msg: "boom".to_owned(),
    }
    .build();
    assert_eq!(
        err.oopsie_exit_code().map(core::num::NonZeroU8::get),
        Some(3)
    );
}

#[test]
fn exit_code_variant_overrides_container_default() {
    use oopsie::Diagnostic as _;
    let err = Overrides {
        msg: "boom".to_owned(),
    }
    .build();
    assert_eq!(
        err.oopsie_exit_code().map(core::num::NonZeroU8::get),
        Some(78)
    );
}

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn exit_code_accessor_and_provider_agree() {
    use oopsie::Diagnostic as _;
    let err = Overrides {
        msg: "boom".to_owned(),
    }
    .build();
    let via_accessor = err.oopsie_exit_code().expect("accessor yields exit code");
    let via_provider = core::error::request_value::<core::num::NonZeroU8>(&err)
        .expect("provider yields exit code");
    assert_eq!(via_accessor, via_provider);
    assert_eq!(via_accessor.get(), 78);
}

// ---- struct exit_code ----

#[oopsie::oopsie]
#[oopsie(exit_code = 70)]
pub struct ExitStructError {
    detail: String,
}

#[test]
fn struct_exit_code_surfaces() {
    use oopsie::Diagnostic as _;
    let err = ExitStructOopsie {
        detail: "x".to_owned(),
    }
    .build();
    assert_eq!(
        err.oopsie_exit_code().map(core::num::NonZeroU8::get),
        Some(70)
    );
}

// ---- transparent forwarding ----

#[oopsie::oopsie]
#[oopsie(exit_code = 65)]
pub enum ExitLeafError {
    #[oopsie("leaf boom")]
    Boom { detail: String },
}

#[derive(Debug, Oopsie)]
#[oopsie(module(false))]
enum ExitWrapperError {
    #[oopsie("wrapped")]
    #[oopsie(transparent)]
    Wrap { source: ExitLeafError },
}

#[test]
fn exit_code_transparent_forwards_from_source() {
    use oopsie::Diagnostic as _;
    let leaf = exit_leaf_oopsies::Boom {
        detail: "x".to_owned(),
    }
    .build();
    let wrapper = ExitWrapperError::Wrap { source: leaf };
    assert_eq!(
        wrapper.oopsie_exit_code().map(core::num::NonZeroU8::get),
        Some(65)
    );
}

// ---- Welp forwarding ----
//
// A `#[oopsie]` origin's exit code survives `.welp()` wrapping. Reaching it
// requires the Provider API (the source is type-erased), so this is gated on
// the unstable feature; on stable `Welp` carries no exit code of its own.

#[cfg(feature = "unstable-error-generic-member-access")]
#[test]
fn welp_forwards_source_exit_code() {
    use oopsie::Diagnostic as _;
    use oopsie::WelpResultExt as _;
    let leaf = exit_leaf_oopsies::Boom {
        detail: "x".to_owned(),
    }
    .build();
    let welp = Err::<(), _>(leaf).welp().unwrap_err();
    assert_eq!(
        welp.oopsie_exit_code().map(core::num::NonZeroU8::get),
        Some(65)
    );
}