excel-api 0.1.0

Safe Rust abstractions for native Microsoft Excel XLL add-ins
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
//! Read-only views over values supplied by Excel for one callback invocation.
//!
//! The only entry point from the raw ABI is [`RawExcelValue::from_callback`].
//! It is unsafe because Rust cannot prove the validity or lifetime of the
//! pointer-bearing members of an `XLOPER12`. Once that boundary has been
//! audited by the callback thunk, decoding and observation are safe.
//!
//! Callback views cannot be sent to another thread:
//!
//! ```compile_fail
//! use excel_api::RawExcelValue;
//!
//! fn require_send<T: Send>() {}
//! require_send::<RawExcelValue<'static>>();
//! ```
//!
//! Nor can their callback lifetime be extended:
//!
//! ```compile_fail
//! use excel_api::{ExcelValueRef, RawExcelValue};
//!
//! fn escape(raw: &RawExcelValue<'_>) -> ExcelValueRef<'static> {
//!     raw.decode().unwrap()
//! }
//! ```

use core::{
    marker::PhantomData,
    mem::size_of,
    ptr::{self, NonNull},
    slice,
};

use excel_api_sys::{
    COL, EXCEL12_MAX_COLUMN, EXCEL12_MAX_COLUMNS, EXCEL12_MAX_ROW, EXCEL12_MAX_ROWS,
    EXCEL12_MAX_STRING_CODE_UNITS, IDSHEET, RW, XCHAR, XLBIT_DLL_FREE, XLBIT_XL_FREE, XLOPER12,
    XLREF12, XLTYPE_BIG_DATA, XLTYPE_BOOL, XLTYPE_ERR, XLTYPE_FLOW, XLTYPE_INT, XLTYPE_MISSING,
    XLTYPE_MULTI, XLTYPE_NIL, XLTYPE_NUM, XLTYPE_REF, XLTYPE_SREF, XLTYPE_STR, XlError,
};

use crate::ExcelError;

type CallbackMarker<'call> = PhantomData<(&'call XLOPER12, *mut ())>;

/// Why a raw callback value could not be represented as a safe borrowed view.
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum DecodeError {
    /// The value tag contains unsupported or mutually inconsistent bits.
    MalformedType(u32),
    /// The value uses a valid Excel tag that this safe view does not represent.
    UnsupportedType(u32),
    /// The numeric Excel error code has no corresponding [`ExcelError`].
    InvalidError(i32),
    /// A string-tagged value has a null payload pointer.
    NullStringPointer,
    /// A string length exceeds Excel's documented UTF-16 limit.
    StringTooLong(usize),
    /// A direct string lacked a terminator within Excel's bounded limit.
    UnterminatedString,
    /// Array dimensions were invalid or overflowed.
    MalformedArrayDimensions {
        /// Raw Excel row count.
        rows: RW,
        /// Raw Excel column count.
        columns: COL,
    },
    /// The validated array extent cannot be represented safely by Rust.
    ArrayTooLarge,
    /// An array-tagged value has a null element pointer.
    NullArrayPointer,
    /// An array element attempted to contain another array.
    NestedArray,
    /// An array element attempted to contain an Excel reference.
    ReferenceInArray,
    /// An `xltypeSRef` value did not contain exactly one area.
    InvalidSingleReferenceCount(u16),
    /// A reference-tagged value has a null reference table pointer.
    NullReferencePointer,
    /// An `xltypeRef` value declared no reference areas.
    EmptyMultiReference,
    /// A reference area has invalid or out-of-range row or column bounds.
    MalformedReferenceArea,
}

/// An `XLOPER12` whose storage is owned by Excel for one callback invocation.
///
/// This wrapper is deliberately neither `Clone`, `Copy`, `Send`, nor `Sync`.
///
/// ```compile_fail
/// use excel_api::RawExcelValue;
/// fn require_sync<T: Sync>() {}
/// require_sync::<RawExcelValue<'static>>();
/// ```
///
/// ```compile_fail
/// use excel_api::RawExcelValue;
/// fn require_clone<T: Clone>() {}
/// require_clone::<RawExcelValue<'static>>();
/// ```
#[derive(Debug)]
pub struct RawExcelValue<'call> {
    raw: NonNull<XLOPER12>,
    _callback: CallbackMarker<'call>,
}

impl<'call> RawExcelValue<'call> {
    /// Establishes the single unsafe boundary for a callback argument.
    ///
    /// # Safety
    ///
    /// `raw` and every pointer-bearing member reachable from it must describe
    /// initialized, properly aligned SDK storage that remains readable and is
    /// not mutated for all of `'call`. Counted strings must include their
    /// prefix and payload, null-terminated direct strings have separate
    /// constructors, multis must contain `rows * columns` initialized elements,
    /// and multi-references must contain `count` initialized areas.
    pub unsafe fn from_callback(raw: &'call XLOPER12) -> Self {
        Self {
            raw: NonNull::from(raw),
            _callback: PhantomData,
        }
    }

    /// Decodes the raw value after masking Excel's ownership bits.
    pub fn decode(&self) -> Result<ExcelValueRef<'call>, DecodeError> {
        // SAFETY: `from_callback` establishes readability and immutability for
        // the complete callback lifetime.
        unsafe { decode_xloper(self.raw, DecodeContext::TopLevel) }
    }
}

/// A decoded, read-only Excel callback value.
///
/// ```compile_fail
/// use excel_api::ExcelValueRef;
/// fn require_send<T: Send>() {}
/// require_send::<ExcelValueRef<'static>>();
/// ```
///
/// ```compile_fail
/// use excel_api::ExcelValueRef;
/// fn require_sync<T: Sync>() {}
/// require_sync::<ExcelValueRef<'static>>();
/// ```
///
/// ```compile_fail
/// use excel_api::ExcelValueRef;
/// fn require_clone<T: Clone>() {}
/// require_clone::<ExcelValueRef<'static>>();
/// ```
#[derive(Debug)]
pub enum ExcelValueRef<'call> {
    /// A binary floating-point scalar.
    Number(f64),
    /// A callback-borrowed UTF-16 string view.
    Text(ExcelStr<'call>),
    /// A Boolean scalar.
    Boolean(bool),
    /// A callback-borrowed worksheet reference.
    Reference(ExcelReference<'call>),
    /// A controlled Excel worksheet error.
    Error(ExcelError),
    /// A callback-borrowed two-dimensional array view.
    Array(ExcelArrayView<'call>),
    /// An omitted argument.
    Missing(ExcelMissing),
    /// Excel's distinct empty value.
    Nil(ExcelNil),
    /// Excel's 32-bit integer scalar.
    Integer(i32),
}

impl ExcelValueRef<'_> {
    /// Returns a stable, human-readable name for this variant's value kind.
    pub const fn kind_name(&self) -> &'static str {
        match self {
            Self::Number(_) => "number",
            Self::Text(_) => "text",
            Self::Boolean(_) => "boolean",
            Self::Reference(_) => "reference",
            Self::Error(_) => "error",
            Self::Array(_) => "array",
            Self::Missing(_) => "missing",
            Self::Nil(_) => "nil",
            Self::Integer(_) => "integer",
        }
    }
}

/// Borrowed UTF-16 code units. No Unicode conversion is performed.
///
/// ```compile_fail
/// use excel_api::ExcelStr;
/// fn require_send<T: Send>() {}
/// require_send::<ExcelStr<'static>>();
/// ```
///
/// ```compile_fail
/// use excel_api::ExcelStr;
/// fn require_sync<T: Sync>() {}
/// require_sync::<ExcelStr<'static>>();
/// ```
///
/// ```compile_fail
/// use excel_api::ExcelStr;
/// fn require_clone<T: Clone>() {}
/// require_clone::<ExcelStr<'static>>();
/// ```
#[derive(Debug)]
pub struct ExcelStr<'call> {
    payload: NonNull<XCHAR>,
    len: usize,
    _callback: CallbackMarker<'call>,
}

impl<'call> ExcelStr<'call> {
    /// Parses a registered counted direct UTF-16 argument.
    ///
    /// # Safety
    ///
    /// `counted` must be aligned, readable, and immutable for `'call`. It must
    /// contain a readable length prefix followed by that many code units.
    pub unsafe fn from_counted_direct(counted: &'call XCHAR) -> Result<Self, DecodeError> {
        // SAFETY: guaranteed by this function's caller.
        unsafe { parse_direct_counted(NonNull::from(counted)) }
    }

    /// Parses a registered null-terminated direct UTF-16 argument.
    ///
    /// Scanning is bounded by Excel's 32,767-code-unit string limit.
    ///
    /// # Safety
    ///
    /// `terminated` must be aligned, readable, and immutable for `'call`
    /// through the first NUL or through index 32,767 inclusive.
    pub unsafe fn from_null_terminated_direct(
        terminated: &'call XCHAR,
    ) -> Result<Self, DecodeError> {
        // SAFETY: guaranteed by this function's caller.
        unsafe { parse_direct_null_terminated(NonNull::from(terminated)) }
    }

    /// Returns the number of UTF-16 code units, not Unicode scalar values.
    pub const fn len(&self) -> usize {
        self.len
    }

    /// Returns whether the borrowed payload contains no UTF-16 code units.
    pub const fn is_empty(&self) -> bool {
        self.len == 0
    }

    /// Returns the exact UTF-16 payload, excluding a prefix or terminator.
    pub fn as_utf16(&self) -> &'call [u16] {
        // SAFETY: every constructor validates the payload extent, and the
        // callback contract keeps it readable and immutable for `'call`.
        unsafe { slice::from_raw_parts(self.payload.as_ptr(), self.len) }
    }
}

/// Marker for an omitted function argument (`xltypeMissing`).
#[derive(Debug, Eq, PartialEq)]
pub struct ExcelMissing {
    _private: (),
}

/// Marker for an empty Excel value (`xltypeNil`).
#[derive(Debug, Eq, PartialEq)]
pub struct ExcelNil {
    _private: (),
}

/// A flat row-major `xltypeMulti` view.
///
/// ```compile_fail
/// use excel_api::ExcelArrayView;
/// fn require_send<T: Send>() {}
/// require_send::<ExcelArrayView<'static>>();
/// ```
///
/// ```compile_fail
/// use excel_api::ExcelArrayView;
/// fn require_sync<T: Sync>() {}
/// require_sync::<ExcelArrayView<'static>>();
/// ```
///
/// ```compile_fail
/// use excel_api::ExcelArrayView;
/// fn require_clone<T: Clone>() {}
/// require_clone::<ExcelArrayView<'static>>();
/// ```
#[derive(Debug)]
pub struct ExcelArrayView<'call> {
    elements: NonNull<XLOPER12>,
    rows: usize,
    columns: usize,
    _callback: CallbackMarker<'call>,
}

impl<'call> ExcelArrayView<'call> {
    /// Returns `(rows, columns)` for this rectangular callback array.
    pub const fn dimensions(&self) -> (usize, usize) {
        (self.rows, self.columns)
    }

    /// Returns the number of rows.
    pub const fn row_count(&self) -> usize {
        self.rows
    }

    /// Returns the number of columns.
    pub const fn column_count(&self) -> usize {
        self.columns
    }

    /// Decodes one element, or returns `Ok(None)` when the index is outside it.
    ///
    /// The returned view borrows the same Excel callback storage as this array.
    pub fn get(
        &self,
        row: usize,
        column: usize,
    ) -> Result<Option<ExcelValueRef<'call>>, DecodeError> {
        if row >= self.rows || column >= self.columns {
            return Ok(None);
        }
        let index = row * self.columns + column;
        self.decode_index(index).map(Some)
    }

    /// Iterates decoded elements in `row`, or returns `None` for an invalid row.
    pub fn row(&self, row: usize) -> Option<ExcelArrayElements<'_, 'call>> {
        (row < self.rows).then_some(ExcelArrayElements {
            array: self,
            next: row * self.columns,
            remaining: self.columns,
            stride: 1,
        })
    }

    /// Iterates decoded elements in `column`, or returns `None` for an invalid column.
    pub fn column(&self, column: usize) -> Option<ExcelArrayElements<'_, 'call>> {
        (column < self.columns).then_some(ExcelArrayElements {
            array: self,
            next: column,
            remaining: self.rows,
            stride: self.columns,
        })
    }

    /// Iterates the rows in row-major order.
    pub fn rows(&self) -> ExcelArrayRows<'_, 'call> {
        ExcelArrayRows {
            array: self,
            next: 0,
        }
    }

    /// Iterates the columns in column-major order.
    pub fn columns(&self) -> ExcelArrayColumns<'_, 'call> {
        ExcelArrayColumns {
            array: self,
            next: 0,
        }
    }

    fn decode_index(&self, index: usize) -> Result<ExcelValueRef<'call>, DecodeError> {
        // SAFETY: array construction validates the allocation extent and this
        // method only receives an in-bounds linear index.
        let element = unsafe { NonNull::new_unchecked(self.elements.as_ptr().add(index)) };
        // SAFETY: the array callback contract covers every initialized element.
        unsafe { decode_xloper(element, DecodeContext::ArrayElement) }
    }

    fn validate_elements(&self) -> Result<(), DecodeError> {
        let count = self.rows * self.columns;
        for index in 0..count {
            self.decode_index(index)?;
        }
        Ok(())
    }
}

/// Elements in one row or column of an Excel array.
#[derive(Debug)]
pub struct ExcelArrayElements<'view, 'call> {
    array: &'view ExcelArrayView<'call>,
    next: usize,
    remaining: usize,
    stride: usize,
}

impl<'call> Iterator for ExcelArrayElements<'_, 'call> {
    type Item = Result<ExcelValueRef<'call>, DecodeError>;

    fn next(&mut self) -> Option<Self::Item> {
        if self.remaining == 0 {
            return None;
        }
        let index = self.next;
        self.next += self.stride;
        self.remaining -= 1;
        Some(self.array.decode_index(index))
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.remaining, Some(self.remaining))
    }
}

impl ExactSizeIterator for ExcelArrayElements<'_, '_> {}

/// Iterator over an array's rows.
#[derive(Debug)]
pub struct ExcelArrayRows<'view, 'call> {
    array: &'view ExcelArrayView<'call>,
    next: usize,
}

impl<'view, 'call> Iterator for ExcelArrayRows<'view, 'call> {
    type Item = ExcelArrayElements<'view, 'call>;

    fn next(&mut self) -> Option<Self::Item> {
        let row = self.next;
        let result = self.array.row(row)?;
        self.next += 1;
        Some(result)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.array.rows - self.next;
        (remaining, Some(remaining))
    }
}

impl ExactSizeIterator for ExcelArrayRows<'_, '_> {}

/// Iterator over an array's columns.
#[derive(Debug)]
pub struct ExcelArrayColumns<'view, 'call> {
    array: &'view ExcelArrayView<'call>,
    next: usize,
}

impl<'view, 'call> Iterator for ExcelArrayColumns<'view, 'call> {
    type Item = ExcelArrayElements<'view, 'call>;

    fn next(&mut self) -> Option<Self::Item> {
        let column = self.next;
        let result = self.array.column(column)?;
        self.next += 1;
        Some(result)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.array.columns - self.next;
        (remaining, Some(remaining))
    }
}

impl ExactSizeIterator for ExcelArrayColumns<'_, '_> {}

/// A reference-preserving callback value.
///
/// ```compile_fail
/// use excel_api::ExcelReference;
/// fn require_send<T: Send>() {}
/// require_send::<ExcelReference<'static>>();
/// ```
///
/// ```compile_fail
/// use excel_api::ExcelReference;
/// fn require_sync<T: Sync>() {}
/// require_sync::<ExcelReference<'static>>();
/// ```
///
/// ```compile_fail
/// use excel_api::ExcelReference;
/// fn require_clone<T: Clone>() {}
/// require_clone::<ExcelReference<'static>>();
/// ```
#[derive(Debug)]
pub enum ExcelReference<'call> {
    /// One inline current-sheet area.
    Single(ExcelSingleReference<'call>),
    /// One or more explicit-sheet areas.
    Multiple(ExcelMultiReference<'call>),
}

/// An inline, current-sheet `xltypeSRef`.
#[derive(Debug)]
pub struct ExcelSingleReference<'call> {
    area: ExcelReferenceArea<'call>,
}

impl<'call> ExcelSingleReference<'call> {
    /// Returns the sole rectangular area without copying callback storage.
    pub const fn area(&self) -> &ExcelReferenceArea<'call> {
        &self.area
    }
}

/// An explicit-sheet, one-or-more-area `xltypeRef`.
#[derive(Debug)]
pub struct ExcelMultiReference<'call> {
    areas: NonNull<XLREF12>,
    area_count: usize,
    sheet_id: IDSHEET,
    _callback: CallbackMarker<'call>,
}

impl<'call> ExcelMultiReference<'call> {
    /// Returns Excel's identifier for the referenced worksheet.
    pub const fn sheet_id(&self) -> IDSHEET {
        self.sheet_id
    }

    /// Returns the number of rectangular areas in this reference.
    pub const fn area_count(&self) -> usize {
        self.area_count
    }

    /// Returns a copied view of the indexed area, if it exists.
    pub fn area(&self, index: usize) -> Option<ExcelReferenceArea<'call>> {
        if index >= self.area_count {
            return None;
        }
        // SAFETY: the callback contract guarantees `area_count` initialized
        // entries and the bounds check keeps this pointer within them.
        let area = unsafe { NonNull::new_unchecked(self.areas.as_ptr().add(index)) };
        Some(ExcelReferenceArea {
            area,
            _callback: PhantomData,
        })
    }

    /// Iterates all referenced areas in Excel's table order.
    pub fn areas(&self) -> ExcelReferenceAreas<'_, 'call> {
        ExcelReferenceAreas {
            reference: self,
            next: 0,
        }
    }
}

/// One rectangular area within a reference.
#[derive(Debug)]
pub struct ExcelReferenceArea<'call> {
    area: NonNull<XLREF12>,
    _callback: CallbackMarker<'call>,
}

impl ExcelReferenceArea<'_> {
    /// Returns the first included zero-based worksheet row.
    pub fn first_row(&self) -> RW {
        self.read().rwFirst
    }

    /// Returns the last included zero-based worksheet row.
    pub fn last_row(&self) -> RW {
        self.read().rwLast
    }

    /// Returns the first included zero-based worksheet column.
    pub fn first_column(&self) -> COL {
        self.read().colFirst
    }

    /// Returns the last included zero-based worksheet column.
    pub fn last_column(&self) -> COL {
        self.read().colLast
    }

    fn read(&self) -> XLREF12 {
        // SAFETY: construction validates that this area is initialized and the
        // callback contract prevents mutation for its lifetime.
        unsafe { self.area.as_ptr().read() }
    }
}

/// Iterator over the areas of an `xltypeRef`.
#[derive(Debug)]
pub struct ExcelReferenceAreas<'view, 'call> {
    reference: &'view ExcelMultiReference<'call>,
    next: usize,
}

impl<'call> Iterator for ExcelReferenceAreas<'_, 'call> {
    type Item = ExcelReferenceArea<'call>;

    fn next(&mut self) -> Option<Self::Item> {
        let result = self.reference.area(self.next)?;
        self.next += 1;
        Some(result)
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let remaining = self.reference.area_count - self.next;
        (remaining, Some(remaining))
    }
}

impl ExactSizeIterator for ExcelReferenceAreas<'_, '_> {}

#[derive(Clone, Copy)]
enum DecodeContext {
    TopLevel,
    ArrayElement,
}

unsafe fn decode_xloper<'call>(
    raw: NonNull<XLOPER12>,
    context: DecodeContext,
) -> Result<ExcelValueRef<'call>, DecodeError> {
    // SAFETY: the caller carries the callback allocation invariant.
    let xltype = unsafe { ptr::addr_of!((*raw.as_ptr()).xltype).read() };
    let base_type = xltype & !(XLBIT_XL_FREE | XLBIT_DLL_FREE);

    match base_type {
        XLTYPE_NUM => {
            // SAFETY: the validated tag selects the `num` union member.
            let value = unsafe { ptr::addr_of!((*raw.as_ptr()).val.num).read() };
            Ok(ExcelValueRef::Number(value))
        }
        XLTYPE_STR => {
            // SAFETY: the validated tag selects the `str` union member.
            let string = unsafe { ptr::addr_of!((*raw.as_ptr()).val.str).read() };
            let string = NonNull::new(string).ok_or(DecodeError::NullStringPointer)?;
            // SAFETY: the callback boundary guarantees the counted payload.
            unsafe { parse_xloper_counted(string).map(ExcelValueRef::Text) }
        }
        XLTYPE_BOOL => {
            // SAFETY: the validated tag selects the `xbool` union member.
            let value = unsafe { ptr::addr_of!((*raw.as_ptr()).val.xbool).read() };
            Ok(ExcelValueRef::Boolean(value != 0))
        }
        XLTYPE_REF => {
            if matches!(context, DecodeContext::ArrayElement) {
                return Err(DecodeError::ReferenceInArray);
            }
            // SAFETY: the validated tag selects the `mref` union member.
            let value = unsafe { ptr::addr_of!((*raw.as_ptr()).val.mref).read() };
            let reference = NonNull::new(value.lpmref).ok_or(DecodeError::NullReferencePointer)?;
            // SAFETY: the callback boundary guarantees a readable XLMREF12.
            let count = unsafe { ptr::addr_of!((*reference.as_ptr()).count).read() } as usize;
            if count == 0 {
                return Err(DecodeError::EmptyMultiReference);
            }
            // SAFETY: `reftbl` begins at this SDK-defined field address.
            let areas = unsafe {
                NonNull::new_unchecked(
                    ptr::addr_of!((*reference.as_ptr()).reftbl)
                        .cast::<XLREF12>()
                        .cast_mut(),
                )
            };
            validate_reference_areas(areas, count)?;
            Ok(ExcelValueRef::Reference(ExcelReference::Multiple(
                ExcelMultiReference {
                    areas,
                    area_count: count,
                    sheet_id: value.idSheet,
                    _callback: PhantomData,
                },
            )))
        }
        XLTYPE_ERR => {
            // SAFETY: the validated tag selects the `err` union member.
            let value = unsafe { ptr::addr_of!((*raw.as_ptr()).val.err).read() };
            decode_error(value).map(ExcelValueRef::Error)
        }
        XLTYPE_FLOW => Err(DecodeError::UnsupportedType(base_type)),
        XLTYPE_MULTI => {
            if matches!(context, DecodeContext::ArrayElement) {
                return Err(DecodeError::NestedArray);
            }
            // SAFETY: the validated tag selects the `array` union member.
            let value = unsafe { ptr::addr_of!((*raw.as_ptr()).val.array).read() };
            let (rows, columns) = validate_array_dimensions(value.rows, value.columns)?;
            let elements = NonNull::new(value.lparray).ok_or(DecodeError::NullArrayPointer)?;
            let array = ExcelArrayView {
                elements,
                rows,
                columns,
                _callback: PhantomData,
            };
            array.validate_elements()?;
            Ok(ExcelValueRef::Array(array))
        }
        XLTYPE_MISSING => Ok(ExcelValueRef::Missing(ExcelMissing { _private: () })),
        XLTYPE_NIL => Ok(ExcelValueRef::Nil(ExcelNil { _private: () })),
        XLTYPE_SREF => {
            if matches!(context, DecodeContext::ArrayElement) {
                return Err(DecodeError::ReferenceInArray);
            }
            // SAFETY: the validated tag selects the `sref` union member.
            let value = unsafe { ptr::addr_of!((*raw.as_ptr()).val.sref).read() };
            if value.count != 1 {
                return Err(DecodeError::InvalidSingleReferenceCount(value.count));
            }
            validate_reference_area(value.reference)?;
            // SAFETY: this is the address of the inline `reference` field and
            // the parent XLOPER12 remains fixed for the callback lifetime.
            let area = unsafe {
                NonNull::new_unchecked(ptr::addr_of!((*raw.as_ptr()).val.sref.reference).cast_mut())
            };
            Ok(ExcelValueRef::Reference(ExcelReference::Single(
                ExcelSingleReference {
                    area: ExcelReferenceArea {
                        area,
                        _callback: PhantomData,
                    },
                },
            )))
        }
        XLTYPE_INT => {
            // SAFETY: the validated tag selects the `w` union member.
            let value = unsafe { ptr::addr_of!((*raw.as_ptr()).val.w).read() };
            Ok(ExcelValueRef::Integer(value))
        }
        XLTYPE_BIG_DATA => Err(DecodeError::UnsupportedType(base_type)),
        _ => Err(DecodeError::MalformedType(base_type)),
    }
}

unsafe fn parse_xloper_counted<'call>(
    counted: NonNull<XCHAR>,
) -> Result<ExcelStr<'call>, DecodeError> {
    // SAFETY: this parser's caller guarantees a readable counted XLOPER string.
    unsafe { parse_counted_payload(counted) }
}

unsafe fn parse_direct_counted<'call>(
    counted: NonNull<XCHAR>,
) -> Result<ExcelStr<'call>, DecodeError> {
    // SAFETY: this parser's caller guarantees a readable direct counted string.
    unsafe { parse_counted_payload(counted) }
}

unsafe fn parse_counted_payload<'call>(
    counted: NonNull<XCHAR>,
) -> Result<ExcelStr<'call>, DecodeError> {
    // SAFETY: the prefix is initialized and readable by the parser contract.
    let len = unsafe { counted.as_ptr().read() } as usize;
    if len > EXCEL12_MAX_STRING_CODE_UNITS {
        return Err(DecodeError::StringTooLong(len));
    }
    // SAFETY: a counted string always has a prefix, so adding one stays at the
    // payload start even when the logical payload is empty.
    let payload = unsafe { NonNull::new_unchecked(counted.as_ptr().add(1)) };
    Ok(ExcelStr {
        payload,
        len,
        _callback: PhantomData,
    })
}

unsafe fn parse_direct_null_terminated<'call>(
    terminated: NonNull<XCHAR>,
) -> Result<ExcelStr<'call>, DecodeError> {
    for len in 0..=EXCEL12_MAX_STRING_CODE_UNITS {
        // SAFETY: the parser contract guarantees readability through this
        // bounded scan range or the first terminator.
        if unsafe { terminated.as_ptr().add(len).read() } == 0 {
            return Ok(ExcelStr {
                payload: terminated,
                len,
                _callback: PhantomData,
            });
        }
    }
    Err(DecodeError::UnterminatedString)
}

fn validate_array_dimensions(rows: RW, columns: COL) -> Result<(usize, usize), DecodeError> {
    if !(1..=EXCEL12_MAX_ROWS).contains(&rows) || !(1..=EXCEL12_MAX_COLUMNS).contains(&columns) {
        return Err(DecodeError::MalformedArrayDimensions { rows, columns });
    }
    let rows = usize::try_from(rows).map_err(|_| DecodeError::ArrayTooLarge)?;
    let columns = usize::try_from(columns).map_err(|_| DecodeError::ArrayTooLarge)?;
    let count = rows
        .checked_mul(columns)
        .ok_or(DecodeError::ArrayTooLarge)?;
    if count > isize::MAX as usize / size_of::<XLOPER12>() {
        return Err(DecodeError::ArrayTooLarge);
    }
    Ok((rows, columns))
}

fn validate_reference_areas(areas: NonNull<XLREF12>, count: usize) -> Result<(), DecodeError> {
    for index in 0..count {
        // SAFETY: the multi-reference callback contract guarantees `count`
        // initialized entries.
        let area = unsafe { areas.as_ptr().add(index).read() };
        validate_reference_area(area)?;
    }
    Ok(())
}

fn validate_reference_area(area: XLREF12) -> Result<(), DecodeError> {
    let rows_valid = (0..=EXCEL12_MAX_ROW).contains(&area.rwFirst)
        && (area.rwFirst..=EXCEL12_MAX_ROW).contains(&area.rwLast);
    let columns_valid = (0..=EXCEL12_MAX_COLUMN).contains(&area.colFirst)
        && (area.colFirst..=EXCEL12_MAX_COLUMN).contains(&area.colLast);
    if rows_valid && columns_valid {
        Ok(())
    } else {
        Err(DecodeError::MalformedReferenceArea)
    }
}

fn decode_error(value: i32) -> Result<ExcelError, DecodeError> {
    let value = match value {
        excel_api_sys::xlerrNull => XlError::Null,
        excel_api_sys::xlerrDiv0 => XlError::Div0,
        excel_api_sys::xlerrValue => XlError::Value,
        excel_api_sys::xlerrRef => XlError::Ref,
        excel_api_sys::xlerrName => XlError::Name,
        excel_api_sys::xlerrNum => XlError::Num,
        excel_api_sys::xlerrNA => XlError::Na,
        excel_api_sys::xlerrGettingData => XlError::GettingData,
        _ => return Err(DecodeError::InvalidError(value)),
    };
    Ok(value.into())
}

#[cfg(test)]
mod tests {
    use core::ffi::c_void;

    use excel_api_sys::{
        XLMREF12, XLOPER12Array, XLOPER12BigData, XLOPER12BigDataHandle, XLOPER12MRef,
        XLOPER12SRef, XLOPER12Value, xlbitDLLFree, xlbitXLFree, xltypeBigData, xltypeBool,
        xltypeErr, xltypeFlow, xltypeInt, xltypeMissing, xltypeMulti, xltypeNil, xltypeNum,
        xltypeRef, xltypeSRef, xltypeStr,
    };

    use super::*;

    fn raw(value: XLOPER12Value, xltype: u32) -> XLOPER12 {
        XLOPER12 { val: value, xltype }
    }

    fn decode(value: &XLOPER12) -> Result<ExcelValueRef<'_>, DecodeError> {
        // SAFETY: each test keeps all referenced fixtures alive and immutable
        // for the returned view's lifetime.
        unsafe { RawExcelValue::from_callback(value) }.decode()
    }

    fn zeroed_value() -> XLOPER12Value {
        // SAFETY: every union member consists only of integer or raw-pointer
        // fields, for which the all-zero bit pattern is valid.
        unsafe { core::mem::zeroed() }
    }

    #[test]
    fn decodes_scalar_tags_and_masks_ownership_bits() {
        let number = raw(
            XLOPER12Value { num: 2.5 },
            xltypeNum | xlbitXLFree | xlbitDLLFree,
        );
        assert!(matches!(decode(&number), Ok(ExcelValueRef::Number(2.5))));

        let boolean = raw(XLOPER12Value { xbool: -1 }, xltypeBool);
        assert!(matches!(decode(&boolean), Ok(ExcelValueRef::Boolean(true))));

        let integer = raw(XLOPER12Value { w: -42 }, xltypeInt);
        assert!(matches!(decode(&integer), Ok(ExcelValueRef::Integer(-42))));

        for (raw_error, expected) in [
            (excel_api_sys::xlerrNull, ExcelError::Null),
            (excel_api_sys::xlerrDiv0, ExcelError::Div0),
            (excel_api_sys::xlerrValue, ExcelError::Value),
            (excel_api_sys::xlerrRef, ExcelError::Ref),
            (excel_api_sys::xlerrName, ExcelError::Name),
            (excel_api_sys::xlerrNum, ExcelError::Num),
            (excel_api_sys::xlerrNA, ExcelError::Na),
            (excel_api_sys::xlerrGettingData, ExcelError::GettingData),
        ] {
            let error = raw(XLOPER12Value { err: raw_error }, xltypeErr);
            let Ok(ExcelValueRef::Error(actual)) = decode(&error) else {
                panic!("expected error value");
            };
            assert_eq!(actual, expected);
        }
    }

    #[test]
    fn missing_and_nil_are_independent() {
        let missing = raw(XLOPER12Value { w: 0 }, xltypeMissing);
        let nil = raw(XLOPER12Value { w: 0 }, xltypeNil);
        assert!(matches!(decode(&missing), Ok(ExcelValueRef::Missing(_))));
        assert!(matches!(decode(&nil), Ok(ExcelValueRef::Nil(_))));
    }

    #[test]
    fn parses_all_three_string_abis_without_conversion() {
        let mut xloper_text = [3, b'A' as u16, 0, 0xD800];
        let value = raw(
            XLOPER12Value {
                str: xloper_text.as_mut_ptr(),
            },
            xltypeStr,
        );
        let Ok(ExcelValueRef::Text(text)) = decode(&value) else {
            panic!("expected text");
        };
        assert_eq!(text.as_utf16(), &[b'A' as u16, 0, 0xD800]);

        let counted = [2, 0xD800, b'Z' as u16];
        // SAFETY: `counted` contains its prefix and complete payload.
        let direct = unsafe { ExcelStr::from_counted_direct(&counted[0]) }.unwrap();
        assert_eq!(direct.as_utf16(), &[0xD800, b'Z' as u16]);

        let terminated = [b'X' as u16, b'Y' as u16, 0, b'Z' as u16];
        // SAFETY: the fixture contains a terminator within the bounded scan.
        let direct = unsafe { ExcelStr::from_null_terminated_direct(&terminated[0]) }.unwrap();
        assert_eq!(direct.as_utf16(), &[b'X' as u16, b'Y' as u16]);

        let mut empty = [0_u16];
        let value = raw(
            XLOPER12Value {
                str: empty.as_mut_ptr(),
            },
            xltypeStr,
        );
        let Ok(ExcelValueRef::Text(empty)) = decode(&value) else {
            panic!("expected empty text");
        };
        assert!(empty.is_empty());
        assert_eq!(empty.as_utf16(), &[]);
    }

    #[test]
    fn rejects_malformed_strings() {
        let mut too_long = [u16::MAX, 0];
        let value = raw(
            XLOPER12Value {
                str: too_long.as_mut_ptr(),
            },
            xltypeStr,
        );
        assert!(matches!(
            decode(&value),
            Err(DecodeError::StringTooLong(65_535))
        ));

        let null = raw(
            XLOPER12Value {
                str: ptr::null_mut(),
            },
            xltypeStr,
        );
        assert!(matches!(decode(&null), Err(DecodeError::NullStringPointer)));

        let unterminated = [1_u16; EXCEL12_MAX_STRING_CODE_UNITS + 1];
        // SAFETY: the entire bounded scan range is initialized.
        let result = unsafe { ExcelStr::from_null_terminated_direct(&unterminated[0]) };
        assert!(matches!(result, Err(DecodeError::UnterminatedString)));
    }

    #[test]
    fn array_supports_indexing_rows_and_columns() {
        let mut elements = [
            raw(XLOPER12Value { num: 1.0 }, xltypeNum),
            raw(XLOPER12Value { num: 2.0 }, xltypeNum),
            raw(XLOPER12Value { num: 3.0 }, xltypeNum),
            raw(XLOPER12Value { num: 4.0 }, xltypeNum),
        ];
        let value = raw(
            XLOPER12Value {
                array: XLOPER12Array {
                    lparray: elements.as_mut_ptr(),
                    rows: 2,
                    columns: 2,
                },
            },
            xltypeMulti,
        );
        let Ok(ExcelValueRef::Array(array)) = decode(&value) else {
            panic!("expected array");
        };
        assert_eq!(array.dimensions(), (2, 2));
        assert!(matches!(
            array.get(1, 0),
            Ok(Some(ExcelValueRef::Number(3.0)))
        ));
        assert!(matches!(array.get(2, 0), Ok(None)));

        let second_row: Vec<_> = array.row(1).unwrap().collect();
        assert!(matches!(second_row[0], Ok(ExcelValueRef::Number(3.0))));
        assert!(matches!(second_row[1], Ok(ExcelValueRef::Number(4.0))));

        let second_column: Vec<_> = array.column(1).unwrap().collect();
        assert!(matches!(second_column[0], Ok(ExcelValueRef::Number(2.0))));
        assert!(matches!(second_column[1], Ok(ExcelValueRef::Number(4.0))));
        assert_eq!(array.rows().len(), 2);
        assert_eq!(array.columns().len(), 2);
    }

    #[test]
    fn rejects_malformed_array_dimensions_and_pointer() {
        for (rows, columns) in [(0, 1), (1, 0), (-1, 1), (1, -1), (EXCEL12_MAX_ROWS + 1, 1)] {
            let value = raw(
                XLOPER12Value {
                    array: XLOPER12Array {
                        lparray: ptr::null_mut(),
                        rows,
                        columns,
                    },
                },
                xltypeMulti,
            );
            assert!(matches!(
                decode(&value),
                Err(DecodeError::MalformedArrayDimensions {
                    rows: actual_rows,
                    columns: actual_columns,
                }) if actual_rows == rows && actual_columns == columns
            ));
        }

        let value = raw(
            XLOPER12Value {
                array: XLOPER12Array {
                    lparray: ptr::null_mut(),
                    rows: 1,
                    columns: 1,
                },
            },
            xltypeMulti,
        );
        assert!(matches!(decode(&value), Err(DecodeError::NullArrayPointer)));
    }

    #[test]
    fn rejects_nested_arrays_and_references_in_arrays() {
        let mut nested = raw(
            XLOPER12Value {
                array: XLOPER12Array {
                    lparray: ptr::null_mut(),
                    rows: 1,
                    columns: 1,
                },
            },
            xltypeMulti,
        );
        let outer = raw(
            XLOPER12Value {
                array: XLOPER12Array {
                    lparray: &mut nested,
                    rows: 1,
                    columns: 1,
                },
            },
            xltypeMulti,
        );
        assert!(matches!(decode(&outer), Err(DecodeError::NestedArray)));

        let mut reference = raw(
            XLOPER12Value {
                sref: XLOPER12SRef {
                    count: 1,
                    reference: XLREF12::default(),
                },
            },
            xltypeSRef,
        );
        let outer = raw(
            XLOPER12Value {
                array: XLOPER12Array {
                    lparray: &mut reference,
                    rows: 1,
                    columns: 1,
                },
            },
            xltypeMulti,
        );
        assert!(matches!(decode(&outer), Err(DecodeError::ReferenceInArray)));
    }

    #[test]
    fn represents_single_and_multi_references_distinctly() {
        let single = raw(
            XLOPER12Value {
                sref: XLOPER12SRef {
                    count: 1,
                    reference: XLREF12 {
                        rwFirst: 1,
                        rwLast: 2,
                        colFirst: 3,
                        colLast: 4,
                    },
                },
            },
            xltypeSRef,
        );
        let Ok(ExcelValueRef::Reference(ExcelReference::Single(single))) = decode(&single) else {
            panic!("expected single reference");
        };
        assert_eq!(single.area().first_row(), 1);
        assert_eq!(single.area().last_column(), 4);

        let mut mref = XLMREF12 {
            count: 1,
            reftbl: [XLREF12 {
                rwFirst: 5,
                rwLast: 6,
                colFirst: 7,
                colLast: 8,
            }],
        };
        let multiple = raw(
            XLOPER12Value {
                mref: XLOPER12MRef {
                    lpmref: &mut mref,
                    idSheet: 99,
                },
            },
            xltypeRef,
        );
        let Ok(ExcelValueRef::Reference(ExcelReference::Multiple(multiple))) = decode(&multiple)
        else {
            panic!("expected multi reference");
        };
        assert_eq!(multiple.sheet_id(), 99);
        assert_eq!(multiple.area_count(), 1);
        assert_eq!(multiple.areas().next().unwrap().first_column(), 7);
    }

    #[test]
    fn multi_reference_exposes_every_sdk_tail_area() {
        #[repr(C)]
        struct TwoAreaReference {
            count: u16,
            padding: u16,
            areas: [XLREF12; 2],
        }

        let mut storage = TwoAreaReference {
            count: 2,
            padding: 0,
            areas: [
                XLREF12 {
                    rwFirst: 1,
                    rwLast: 1,
                    colFirst: 2,
                    colLast: 2,
                },
                XLREF12 {
                    rwFirst: 3,
                    rwLast: 4,
                    colFirst: 5,
                    colLast: 6,
                },
            ],
        };
        let value = raw(
            XLOPER12Value {
                mref: XLOPER12MRef {
                    lpmref: (&mut storage as *mut TwoAreaReference).cast::<XLMREF12>(),
                    idSheet: 7,
                },
            },
            xltypeRef,
        );
        let Ok(ExcelValueRef::Reference(ExcelReference::Multiple(reference))) = decode(&value)
        else {
            panic!("expected multi reference");
        };
        assert_eq!(reference.area_count(), 2);
        assert_eq!(reference.area(0).unwrap().first_row(), 1);
        assert_eq!(reference.area(1).unwrap().last_column(), 6);
        assert!(reference.area(2).is_none());
    }

    #[test]
    fn validates_reference_shapes() {
        let invalid_count = raw(
            XLOPER12Value {
                sref: XLOPER12SRef {
                    count: 2,
                    reference: XLREF12::default(),
                },
            },
            xltypeSRef,
        );
        assert!(matches!(
            decode(&invalid_count),
            Err(DecodeError::InvalidSingleReferenceCount(2))
        ));

        let invalid_area = raw(
            XLOPER12Value {
                sref: XLOPER12SRef {
                    count: 1,
                    reference: XLREF12 {
                        rwFirst: 2,
                        rwLast: 1,
                        colFirst: 0,
                        colLast: 0,
                    },
                },
            },
            xltypeSRef,
        );
        assert!(matches!(
            decode(&invalid_area),
            Err(DecodeError::MalformedReferenceArea)
        ));
    }

    #[test]
    fn every_sdk_type_is_decoded_or_explicitly_rejected() {
        let flow = raw(XLOPER12Value { w: 0 }, xltypeFlow);
        assert!(matches!(
            decode(&flow),
            Err(DecodeError::UnsupportedType(value)) if value == xltypeFlow
        ));

        let big_data = raw(
            XLOPER12Value {
                bigdata: XLOPER12BigData {
                    h: XLOPER12BigDataHandle {
                        hdata: ptr::null_mut::<c_void>(),
                    },
                    cbData: 0,
                },
            },
            xltypeBigData,
        );
        assert!(matches!(
            decode(&big_data),
            Err(DecodeError::UnsupportedType(value)) if value == xltypeBigData
        ));

        let invalid_error = raw(XLOPER12Value { err: -1 }, xltypeErr);
        assert!(matches!(
            decode(&invalid_error),
            Err(DecodeError::InvalidError(-1))
        ));
    }

    #[test]
    fn malformed_tags_are_not_interpreted_as_union_members() {
        for xltype in [
            0,
            xltypeNum | xltypeBool,
            xlbitXLFree,
            xlbitDLLFree,
            0x2000,
            0x8000_0000 | xltypeNum,
        ] {
            let value = raw(zeroed_value(), xltype);
            assert!(matches!(decode(&value), Err(DecodeError::MalformedType(_))));
        }
    }

    #[test]
    fn malformed_type_fuzz_never_panics() {
        for xltype in 0_u32..=u16::MAX as u32 {
            // A zeroed union is a valid raw-pointer/integer bit pattern. Any
            // pointer-bearing recognized tag is rejected before dereference.
            let value = raw(zeroed_value(), xltype);
            let _ = decode(&value);
        }
        for xltype in [u32::MAX, 0x8000_0001, 0x0002_0000, 0xDEAD_BEEF] {
            let value = raw(zeroed_value(), xltype);
            let _ = decode(&value);
        }
    }
}