beve 8.0.0

Rust implementation of the BEVE (Binary Efficient Versatile Encoding) specification with serde support
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
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
//! BEVE -> MATLAB v7.3 conversion.
//!
//! Requires the `mat` feature, which is off by default:
//! `beve = { version = "8", features = ["mat"] }`.
//!
//! Walks the BEVE wire format and emits MATLAB-equivalent values directly
//! through `hdf5_pure::mat::MatBuilder`. No intermediate value tree is
//! materialized: the BEVE reader and the MAT builder advance in lockstep.

use std::ffi::OsString;
use std::fmt::Write as FmtWrite;
use std::fs::OpenOptions;
use std::io::Write as IoWrite;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicU64, Ordering};

use half::{bf16, f16};
use hdf5_pure::mat::{self as mat_pure, CellWriter, MatBuilder};
use serde::{Deserialize, Serialize};

use crate::error::{Error, Result};
use crate::ext::MatrixLayout;
use crate::header::*;
use crate::raw::{ComplexHeader, ObjectHeader, Reader, TypedArrayClass};

/// Byte codes for the two 2-byte encodings in the BEVE float class. Named so
/// the half-precision paths key on the encoding rather than on a bare literal.
const ARRAY_FLOAT_BF16_CODE: u8 = 0;
const ARRAY_FLOAT_F16_CODE: u8 = 1;

// Re-export shared option enums from hdf5-pure so beve users keep using them
// under the same names.
pub use hdf5_pure::LibVer;
pub use hdf5_pure::mat::{
    Compression, EmptyMarkerEncoding, InvalidNamePolicy, NullPolicy, OneDimensionalMode,
    RowMajorPolicy, StringClass, UnsupportedPolicy,
};

/// Controls how the BEVE root value is bound into MATLAB workspace variables.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum RootBinding<'a> {
    /// Convert the BEVE root into a single MATLAB variable with the provided name.
    NamedVariable(&'a str),
    /// Require a string-keyed BEVE object and expand each top-level entry into a
    /// separate MATLAB workspace variable.
    WorkspaceObject,
}

/// Options for BEVE -> MATLAB v7.3 conversion.
///
/// Convenience wrapper over [`hdf5_pure::mat::Options`] that pins
/// `string_class = String` (the BEVE historical default, and what real
/// MATLAB's `save -v7.3` produces). The remaining policies mirror upstream.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct MatV73Options {
    pub compression: Compression,
    pub invalid_name_policy: InvalidNamePolicy,
    pub null_policy: NullPolicy,
    pub unsupported_policy: UnsupportedPolicy,
    pub one_dimensional_mode: OneDimensionalMode,
    pub row_major_policy: RowMajorPolicy,
    /// The newest HDF5 on-disk format the MAT file may use.
    ///
    /// Defaults to [`LibVer::V18`], the newest format every MATLAB release can
    /// open: a version 3 superblock is an HDF5 1.10 addition, and MATLAB's MAT
    /// v7.3 loader refuses one even on releases whose own libhdf5 reads it
    /// without difficulty.
    ///
    /// [`LibVer::V110`] is what [`Compression`] needs, since compression
    /// requires chunked storage and the chunk indices hdf5-pure writes arrived
    /// in 1.10. Compression against a lower bound is refused rather than
    /// resolved either way, so raise this deliberately and accept that MATLAB
    /// will not `load` the result.
    pub libver: LibVer,
}

impl Default for MatV73Options {
    fn default() -> Self {
        Self {
            compression: Compression::None,
            invalid_name_policy: InvalidNamePolicy::Error,
            null_policy: NullPolicy::EmptyStructArray,
            unsupported_policy: UnsupportedPolicy::Error,
            one_dimensional_mode: OneDimensionalMode::ColumnVector,
            row_major_policy: RowMajorPolicy::ReorderToColumnMajor,
            libver: LibVer::V18,
        }
    }
}

impl MatV73Options {
    /// Convert to the underlying hdf5-pure options. Always pins
    /// `string_class = String`.
    ///
    /// `unit_variant_encoding` and `empty_sequence_policy` are deliberately not
    /// mirrored: hdf5-pure consults them only from its serde writer, to recover
    /// type information that serde withholds. BEVE carries that information in
    /// the document, so this walker reads the answer rather than choosing it.
    fn to_pure(&self) -> mat_pure::Options {
        let mut opts = mat_pure::Options::with_modern_strings();
        opts.compression = self.compression;
        opts.invalid_name_policy = self.invalid_name_policy;
        opts.null_policy = self.null_policy;
        opts.unsupported_policy = self.unsupported_policy;
        opts.one_dimensional_mode = self.one_dimensional_mode;
        opts.row_major_policy = self.row_major_policy;
        opts.libver = self.libver;
        opts
    }
}

// ---------------------------------------------------------------------------
// Public API
// ---------------------------------------------------------------------------

/// Convert a BEVE payload into a MATLAB v7.3 MAT file. Stages output in a
/// temp file next to the destination and only replaces the target after the
/// MAT file is fully written.
///
/// The MAT file is streamed into the temp file rather than assembled in
/// memory first, so converting costs the payload plus one dataset rather than
/// the payload plus the whole output.
pub fn beve_slice_to_mat_v73_file<P: AsRef<Path>>(
    beve: &[u8],
    output_path: P,
    root: RootBinding<'_>,
    options: &MatV73Options,
) -> Result<()> {
    let output_path = output_path.as_ref();
    let temp_output = TempOutputFile::new(output_path)?;
    {
        let file = OpenOptions::new()
            .write(true)
            .truncate(true)
            .open(temp_output.path())
            .map_err(|e| Error::msg(e.to_string()))?;
        let mut sink = std::io::BufWriter::new(file);
        beve_slice_to_mat_v73_writer(beve, &mut sink, root, options)?;
        sink.flush().map_err(|e| Error::msg(e.to_string()))?;
    }
    sync_regular_file(temp_output.path())?;
    temp_output.persist(output_path)
}

/// Convert a BEVE payload to MATLAB v7.3 MAT file bytes in memory.
///
/// Prefer [`beve_slice_to_mat_v73_writer`] when the output is headed for a
/// file or a socket: it produces the same bytes without ever holding the
/// assembled file.
pub fn beve_slice_to_mat_v73_bytes(
    beve: &[u8],
    root: RootBinding<'_>,
    options: &MatV73Options,
) -> Result<Vec<u8>> {
    build_mat_v73(beve, root, options)?
        .finish()
        .map_err(map_mat_error)
}

/// Convert a BEVE payload into a MATLAB v7.3 MAT file written front-to-back
/// onto `w`.
///
/// Produces byte-for-byte what [`beve_slice_to_mat_v73_bytes`] returns, but
/// never holds the assembled file, so the output size stops bounding what can
/// be converted. The sink is never seeked and so may be a socket as readily as
/// a file.
///
/// A failure partway leaves whatever was already written on `w`. A caller that
/// needs all-or-nothing should write to a temporary path and rename on success,
/// which is what [`beve_slice_to_mat_v73_file`] does.
pub fn beve_slice_to_mat_v73_writer<W: std::io::Write>(
    beve: &[u8],
    w: W,
    root: RootBinding<'_>,
    options: &MatV73Options,
) -> Result<()> {
    build_mat_v73(beve, root, options)?
        .finish_to(w)
        .map_err(map_mat_error)
}

/// Walk `beve` into a builder, leaving the choice of exit to the caller. The
/// two public conversions differ only in how they finish, so the walk lives
/// here rather than being duplicated and left to drift.
fn build_mat_v73(
    beve: &[u8],
    root: RootBinding<'_>,
    options: &MatV73Options,
) -> Result<MatBuilder> {
    let mut mb = MatBuilder::new(options.to_pure());
    let mut reader = Reader::new(beve);
    walk_root(&mut reader, &mut mb, options, root)?;
    if !reader.is_finished() {
        return Err(Error::InvalidType("unexpected trailing BEVE data"));
    }
    Ok(mb)
}

/// Read a `.beve` file from disk and write a MATLAB v7.3 MAT file.
pub fn beve_file_to_mat_v73_file<I: AsRef<Path>, O: AsRef<Path>>(
    input_path: I,
    output_path: O,
    root: RootBinding<'_>,
    options: &MatV73Options,
) -> Result<()> {
    let bytes = std::fs::read(input_path).map_err(|e| Error::msg(e.to_string()))?;
    beve_slice_to_mat_v73_file(&bytes, output_path, root, options)
}

// ---------------------------------------------------------------------------
// Bulk typed-array readers
//
// BEVE typed numeric arrays are LE-packed. Reading the whole region as `&[u8]`
// once and chunking is significantly faster than the per-element reader calls,
// which each re-check bounds and update `pos`. Conversion is host-endian
// independent via `from_le_bytes`.
// ---------------------------------------------------------------------------

#[inline]
fn read_f32_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<f32>> {
    let bytes = reader.read_exact(len * 4)?;
    let mut out = Vec::with_capacity(len);
    for chunk in bytes.as_chunks::<4>().0 {
        out.push(f32::from_le_bytes(*chunk));
    }
    Ok(out)
}

#[inline]
fn read_f64_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<f64>> {
    let bytes = reader.read_exact(len * 8)?;
    let mut out = Vec::with_capacity(len);
    for chunk in bytes.as_chunks::<8>().0 {
        out.push(f64::from_le_bytes(*chunk));
    }
    Ok(out)
}

#[inline]
fn read_i8_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<i8>> {
    let bytes = reader.read_exact(len)?;
    Ok(bytes.iter().map(|&b| b as i8).collect())
}

#[inline]
fn read_i16_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<i16>> {
    let bytes = reader.read_exact(len * 2)?;
    let mut out = Vec::with_capacity(len);
    for chunk in bytes.as_chunks::<2>().0 {
        out.push(i16::from_le_bytes(*chunk));
    }
    Ok(out)
}

#[inline]
fn read_i32_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<i32>> {
    let bytes = reader.read_exact(len * 4)?;
    let mut out = Vec::with_capacity(len);
    for chunk in bytes.as_chunks::<4>().0 {
        out.push(i32::from_le_bytes(*chunk));
    }
    Ok(out)
}

#[inline]
fn read_i64_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<i64>> {
    let bytes = reader.read_exact(len * 8)?;
    let mut out = Vec::with_capacity(len);
    for chunk in bytes.as_chunks::<8>().0 {
        out.push(i64::from_le_bytes(*chunk));
    }
    Ok(out)
}

#[inline]
fn read_u8_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<u8>> {
    Ok(reader.read_exact(len)?.to_vec())
}

#[inline]
fn read_u16_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<u16>> {
    let bytes = reader.read_exact(len * 2)?;
    let mut out = Vec::with_capacity(len);
    for chunk in bytes.as_chunks::<2>().0 {
        out.push(u16::from_le_bytes(*chunk));
    }
    Ok(out)
}

#[inline]
fn read_u32_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<u32>> {
    let bytes = reader.read_exact(len * 4)?;
    let mut out = Vec::with_capacity(len);
    for chunk in bytes.as_chunks::<4>().0 {
        out.push(u32::from_le_bytes(*chunk));
    }
    Ok(out)
}

#[inline]
fn read_u64_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<u64>> {
    let bytes = reader.read_exact(len * 8)?;
    let mut out = Vec::with_capacity(len);
    for chunk in bytes.as_chunks::<8>().0 {
        out.push(u64::from_le_bytes(*chunk));
    }
    Ok(out)
}

/// A BEVE complex array is LE-packed interleaved re/im pairs of the element
/// scalar, so one bulk read plus chunking covers every width. The element type
/// is fixed by the complex header's (class, byte_code), not by the array.
macro_rules! read_complex_array_fn {
    ($name:ident, $ty:ty) => {
        #[inline]
        fn $name(reader: &mut Reader<'_>, len: usize) -> Result<Vec<($ty, $ty)>> {
            const ELEM: usize = core::mem::size_of::<$ty>();
            const PAIR: usize = ELEM * 2;
            let byte_len = len.checked_mul(PAIR).ok_or(Error::InvalidSize)?;
            let bytes = reader.read_exact(byte_len)?;
            let mut out = Vec::with_capacity(len);
            for chunk in bytes.as_chunks::<PAIR>().0 {
                let (re_bytes, im_bytes) = chunk.split_at(ELEM);
                let re = <$ty>::from_le_bytes(re_bytes.try_into().unwrap());
                let im = <$ty>::from_le_bytes(im_bytes.try_into().unwrap());
                out.push((re, im));
            }
            Ok(out)
        }
    };
}

read_complex_array_fn!(read_complex_f32_array, f32);
read_complex_array_fn!(read_complex_f64_array, f64);
read_complex_array_fn!(read_complex_i8_array, i8);
read_complex_array_fn!(read_complex_i16_array, i16);
read_complex_array_fn!(read_complex_i32_array, i32);
read_complex_array_fn!(read_complex_i64_array, i64);
read_complex_array_fn!(read_complex_u8_array, u8);
read_complex_array_fn!(read_complex_u16_array, u16);
read_complex_array_fn!(read_complex_u32_array, u32);
read_complex_array_fn!(read_complex_u64_array, u64);

/// Half-precision complex arrays widen to `f32` pairs, matching how the real
/// f16/bf16 typed arrays are handled. Gated by the caller on
/// [`UnsupportedPolicy::LossyNumericWidening`].
///
/// Reads the whole region up front like the fixed-width readers above, so a
/// wire-supplied `len` is bounded by the remaining input before anything is
/// allocated. Reserving first would let a hostile SIZE field request an
/// arbitrary allocation from a payload only a few bytes long.
#[inline]
fn read_complex_half_array(
    reader: &mut Reader<'_>,
    len: usize,
    byte_code: u8,
) -> Result<Vec<(f32, f32)>> {
    // Both f16 and bf16 are 2 bytes per component, 4 per complex element.
    let byte_len = len.checked_mul(4).ok_or(Error::InvalidSize)?;
    let bytes = reader.read_exact(byte_len)?;
    let widen: fn(u16) -> f32 = if byte_code == ARRAY_FLOAT_BF16_CODE {
        |bits| bf16::from_bits(bits).to_f32()
    } else {
        |bits| f16::from_bits(bits).to_f32()
    };
    let mut out = Vec::with_capacity(len);
    for chunk in bytes.as_chunks::<4>().0 {
        let (re_bytes, im_bytes) = chunk.split_at(2);
        let re = widen(u16::from_le_bytes(re_bytes.try_into().unwrap()));
        let im = widen(u16::from_le_bytes(im_bytes.try_into().unwrap()));
        out.push((re, im));
    }
    Ok(out)
}

// ---------------------------------------------------------------------------
// Walker
//
// Routes through `&mut MatBuilder`. The builder's internal scope stack
// (open structs + cell-element pending refs) ensures `mb.write_*` /
// `mb.struct_` / `mb.cell` calls land in the right place regardless of how
// deep the recursion is.
//
// `path` is a single mutable `String` reused for the whole walk: each step
// pushes its segment, recurses, and truncates back to the prefix. Errors
// observed at any depth capture the path in their message at that moment, so
// the unwinding path doesn't need to restore.
// ---------------------------------------------------------------------------

fn walk_root(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    root: RootBinding<'_>,
) -> Result<()> {
    let mut path = String::with_capacity(64);
    path.push('$');
    match root {
        RootBinding::NamedVariable(name) => walk_value(reader, mb, options, name, &mut path),
        RootBinding::WorkspaceObject => {
            let header = reader.read_header()?;
            let object = reader.read_object_header(header)?;
            if object.key_type != KEY_STRING {
                return Err(Error::Unsupported(
                    "workspace root objects must use string keys",
                ));
            }
            for _ in 0..object.len {
                let key = reader.read_string()?.to_owned();
                let prefix_len = path.len();
                path.push('.');
                path.push_str(&key);
                walk_value(reader, mb, options, &key, &mut path)?;
                path.truncate(prefix_len);
            }
            Ok(())
        }
    }
}

/// Walk one BEVE value into the current MatBuilder scope.
fn walk_value(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    path: &mut String,
) -> Result<()> {
    let header = reader.read_header()?;
    walk_value_with_header(reader, mb, options, name, header, path)
}

fn walk_value_with_header(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    header: u8,
    path: &mut String,
) -> Result<()> {
    match parse_type(header) {
        TYPE_NULL_BOOL => {
            if header == 0 {
                handle_null(mb, options, name, path)
            } else {
                let v = bool_value(header)?;
                mb.write_scalar_logical(name, v).map_err(map_mat_error)?;
                Ok(())
            }
        }
        TYPE_NUMBER => walk_number(reader, mb, options, name, header, path),
        TYPE_STRING => {
            let value = reader.read_string()?.to_owned();
            mb.write_string_object(name, &[value], &[1, 1])
                .map_err(map_mat_error)?;
            Ok(())
        }
        TYPE_OBJECT => walk_object(reader, mb, options, name, header, path),
        TYPE_TYPED_ARRAY => walk_typed_array(reader, mb, options, name, header, path),
        TYPE_GENERIC_ARRAY => walk_generic_array(reader, mb, options, name, path),
        TYPE_EXTENSION => walk_extension(reader, mb, options, name, header, path),
        _ => Err(Error::InvalidHeader(header)),
    }
}

fn handle_null(mb: &mut MatBuilder, options: &MatV73Options, name: &str, path: &str) -> Result<()> {
    match options.null_policy {
        NullPolicy::EmptyStructArray => mb
            .write_empty_struct_array(name)
            .map(|_| ())
            .map_err(map_mat_error),
        // Write nothing at all: `walk_object` builds the struct's field list
        // from what the builder actually received, so skipping the write is
        // what drops the field.
        NullPolicy::Omit => Ok(()),
        NullPolicy::Error => Err(Error::msg(format!("unsupported null value at {path}"))),
        _ => Err(Error::Unsupported("unrecognized null policy")),
    }
}

fn walk_number(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    header: u8,
    path: &str,
) -> Result<()> {
    let subtype = parse_subtype(header);
    let byte_code = parse_byte_count_code(header);
    match subtype {
        NUM_SIGNED => match byte_code {
            0 => {
                let v = reader.read_signed(0)? as i8;
                mb.write_scalar_i8(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            1 => {
                let v = reader.read_signed(1)? as i16;
                mb.write_scalar_i16(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            2 => {
                let v = reader.read_signed(2)? as i32;
                mb.write_scalar_i32(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            3 => {
                let v = reader.read_signed(3)? as i64;
                mb.write_scalar_i64(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            4 => handle_128_bit(mb, options, name, path, reader.read_signed(4)?.to_string()),
            _ => Err(Error::InvalidHeader(header)),
        },
        NUM_UNSIGNED => match byte_code {
            0 => {
                let v = reader.read_unsigned(0)? as u8;
                mb.write_scalar_u8(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            1 => {
                let v = reader.read_unsigned(1)? as u16;
                mb.write_scalar_u16(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            2 => {
                let v = reader.read_unsigned(2)? as u32;
                mb.write_scalar_u32(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            3 => {
                let v = reader.read_unsigned(3)? as u64;
                mb.write_scalar_u64(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            4 => handle_128_bit(
                mb,
                options,
                name,
                path,
                reader.read_unsigned(4)?.to_string(),
            ),
            _ => Err(Error::InvalidHeader(header)),
        },
        NUM_FLOAT => match byte_code {
            0 => {
                check_low_precision_float(options, "bf16 scalar", path)?;
                let v = reader.read_bf16_as_f32()?;
                mb.write_scalar_f32(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            1 => {
                check_low_precision_float(options, "f16 scalar", path)?;
                let v = reader.read_f16_as_f32()?;
                mb.write_scalar_f32(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            2 => {
                let v = reader.read_f32()?;
                mb.write_scalar_f32(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            3 => {
                let v = reader.read_f64()?;
                mb.write_scalar_f64(name, v).map_err(map_mat_error)?;
                Ok(())
            }
            _ => Err(Error::Unsupported("float128 is not supported")),
        },
        _ => Err(Error::InvalidHeader(header)),
    }
}

fn handle_128_bit(
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    path: &str,
    value: String,
) -> Result<()> {
    match options.unsupported_policy {
        UnsupportedPolicy::StringFallback => mb
            .write_string_object(name, &[value], &[1, 1])
            .map(|_| ())
            .map_err(map_mat_error),
        _ => Err(Error::msg(format!(
            "unsupported 128-bit integer scalar at {path}"
        ))),
    }
}

fn walk_object(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    header: u8,
    path: &mut String,
) -> Result<()> {
    let ObjectHeader {
        key_type,
        byte_code,
        len,
    } = reader.read_object_header(header)?;
    // No walker-side dedup: MatBuilder enforces uniqueness inside the open
    // struct group, so adding our own BTreeSet/HashSet here just doubles the
    // work. The builder also handles validation/sanitization per the
    // configured policy.
    mb.struct_(name, |sw| {
        let inner_mb = sw.builder();
        for _ in 0..len {
            let key: String = match key_type {
                KEY_STRING => reader.read_string().map_err(map_beve_error)?.to_owned(),
                KEY_SIGNED => reader
                    .read_signed(byte_code)
                    .map_err(map_beve_error)?
                    .to_string(),
                KEY_UNSIGNED => reader
                    .read_unsigned(byte_code)
                    .map_err(map_beve_error)?
                    .to_string(),
                _ => return Err(map_beve_error(Error::InvalidHeader(header))),
            };
            let prefix_len = path.len();
            path.push('.');
            path.push_str(&key);
            walk_value(reader, inner_mb, options, &key, path).map_err(map_beve_error)?;
            path.truncate(prefix_len);
        }
        Ok(())
    })
    .map_err(map_mat_error)?;
    Ok(())
}

fn walk_typed_array(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    header: u8,
    path: &str,
) -> Result<()> {
    let info = reader.read_typed_array_header(header)?;
    let dims = mb.vector_dims(info.len);
    match info.class {
        TypedArrayClass::Float => write_float_array(
            reader,
            mb,
            options,
            name,
            &dims,
            info.byte_code,
            info.len,
            path,
        ),
        TypedArrayClass::Signed => {
            write_signed_array(reader, mb, name, &dims, info.byte_code, info.len, path)
        }
        TypedArrayClass::Unsigned => {
            write_unsigned_array(reader, mb, name, &dims, info.byte_code, info.len, path)
        }
        TypedArrayClass::Bool => {
            let bytes = read_bool_array(reader, info.len)?;
            mb.write_logical(name, &dims, &bytes)
                .map_err(map_mat_error)?;
            Ok(())
        }
        TypedArrayClass::String => {
            let values = read_string_array(reader, info.len)?;
            mb.write_string_object(name, &values, &dims)
                .map_err(map_mat_error)?;
            Ok(())
        }
    }
}

fn walk_generic_array(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    path: &mut String,
) -> Result<()> {
    let len = reader.read_size()?;
    let dims = mb.vector_dims(len);
    if len == 0 {
        mb.write_empty(name, mat_pure::MatClass::Cell, &dims)
            .map_err(map_mat_error)?;
        return Ok(());
    }
    mb.cell(name, &dims, |cw| {
        for idx in 0..len {
            let prefix_len = path.len();
            // String's std::fmt::Write impl is infallible.
            let _ = write!(path, "[{idx}]");
            walk_value_at_cell_element(reader, cw, options, path).map_err(map_beve_error)?;
            path.truncate(prefix_len);
        }
        Ok(())
    })
    .map_err(map_mat_error)?;
    Ok(())
}

fn walk_extension(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    header: u8,
    path: &str,
) -> Result<()> {
    match parse_extension_id(header) {
        EXT_COMPLEX => walk_complex(reader, mb, options, name, path),
        EXT_MATRICES => walk_matrix(reader, mb, options, name, path),
        _ => Err(Error::msg(format!(
            "unsupported BEVE extension {:#x} at {path}",
            parse_extension_id(header)
        ))),
    }
}

fn walk_complex(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    path: &str,
) -> Result<()> {
    let header = reader.read_complex_header()?;
    if header.is_array {
        let len = reader.read_size()?;
        let dims = mb.vector_dims(len);
        write_complex_array(reader, mb, options, name, &dims, header, len, None, path)
    } else {
        // A complex scalar's payload is the same LE-packed re/im pair a
        // one-element array carries, so the array path covers it directly.
        write_complex_array(reader, mb, options, name, &[1, 1], header, 1, None, path)
    }
}

/// Read a BEVE complex payload of `len` elements and write it as a MATLAB
/// complex array. The element class comes from the complex header rather than
/// the surrounding context, so MATLAB receives an `int16` complex array for an
/// `int16` complex payload and a `single` one for an `f32` payload. Nothing
/// here widens an integer to a float: MATLAB has first-class complex integer
/// arrays, and silently promoting would make the file misreport what was
/// captured.
///
/// `reorder` is `Some((layout, extents))` for matrix payloads, which may need
/// row-major to column-major reordering, and `None` for plain arrays and
/// scalars, which are already in the order MATLAB expects.
#[allow(clippy::too_many_arguments)]
fn write_complex_array(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    dims: &[usize],
    header: ComplexHeader,
    len: usize,
    reorder: Option<(MatrixLayout, &[usize])>,
    path: &str,
) -> Result<()> {
    macro_rules! emit {
        ($read:ident, $write:ident) => {{
            let data = $read(reader, len)?;
            let data = maybe_reorder_complex(data, reorder, options, path)?;
            mb.$write(name, dims, &data).map_err(map_mat_error)?;
            Ok(())
        }};
    }

    match (header.class, header.byte_code) {
        (ARRAY_FLOAT, ARRAY_FLOAT_BF16_CODE | ARRAY_FLOAT_F16_CODE) => {
            // Match the split labels the real f16/bf16 paths use, so the error
            // names the encoding rather than just "half-precision".
            let label = if header.byte_code == ARRAY_FLOAT_BF16_CODE {
                "bf16 complex"
            } else {
                "f16 complex"
            };
            check_low_precision_float(options, label, path)?;
            let data = read_complex_half_array(reader, len, header.byte_code)?;
            let data = maybe_reorder_complex(data, reorder, options, path)?;
            mb.write_complex_f32(name, dims, &data)
                .map_err(map_mat_error)?;
            Ok(())
        }
        (ARRAY_FLOAT, 2) => emit!(read_complex_f32_array, write_complex_f32),
        (ARRAY_FLOAT, 3) => emit!(read_complex_f64_array, write_complex_f64),
        (ARRAY_SIGNED, 0) => emit!(read_complex_i8_array, write_complex_i8),
        (ARRAY_SIGNED, 1) => emit!(read_complex_i16_array, write_complex_i16),
        (ARRAY_SIGNED, 2) => emit!(read_complex_i32_array, write_complex_i32),
        (ARRAY_SIGNED, 3) => emit!(read_complex_i64_array, write_complex_i64),
        (ARRAY_UNSIGNED, 0) => emit!(read_complex_u8_array, write_complex_u8),
        (ARRAY_UNSIGNED, 1) => emit!(read_complex_u16_array, write_complex_u16),
        (ARRAY_UNSIGNED, 2) => emit!(read_complex_u32_array, write_complex_u32),
        (ARRAY_UNSIGNED, 3) => emit!(read_complex_u64_array, write_complex_u64),
        (ARRAY_FLOAT, 4) => Err(Error::msg(format!(
            "unsupported 128-bit float complex at {path}"
        ))),
        (ARRAY_SIGNED | ARRAY_UNSIGNED, 4) => Err(Error::msg(format!(
            "unsupported 128-bit integer complex at {path}; MATLAB has no 128-bit class"
        ))),
        _ => Err(Error::msg(format!(
            "unsupported complex element type (class {}, width code {}) at {path}",
            header.class, header.byte_code
        ))),
    }
}

fn maybe_reorder_complex<T: Clone>(
    data: Vec<T>,
    reorder: Option<(MatrixLayout, &[usize])>,
    options: &MatV73Options,
    path: &str,
) -> Result<Vec<T>> {
    match reorder {
        Some((layout, extents)) => maybe_reorder(data, layout, extents, options, path),
        None => Ok(data),
    }
}

fn walk_matrix(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    path: &str,
) -> Result<()> {
    let layout = if (reader.read_header()? & 0x01) == 0 {
        MatrixLayout::Right
    } else {
        MatrixLayout::Left
    };
    let extents = reader.read_matrix_extents()?;
    validate_matrix_extents(&extents, path)?;
    let header = reader.read_header()?;
    let dims = mb.matrix_dims(&extents);
    match parse_type(header) {
        TYPE_TYPED_ARRAY => walk_matrix_typed_array(
            reader, mb, options, name, &dims, &extents, layout, header, path,
        ),
        TYPE_EXTENSION if parse_extension_id(header) == EXT_COMPLEX => {
            walk_matrix_complex(reader, mb, options, name, &dims, &extents, layout, path)
        }
        _ => Err(Error::msg(format!("unsupported matrix payload at {path}"))),
    }
}

// ---------------------------------------------------------------------------
// Cell-element walker
// ---------------------------------------------------------------------------

fn walk_value_at_cell_element(
    reader: &mut Reader<'_>,
    cw: &mut CellWriter<'_>,
    options: &MatV73Options,
    path: &mut String,
) -> Result<()> {
    let header = reader.read_header()?;

    // `NullPolicy::Omit` cannot be honored here. A cell array's element count is
    // fixed by its dims, so the slot exists whether or not anything is written
    // into it: omitting a field is expressible, omitting an element is not.
    // Writing nothing still consumes a `#refs#` slot and leaves the element
    // pointing at an object that was never created, which MATLAB cannot
    // dereference. So a null in this position takes the empty-struct-array
    // marker regardless, matching what hdf5-pure's own serde emitter does for
    // the same case. The other two policies need no special handling: one
    // writes that marker anyway, and `Error` fails the whole conversion.
    if header == 0 && matches!(options.null_policy, NullPolicy::Omit) {
        cw.push_empty_struct_array().map_err(map_mat_error)?;
        return Ok(());
    }

    cw.push_with(|mb| {
        // The closure runs with a fresh `next_target` armed; the first
        // MatBuilder call routes to `#refs#/ref_NNNN`. After that first call
        // the arm is consumed and any deeper writes (e.g. inside a struct or
        // cell that was just opened) target the right scope automatically.
        walk_value_with_header(reader, mb, options, "", header, path).map_err(map_beve_error)
    })
    .map_err(map_mat_error)?;
    Ok(())
}

// ---------------------------------------------------------------------------
// Numeric typed-array writers (use bulk readers above)
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
fn write_float_array(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    dims: &[usize],
    byte_code: u8,
    len: usize,
    path: &str,
) -> Result<()> {
    match byte_code {
        0 => {
            check_low_precision_float(options, "bf16 array", path)?;
            let mut values = Vec::with_capacity(len);
            for _ in 0..len {
                values.push(reader.read_bf16_as_f32()?);
            }
            mb.write_f32(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        1 => {
            check_low_precision_float(options, "f16 array", path)?;
            let mut values = Vec::with_capacity(len);
            for _ in 0..len {
                values.push(reader.read_f16_as_f32()?);
            }
            mb.write_f32(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        2 => {
            let values = read_f32_array(reader, len)?;
            mb.write_f32(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        3 => {
            let values = read_f64_array(reader, len)?;
            mb.write_f64(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        _ => Err(Error::Unsupported("float128 arrays are not supported")),
    }
}

fn write_signed_array(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    name: &str,
    dims: &[usize],
    byte_code: u8,
    len: usize,
    path: &str,
) -> Result<()> {
    match byte_code {
        0 => {
            let values = read_i8_array(reader, len)?;
            mb.write_i8(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        1 => {
            let values = read_i16_array(reader, len)?;
            mb.write_i16(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        2 => {
            let values = read_i32_array(reader, len)?;
            mb.write_i32(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        3 => {
            let values = read_i64_array(reader, len)?;
            mb.write_i64(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        4 => Err(Error::msg(format!("unsupported i128 array at {path}"))),
        _ => Err(Error::InvalidSize),
    }
}

fn write_unsigned_array(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    name: &str,
    dims: &[usize],
    byte_code: u8,
    len: usize,
    path: &str,
) -> Result<()> {
    match byte_code {
        0 => {
            let values = read_u8_array(reader, len)?;
            mb.write_u8(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        1 => {
            let values = read_u16_array(reader, len)?;
            mb.write_u16(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        2 => {
            let values = read_u32_array(reader, len)?;
            mb.write_u32(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        3 => {
            let values = read_u64_array(reader, len)?;
            mb.write_u64(name, dims, &values).map_err(map_mat_error)?;
            Ok(())
        }
        4 => Err(Error::msg(format!("unsupported u128 array at {path}"))),
        _ => Err(Error::InvalidSize),
    }
}

// ---------------------------------------------------------------------------
// Matrix walkers
// ---------------------------------------------------------------------------

#[allow(clippy::too_many_arguments)]
fn walk_matrix_typed_array(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    dims: &[usize],
    extents: &[usize],
    layout: MatrixLayout,
    header: u8,
    path: &str,
) -> Result<()> {
    let info = reader.read_typed_array_header(header)?;
    let expected_len = product(extents)?;
    if info.len != expected_len {
        return Err(Error::msg(format!(
            "matrix payload length {} does not match extents product {} at {path}",
            info.len, expected_len
        )));
    }
    match info.class {
        TypedArrayClass::Float => walk_matrix_float(
            reader,
            mb,
            options,
            name,
            dims,
            extents,
            layout,
            info.byte_code,
            path,
        ),
        TypedArrayClass::Signed => walk_matrix_signed(
            reader,
            mb,
            options,
            name,
            dims,
            extents,
            layout,
            info.byte_code,
            path,
        ),
        TypedArrayClass::Unsigned => walk_matrix_unsigned(
            reader,
            mb,
            options,
            name,
            dims,
            extents,
            layout,
            info.byte_code,
            path,
        ),
        TypedArrayClass::Bool => {
            let bytes = read_bool_array(reader, info.len)?;
            let reordered = maybe_reorder(bytes, layout, extents, options, path)?;
            mb.write_logical(name, dims, &reordered)
                .map_err(map_mat_error)?;
            Ok(())
        }
        TypedArrayClass::String => {
            let values = read_string_array(reader, info.len)?;
            let reordered = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_string_object(name, &reordered, dims)
                .map_err(map_mat_error)?;
            Ok(())
        }
    }
}

#[allow(clippy::too_many_arguments)]
fn walk_matrix_float(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    dims: &[usize],
    extents: &[usize],
    layout: MatrixLayout,
    byte_code: u8,
    path: &str,
) -> Result<()> {
    let len = product(extents)?;
    match byte_code {
        0 => {
            check_low_precision_float(options, "bf16 matrix", path)?;
            let mut values = Vec::with_capacity(len);
            for _ in 0..len {
                values.push(reader.read_bf16_as_f32()?);
            }
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_f32(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        1 => {
            check_low_precision_float(options, "f16 matrix", path)?;
            let mut values = Vec::with_capacity(len);
            for _ in 0..len {
                values.push(reader.read_f16_as_f32()?);
            }
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_f32(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        2 => {
            let values = read_f32_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_f32(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        3 => {
            let values = read_f64_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_f64(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        _ => Err(Error::msg(format!(
            "unsupported float matrix width at {path}"
        ))),
    }
}

#[allow(clippy::too_many_arguments)]
fn walk_matrix_signed(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    dims: &[usize],
    extents: &[usize],
    layout: MatrixLayout,
    byte_code: u8,
    path: &str,
) -> Result<()> {
    let len = product(extents)?;
    match byte_code {
        0 => {
            let values = read_i8_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_i8(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        1 => {
            let values = read_i16_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_i16(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        2 => {
            let values = read_i32_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_i32(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        3 => {
            let values = read_i64_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_i64(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        _ => Err(Error::msg(format!("unsupported i128 matrix at {path}"))),
    }
}

#[allow(clippy::too_many_arguments)]
fn walk_matrix_unsigned(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    dims: &[usize],
    extents: &[usize],
    layout: MatrixLayout,
    byte_code: u8,
    path: &str,
) -> Result<()> {
    let len = product(extents)?;
    match byte_code {
        0 => {
            let values = read_u8_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_u8(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        1 => {
            let values = read_u16_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_u16(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        2 => {
            let values = read_u32_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_u32(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        3 => {
            let values = read_u64_array(reader, len)?;
            let v = maybe_reorder(values, layout, extents, options, path)?;
            mb.write_u64(name, dims, &v).map_err(map_mat_error)?;
            Ok(())
        }
        _ => Err(Error::msg(format!("unsupported u128 matrix at {path}"))),
    }
}

#[allow(clippy::too_many_arguments)]
fn walk_matrix_complex(
    reader: &mut Reader<'_>,
    mb: &mut MatBuilder,
    options: &MatV73Options,
    name: &str,
    dims: &[usize],
    extents: &[usize],
    layout: MatrixLayout,
    path: &str,
) -> Result<()> {
    let info = reader.read_complex_header()?;
    if !info.is_array {
        return Err(Error::msg(format!(
            "matrix complex payload must be an array at {path}"
        )));
    }
    let len = reader.read_size()?;
    let expected = product(extents)?;
    if len != expected {
        return Err(Error::msg(format!(
            "matrix complex payload length {} does not match extents product {} at {path}",
            len, expected
        )));
    }
    write_complex_array(
        reader,
        mb,
        options,
        name,
        dims,
        info,
        len,
        Some((layout, extents)),
        path,
    )
}

// ---------------------------------------------------------------------------
// Shared helpers
// ---------------------------------------------------------------------------

fn read_bool_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<u8>> {
    let packed = reader.read_exact(len.div_ceil(8))?;
    Ok(unpack_bools(packed, len))
}

fn read_string_array(reader: &mut Reader<'_>, len: usize) -> Result<Vec<String>> {
    let mut values = Vec::with_capacity(len);
    for _ in 0..len {
        values.push(reader.read_string()?.to_owned());
    }
    Ok(values)
}

fn unpack_bools(packed: &[u8], len: usize) -> Vec<u8> {
    let mut out = Vec::with_capacity(len);
    for idx in 0..len {
        let byte = packed[idx / 8];
        let bit = (byte >> (idx % 8)) & 1;
        out.push(bit);
    }
    out
}

fn check_low_precision_float(options: &MatV73Options, label: &str, path: &str) -> Result<()> {
    match options.unsupported_policy {
        UnsupportedPolicy::LossyNumericWidening => Ok(()),
        _ => Err(Error::msg(format!(
            "unsupported {label} at {path}; enable LossyNumericWidening to map it to MATLAB single"
        ))),
    }
}

fn maybe_reorder<T: Clone>(
    data: Vec<T>,
    layout: MatrixLayout,
    extents: &[usize],
    options: &MatV73Options,
    path: &str,
) -> Result<Vec<T>> {
    match layout {
        MatrixLayout::Left => Ok(data),
        MatrixLayout::Right => match options.row_major_policy {
            RowMajorPolicy::ReorderToColumnMajor => {
                Ok(reorder_row_major_to_column_major(&data, extents))
            }
            RowMajorPolicy::Error => Err(Error::msg(format!(
                "row-major matrix at {path} requires reordering to MATLAB column-major layout"
            ))),
            _ => Err(Error::Unsupported("unrecognized row-major policy")),
        },
    }
}

fn reorder_row_major_to_column_major<T: Clone>(data: &[T], extents: &[usize]) -> Vec<T> {
    if extents.len() <= 1 || data.len() <= 1 {
        return data.to_vec();
    }
    if extents.len() == 2 {
        // 2-D fast path: avoid the modulo/divide per element of the N-D path.
        let rows = extents[0];
        let cols = extents[1];
        let mut out = Vec::with_capacity(rows * cols);
        for c in 0..cols {
            for r in 0..rows {
                out.push(data[r * cols + c].clone());
            }
        }
        return out;
    }
    let mut row_strides = vec![1usize; extents.len()];
    for idx in (0..extents.len().saturating_sub(1)).rev() {
        row_strides[idx] = row_strides[idx + 1] * extents[idx + 1];
    }
    let mut out = Vec::with_capacity(data.len());
    for column_major_pos in 0..data.len() {
        let mut remaining = column_major_pos;
        let mut row_index = 0usize;
        for (axis, &extent) in extents.iter().enumerate() {
            let coord = remaining % extent;
            remaining /= extent;
            row_index += coord * row_strides[axis];
        }
        out.push(data[row_index].clone());
    }
    out
}

fn product(extents: &[usize]) -> Result<usize> {
    extents.iter().try_fold(1usize, |acc, &dim| {
        acc.checked_mul(dim).ok_or(Error::InvalidSize)
    })
}

fn validate_matrix_extents(extents: &[usize], path: &str) -> Result<()> {
    if extents.is_empty() {
        return Err(Error::msg(format!(
            "matrix extents cannot be empty at {path}"
        )));
    }
    if extents.contains(&0) {
        return Err(Error::msg(format!(
            "matrix dimensions cannot be zero at {path}"
        )));
    }
    Ok(())
}

fn map_mat_error(e: hdf5_pure::mat::MatError) -> Error {
    Error::msg(e.to_string())
}

fn map_beve_error(e: Error) -> hdf5_pure::mat::MatError {
    hdf5_pure::mat::MatError::Custom(e.to_string())
}

// ---------------------------------------------------------------------------
// Atomic temp-file write helpers (beve-specific)
// ---------------------------------------------------------------------------

struct TempOutputFile {
    path: PathBuf,
    persisted: bool,
}

impl TempOutputFile {
    fn new(output_path: &Path) -> Result<Self> {
        static NEXT_ID: AtomicU64 = AtomicU64::new(0);

        let directory = output_path
            .parent()
            .filter(|path| !path.as_os_str().is_empty())
            .unwrap_or_else(|| Path::new("."));
        let base_name = output_path
            .file_name()
            .map(ToOwned::to_owned)
            .unwrap_or_else(|| OsString::from("beve.mat"));

        for _ in 0..1024 {
            let sequence = NEXT_ID.fetch_add(1, Ordering::Relaxed);
            let mut temp_name = OsString::from(".");
            temp_name.push(&base_name);
            temp_name.push(format!(".beve-tmp-{:x}-{:x}", std::process::id(), sequence));
            let path = directory.join(temp_name);
            match OpenOptions::new().write(true).create_new(true).open(&path) {
                Ok(file) => {
                    drop(file);
                    return Ok(Self {
                        path,
                        persisted: false,
                    });
                }
                Err(err) if err.kind() == std::io::ErrorKind::AlreadyExists => continue,
                Err(err) => return Err(Error::msg(err.to_string())),
            }
        }

        Err(Error::msg(format!(
            "failed to reserve a temporary MAT path near {}",
            output_path.display()
        )))
    }

    fn path(&self) -> &Path {
        &self.path
    }

    fn persist(mut self, output_path: &Path) -> Result<()> {
        replace_file(&self.path, output_path).map_err(|e| Error::msg(e.to_string()))?;
        self.persisted = true;
        Ok(())
    }
}

impl Drop for TempOutputFile {
    fn drop(&mut self) {
        if !self.persisted {
            let _ = std::fs::remove_file(&self.path);
        }
    }
}

fn sync_regular_file(path: &Path) -> Result<()> {
    OpenOptions::new()
        .read(true)
        .open(path)
        .and_then(|file| file.sync_all())
        .map_err(|e| Error::msg(e.to_string()))
}

#[cfg(not(windows))]
fn replace_file(from: &Path, to: &Path) -> std::io::Result<()> {
    std::fs::rename(from, to)
}

#[cfg(windows)]
fn replace_file(from: &Path, to: &Path) -> std::io::Result<()> {
    use std::os::windows::ffi::OsStrExt;

    const MOVEFILE_REPLACE_EXISTING: u32 = 0x1;
    const MOVEFILE_WRITE_THROUGH: u32 = 0x8;

    unsafe extern "system" {
        fn MoveFileExW(
            existing_file_name: *const u16,
            new_file_name: *const u16,
            flags: u32,
        ) -> i32;
    }

    fn to_wide(path: &Path) -> Vec<u16> {
        path.as_os_str().encode_wide().chain(Some(0)).collect()
    }

    let from_wide = to_wide(from);
    let to_wide = to_wide(to);
    let result = unsafe {
        MoveFileExW(
            from_wide.as_ptr(),
            to_wide.as_ptr(),
            MOVEFILE_REPLACE_EXISTING | MOVEFILE_WRITE_THROUGH,
        )
    };
    if result != 0 {
        Ok(())
    } else {
        Err(std::io::Error::last_os_error())
    }
}