readcon-core 0.14.0

An oxidized single and multiple CON file reader and writer with FFI bindings for ergonomic C/C++ usage.
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
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
/*
 * readcon-core C API.
 *
 * Memory ownership
 *   - Opaque handles (RKRConFrame, RKRConFrameWriter, RKRConFrameBuilder,
 *     CConFrameIterator) are caller-owned. Free each with its dedicated
 *     destructor: free_rkr_frame, free_rkr_frame_writer,
 *     free_rkr_frame_builder, free_con_frame_iterator.
 *   - rkr_read_all_frames returns a heap-allocated array of RKRConFrame*
 *     handles together with the frame count via the *num_frames out-param.
 *     The array's capacity equals the count (shrunk to fit). Free with
 *     free_rkr_frame_array(frames, *num_frames). Each individual frame is
 *     released by free_rkr_frame_array; do NOT also call free_rkr_frame.
 *   - char* values returned by rkr_frame_metadata_json,
 *     rkr_frame_potential_type, and rkr_frame_get_header_line_cpp are heap
 *     allocated; free them with rkr_free_string. rkr_free_string is safe
 *     to call with NULL (no-op).
 *   - const char* returned by rkr_library_version is process-static; do
 *     NOT free it.
 *   - rkr_frame_to_c_frame returns a CFrame whose `atoms` array is owned
 *     by the caller; release with free_c_frame.
 *
 * Sentinel values for absent metadata
 *   - Floating-point getters (rkr_frame_energy, rkr_frame_time,
 *     rkr_frame_timestep, etc.) return NaN when the metadata key is
 *     absent. Test with isnan() from <math.h>.
 *   - Unsigned-integer getters (rkr_frame_frame_index, rkr_frame_neb_bead,
 *     rkr_frame_neb_band) return UINT64_MAX when absent.
 *   - String getters return NULL when absent.
 *
 * Thread safety
 *   - Opaque handles are NOT Sync. Do not share a single
 *     RKRConFrame/RKRConFrameWriter/RKRConFrameBuilder across threads
 *     without external synchronization. Distinct handles are independent.
 *   - rkr_read_all_frames internally uses sequential parsing; the
 *     `parallel` Cargo feature is exposed only through the Rust API and
 *     is not surfaced via this C header.
 */

/* Forward-declare the DLPack-managed tensor type for the tier-3
 * builder export FFI. Consumers that want to dereference fields
 * (data, shape, strides, dtype, deleter) must include
 * <dlpack/dlpack.h> (or equivalent) themselves; the readcon-core C
 * ABI only passes the pointer through. */
struct DLManagedTensorVersioned;
typedef struct DLManagedTensorVersioned RKRDLManagedTensorVersioned;

/* Metatensor block: opaque. Prefer include/readcon-metatensor.h (pulls
 * metatensor.h first). With only this header, define READCON_CORE_HAS_METATENSOR
 * and include <metatensor.h>, or use the incomplete struct typedef below. */
struct mts_block_t;
typedef struct mts_block_t mts_block_t;


#ifndef READCON_H
#define READCON_H

#pragma once

/* Generated with cbindgen:0.29.4 */

/* Warning, this file is autogenerated by cbindgen. Don't modify this manually. */

#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>

#ifdef __cplusplus
namespace readcon {
#endif  // __cplusplus

/**
 * CON/convel format spec version implemented by this build.
 *
 * - Version 1: column 5 present but semantics undefined. Readers MAY
 *   ignore it. No JSON metadata line.
 * - Version 2: column 5 is the original atom index before type-based
 *   grouping; JSON line 2 with at least `{"con_spec_version": 2}`.
 * - Version 3: same as v2 plus **required** `metadata["units"]` object
 *   with non-empty `length` and `energy` unit strings (see `units` module).
 *
 * See `docs/orgmode/spec.org` for the full specification.
 */
#define CON_SPEC_VERSION 3

/**
 * CON/convel format spec version. Use `#if RKR_CON_SPEC_VERSION >= 2` in C/C++
 * to gate code that depends on atom_index semantics.
 *
 * Tracks `crate::CON_SPEC_VERSION` (which the Rust API exposes as
 * `CON_SPEC_VERSION`). Both macros are emitted into the C header for
 * the convenience of either naming convention; they always carry the
 * same value.
 */
#define RKR_CON_SPEC_VERSION 3

#define RKR_DL_INT 0

#define RKR_DL_UINT 1

#define RKR_DL_FLOAT 2

#define RKR_DL_OPAQUE_HANDLE 3

#define RKR_DL_BFLOAT 4

#define RKR_DL_COMPLEX 5

#define RKR_DL_BOOL 6

#define RKR_DL_CPU 1

#define RKR_DL_CUDA 2

#define RKR_DL_CUDA_HOST 3

/**
 * Bit 0: forces section or per-atom forces present.
 */
#define SECTIONS_MASK_FORCES (1 << 0)

/**
 * Bit 1: velocities section or per-atom velocities present.
 */
#define SECTIONS_MASK_VELOCITIES (1 << 1)

/**
 * Bit 2: energies section or finite frame energy present.
 */
#define SECTIONS_MASK_ENERGIES (1 << 2)

/**
 * Error codes for RKR functions.
 */
typedef enum RKRStatus {
    /**
     * Function completed successfully.
     */
    RKR_STATUS_SUCCESS = 0,
    /**
     * A null pointer was passed for a required argument.
     */
    RKR_STATUS_NULL_POINTER = -1,
    /**
     * An input string was not valid UTF-8.
     */
    RKR_STATUS_INVALID_UTF8 = -2,
    /**
     * JSON parsing or serialization failed.
     */
    RKR_STATUS_INVALID_JSON = -3,
    /**
     * File I/O error.
     */
    RKR_STATUS_IO_ERROR = -4,
    /**
     * Index out of bounds.
     */
    RKR_STATUS_INDEX_OUT_OF_BOUNDS = -5,
    /**
     * The destination buffer cannot hold a null-terminated string.
     */
    RKR_STATUS_BUFFER_TOO_SMALL = -6,
    /**
     * An internal logic error or unhandled state.
     */
    RKR_STATUS_INTERNAL_ERROR = -7,
    /**
     * An optional section (velocities, forces, atom_energies) was
     * requested but is not declared on the builder.
     */
    RKR_STATUS_SECTION_ABSENT = -8,
    /**
     * DLPack export or another validation step failed.
     */
    RKR_STATUS_VALIDATION_ERROR = -9,
    /**
     * Chemfiles selection parse/evaluate failed (requires chemfiles-enabled build).
     */
    RKR_STATUS_SELECTION_ERROR = -10,
    /**
     * Requested API is not in this build (Cargo feature off / symbols omitted).
     * Never use `-7` for this — that is [`RKR_STATUS_INTERNAL_ERROR`].
     */
    RKR_STATUS_FEATURE_DISABLED = -11,
} RKRStatus;

/**
 * An iterator that lazily parses simulation frames from a `.con` or `.convel`
 * file's contents.
 *
 * This struct wraps an iterator over the lines of a string and, upon each iteration,
 * attempts to parse a complete `ConFrame`. Velocity sections are detected
 * automatically: if a blank line follows the coordinate blocks, the velocity
 * data is parsed into the atoms.
 *
 * The iterator yields items of type `Result<ConFrame, ParseError>`, allowing for
 * robust error handling for each frame.
 */
typedef struct ConFrameIterator ConFrameIterator;

/**
 * Physical dimension exponents: L, T, M, Q (charge), Θ (temperature).
 */
typedef struct Dimension Dimension;

/**
 * Opaque handle for a cached selection evaluation result.
 */
typedef struct RKRSelectionResult RKRSelectionResult;

typedef struct String String;

/**
 * An opaque handle to a full, lossless Rust `ConFrame` object.
 * The C/C++ side needs to treat this as a void pointer
 */
typedef struct RKRConFrame {
    uint8_t _private[0];
} RKRConFrame;

typedef struct CConFrameIterator {
    struct ConFrameIterator *iterator;
    struct String *file_contents;
} CConFrameIterator;

/**
 * Transparent atom record extracted via [`rkr_frame_to_c_frame`].
 *
 * `is_fixed` is the OR of `fixed_x`, `fixed_y`, `fixed_z`; it is kept
 * for source compatibility with pre-spec-v2 callers that did not have
 * per-axis flags. New code should use the per-axis fields.
 *
 * `vx`/`vy`/`vz`, `fx`/`fy`/`fz`, and `energy` carry meaningful values
 * only when `has_velocity`, `has_forces`, or `has_energy` is true
 * respectively; the values are zeroed otherwise.
 */
typedef struct CAtom {
    uint64_t atomic_number;
    double x;
    double y;
    double z;
    uint64_t atom_id;
    double mass;
    /**
     * True when any of `fixed_x`, `fixed_y`, `fixed_z` is true.
     * Kept for source compatibility; prefer the per-axis fields.
     */
    bool is_fixed;
    bool fixed_x;
    bool fixed_y;
    bool fixed_z;
    double vx;
    double vy;
    double vz;
    bool has_velocity;
    double fx;
    double fy;
    double fz;
    bool has_forces;
    /**
     * Per-atom energy contribution; meaningful only when
     * `has_energy` is true. See [`crate::types::SECTION_ENERGIES`].
     */
    double energy;
    bool has_energy;
} CAtom;

/**
 * A transparent, "lossy" C-struct containing only the core atomic data.
 * This can be extracted from an `RKRConFrame` handle for direct data access.
 * The caller is responsible for freeing the `atoms` array using `free_c_frame`.
 */
typedef struct CFrame {
    struct CAtom *atoms;
    uintptr_t num_atoms;
    double cell[3];
    double angles[3];
    bool has_velocities;
    bool has_forces;
    bool has_energies;
} CFrame;

/**
 * An opaque handle to a Rust `ConFrameWriter` object.
 * The C/C++ side needs to treat this as a void pointer
 */
typedef struct RKRConFrameWriter {
    uint8_t _private[0];
} RKRConFrameWriter;

/**
 * An opaque handle to a Rust `ConFrameBuilder` object.
 */
typedef struct RKRConFrameBuilder {
    uint8_t _private[0];
} RKRConFrameBuilder;

/**
 * Element type request — **layout-identical** to DLPack `DLDataType`
 * (`uint8_t code`, `uint8_t bits`, `uint16_t lanes`). Interchangeable with
 * `DLDataType` from `<dlpack/dlpack.h>` when that header is included.
 */
typedef struct RKRDLDataType {
    /**
     * `DLDataTypeCode` (e.g. [`rkr_dl_type_code::RKR_DL_FLOAT`]).
     */
    uint8_t code;
    /**
     * Bit width (32 or 64 for float sections today).
     */
    uint8_t bits;
    /**
     * Vector lanes (must be 1 for current exports).
     */
    uint16_t lanes;
} RKRDLDataType;

/**
 * Device request — **layout-identical** to DLPack `DLDevice`
 * (`DLDeviceType device_type`, `int32_t device_id`).
 */
typedef struct RKRDLDevice {
    /**
     * `DLDeviceType` (e.g. [`rkr_dl_device_type::RKR_DL_CPU`]).
     */
    int32_t device_type;
    int32_t device_id;
} RKRDLDevice;

/**
 * Options for DLPack export: requested **DLPack** `DLDataType` + `DLDevice`.
 *
 * Pass to `*_dlpack_ex`. NULL → `kDLFloat` / 64 / lanes 1 on **CPU**.
 *
 * **Dtype (CPU):** any combination dlpk can host from converted CON data —
 * signed/unsigned ints (8/16/32/64), IEEE floats (32/64), and bool (8-bit
 * DLPack convention). Values are cast from on-disk binary64 (or u64 for atom
 * ids). Complex / bfloat / float8 / opaque / multi-lane types return
 * `RKR_STATUS_VALIDATION_ERROR` until implemented.
 *
 * **Device:** only `device_type = kDLCPU` (1) is backed today; CUDA and other
 * `DLDeviceType` values are accepted in the struct but return
 * `RKR_STATUS_FEATURE_DISABLED` so callers can feature-detect without an ABI
 * break when device-resident exports land.
 */
typedef struct RKRDlpackExportOptions {
    /**
     * Requested element type (DLPack `DLDataType` layout).
     */
    struct RKRDLDataType dtype;
    /**
     * Requested placement (DLPack `DLDevice` layout).
     */
    struct RKRDLDevice device;
} RKRDlpackExportOptions;

#if !defined(READCON_CORE_HAS_METATENSOR)
/**
 * Lean-build stubs: always export metatensor C symbols so Fortran/C can link without `#ifdef`.
 * Real implementations live under `feature = "metatensor"`.
 */
typedef struct mts_block_t {
    uint8_t _private[0];
} mts_block_t;
#endif









#ifdef __cplusplus
extern "C" {
#endif // __cplusplus

/**
 * Returns the spec version at runtime (for dynamically linked consumers).
 */
uint32_t rkr_con_spec_version(void);

/**
 * Returns a pointer to a static, null-terminated library version string.
 * The returned pointer is valid for the lifetime of the process. Do NOT free it.
 */
const char *rkr_library_version(void);

/**
 * Returns the position of an atom inside the frame's `atom_data` array
 * matching the given `atom_id`. Returns `UINT64_MAX` if no atom with
 * that id exists or `frame_handle` is NULL.
 *
 * O(N) per call. C/C++ consumers performing many lookups should cache
 * a `std::unordered_map<uint64_t, size_t>` from a single sweep over
 * the frame.
 *
 * # Safety
 *
 * `frame_handle` must point to a valid `RKRConFrame` allocation.
 */
uint64_t rkr_frame_atom_index_by_id(const struct RKRConFrame *frame_handle,
                                    uint64_t atom_id);

/**
 * Returns the atomic number for a chemical symbol, or 0 if the symbol
 * is unknown or `symbol` is NULL. Lookup covers H..U (Z = 1..=92) and
 * is case-sensitive: "Fe" works, "fe" does not.
 *
 * # Safety
 *
 * `symbol` must be either NULL or a pointer to a NUL-terminated UTF-8
 * C string valid for reads up to the terminating NUL byte.
 */
uint64_t rkr_symbol_to_z(const char *symbol);

/**
 * Returns a pointer to a static, NUL-terminated chemical symbol for an
 * atomic number, or "X" for unknown values. Coverage is H..U
 * (Z = 1..=92). The returned pointer is valid for the lifetime of the
 * process; do NOT free it.
 */
const char *rkr_z_to_symbol(uint64_t z);

/**
 * Returns the spec version stored in a parsed frame's header.
 * Returns 0 on error (null handle).
 */
uint32_t rkr_frame_spec_version(const struct RKRConFrame *frame_handle);

/**
 * Returns the JSON metadata line from a parsed frame as a heap-allocated
 * null-terminated C string. The caller MUST free with `rkr_free_string`.
 * Returns NULL on error.
 *
 * # Safety
 * frame_handle must be valid. The caller takes ownership of the returned string.
 */
char *rkr_frame_metadata_json(const struct RKRConFrame *frame_handle);

/**
 * Returns the per-frame energy from metadata, or NaN if absent.
 */
double rkr_frame_energy(const struct RKRConFrame *frame_handle);

/**
 * Campaign **finite** energy ([`crate::index_proj::finite_energy`]): NaN if missing or non-finite.
 * Prefer this over [`rkr_frame_energy`] when mirroring `readcon-db` indexes.
 */
double rkr_frame_index_energy(const struct RKRConFrame *frame_handle);

/**
 * Canonical multiset formula (`Cu:2|H:2`) for campaign `idx_formula`. Free with `rkr_free_string`.
 */
char *rkr_frame_composition_formula(const struct RKRConFrame *frame_handle);

/**
 * Total mass from type masses × counts; NaN if not all finite.
 */
double rkr_frame_total_mass(const struct RKRConFrame *frame_handle);

/**
 * Cell volume (lattice det or triclinic); NaN if unavailable.
 */
double rkr_frame_cell_volume(const struct RKRConFrame *frame_handle);

/**
 * Max \(\|F_i\|\); NaN if no finite forces.
 */
double rkr_frame_fmax(const struct RKRConFrame *frame_handle);

/**
 * Sections presence bitmask: bit0 forces, bit1 velocities, bit2 energies (see `index_proj`).
 */
uint8_t rkr_frame_sections_mask(const struct RKRConFrame *frame_handle);

/**
 * Atom count used for campaign `idx_natoms` (same as `atom_data.len()`).
 */
uint32_t rkr_frame_index_natoms(const struct RKRConFrame *frame_handle);

/**
 * Compact JSON of [`crate::index_proj::FrameIndexProjection`] (campaign screening fields).
 * Free with `rkr_free_string`. NULL on error.
 */
char *rkr_frame_index_projection_json(const struct RKRConFrame *frame_handle);

/**
 * Returns the potential type string from metadata as a heap-allocated
 * null-terminated C string. The caller MUST free with `rkr_free_string`.
 * Returns NULL if absent or on error.
 *
 * # Safety
 * frame_handle must be valid. The caller takes ownership of the returned string.
 */
char *rkr_frame_potential_type(const struct RKRConFrame *frame_handle);

/**
 * Returns the zero-based frame index from metadata, or UINT64_MAX if absent.
 */
uint64_t rkr_frame_frame_index(const struct RKRConFrame *frame_handle);

/**
 * Returns the simulation time from metadata, or NaN if absent.
 */
double rkr_frame_time(const struct RKRConFrame *frame_handle);

/**
 * Returns the integration timestep from metadata, or NaN if absent.
 */
double rkr_frame_timestep(const struct RKRConFrame *frame_handle);

/**
 * Returns the NEB bead index from metadata, or UINT64_MAX if absent.
 */
uint64_t rkr_frame_neb_bead(const struct RKRConFrame *frame_handle);

/**
 * Returns the NEB band index from metadata, or UINT64_MAX if absent.
 */
uint64_t rkr_frame_neb_band(const struct RKRConFrame *frame_handle);

/**
 * Number of optional frame topology bonds (`metadata["bonds"]`), or 0 if absent.
 *
 * # Safety
 * `frame_handle` must be a valid handle or NULL.
 */
uint64_t rkr_frame_bond_count(const struct RKRConFrame *frame_handle);

/**
 * Read one bond at `index` (0-based into the `bonds` metadata array).
 *
 * Writes 0-based `atom_data` indices into `out_i` / `out_j`. When the bond
 * has an explicit order, sets `out_has_order` to 1 and `out_order` to that
 * integer; otherwise `out_has_order` is 0.
 *
 * # Safety
 * `frame_handle` must be valid. Output pointers must be non-null.
 */
enum RKRStatus rkr_frame_bond_at(const struct RKRConFrame *frame_handle,
                                 uint64_t index,
                                 uint32_t *out_i,
                                 uint32_t *out_j,
                                 uint8_t *out_has_order,
                                 int32_t *out_order);

/**
 * Returns a stable, static message for a status code.
 * The returned pointer is valid for the lifetime of the process. Do NOT free it.
 */
const char *rkr_status_message(enum RKRStatus status);

/**
 * Creates a new iterator for a .con / .convel path, including transparent
 * gzip (`.con.gz`) and zstd (`.con.zst`, requires `zstd` feature) inputs via
 * [`crate::compression::read_file_contents`].
 *
 * Returns NULL if the file cannot be read, decompressed, or is not valid
 * UTF-8. A successfully-opened file with zero frames returns a non-NULL
 * iterator that yields NULL on the first call to [`con_frame_iterator_next`].
 * The caller OWNS the returned pointer and MUST call [`free_con_frame_iterator`].
 *
 * # Safety
 * filename_c must be a valid null-terminated string. The caller takes
 * ownership of the returned iterator.
 */
struct CConFrameIterator *read_con_file_iterator(const char *filename_c);

/**
 * Iterate frames from an in-memory CON text buffer (null-terminated C string).
 *
 * Use when the caller already decompressed (chemfiles, custom I/O) and wants
 * to avoid a temp file. Same ownership rules as [`read_con_file_iterator`].
 *
 * # Safety
 * `contents_c` must be a valid null-terminated UTF-8 string, or NULL (returns NULL).
 */
struct CConFrameIterator *read_con_string_iterator(const char *contents_c);

/**
 * Iterate frames from a byte buffer (not necessarily null-terminated).
 *
 * `len` is the number of bytes at `data`. Bytes must be valid UTF-8 CON text.
 *
 * # Safety
 * `data` must be valid for `len` bytes if non-null and `len > 0`.
 */
struct CConFrameIterator *read_con_buffer_iterator(const uint8_t *data,
                                                   uintptr_t len);

/**
 * Reads the next frame from the iterator, returning an opaque handle.
 * The caller OWNS the returned handle and must free it with `free_rkr_frame`.
 *
 * # Safety
 * iterator must be valid. The caller takes ownership of the returned frame.
 */
struct RKRConFrame *con_frame_iterator_next(struct CConFrameIterator *iterator);

/**
 * Frees the memory for an opaque `RKRConFrame` handle.
 *
 * # Safety
 * frame_handle must be valid or null.
 */
void free_rkr_frame(struct RKRConFrame *frame_handle);

/**
 * Frees the memory for a `CConFrameIterator`.
 *
 * # Safety
 * iterator must be valid or null.
 */
void free_con_frame_iterator(struct CConFrameIterator *iterator);

/**
 * Extracts the core atomic data into a transparent `CFrame` struct.
 * The caller OWNS the returned pointer and MUST call `free_c_frame` on it.
 *
 * # Safety
 * frame_handle must be valid. The caller takes ownership of the returned CFrame.
 */
struct CFrame *rkr_frame_to_c_frame(const struct RKRConFrame *frame_handle);

/**
 * Frees the memory of a `CFrame` struct, including its internal atoms array.
 *
 * # Safety
 * frame must be valid or null.
 */
void free_c_frame(struct CFrame *frame);

/**
 * Copies a header string line into a caller-provided buffer.
 *
 * `is_prebox=true` selects from the two prebox lines (line 0 = user
 * text, line 1 = JSON metadata); `false` selects from the two postbox
 * lines. Strings longer than `buffer_len - 1` bytes are truncated; the
 * final byte is always set to NUL.
 *
 * Returns `RKR_STATUS_SUCCESS` on success,
 * `RKR_STATUS_INDEX_OUT_OF_BOUNDS` if `line_index >= 2`,
 * `RKR_STATUS_NULL_POINTER` if `frame_handle` or `buffer` is NULL,
 * `RKR_STATUS_BUFFER_TOO_SMALL` if `buffer_len == 0`.
 *
 * Pair with [`rkr_frame_get_header_line_cpp`] when the caller prefers
 * an allocated string with no fixed length cap; that variant returns
 * NULL for the same out-of-bounds condition.
 *
 * # Safety
 * frame_handle must be valid. buffer must be at least buffer_len bytes.
 */
enum RKRStatus rkr_frame_get_header_line(const struct RKRConFrame *frame_handle,
                                         bool is_prebox,
                                         uintptr_t line_index,
                                         char *buffer,
                                         uintptr_t buffer_len);

/**
 * Gets a header string line as a newly allocated, null-terminated C string.
 *
 * The caller OWNS the returned pointer and MUST call `rkr_free_string`
 * on it to prevent a memory leak. Returns NULL on error or if the
 * index is invalid (use [`rkr_frame_get_header_line`] when a status
 * code is preferred to NULL-vs-success disambiguation).
 *
 * The `_cpp` suffix is historical; the function is callable from both
 * C and C++.
 *
 * # Safety
 * frame_handle must be valid. The caller takes ownership of the returned string.
 */
char *rkr_frame_get_header_line_cpp(const struct RKRConFrame *frame_handle,
                                    bool is_prebox,
                                    uintptr_t line_index);

/**
 * Frees a C string that was allocated by Rust (e.g., from
 * `rkr_frame_metadata_json`, `rkr_frame_potential_type`, or
 * `rkr_frame_get_header_line_cpp`). Safe to call with NULL (no-op).
 *
 * # Safety
 * s must be either NULL or a pointer previously returned by an
 * allocating Rust FFI function in this crate.
 */
void rkr_free_string(char *s);

/**
 * Creates a new frame writer for the specified file.
 * The caller OWNS the returned pointer and MUST call `free_rkr_writer`.
 *
 * # Safety
 * filename_c must be valid. The caller takes ownership of the returned writer.
 */
struct RKRConFrameWriter *create_writer_from_path_c(const char *filename_c);

/**
 * Frees the memory for an `RKRConFrameWriter`, closing the associated file.
 *
 * # Safety
 * writer_handle must be valid or null.
 */
void free_rkr_writer(struct RKRConFrameWriter *writer_handle);

/**
 * Writes multiple frames from an array of handles to the file managed by the writer.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * writer_handle and frame_handles must be valid.
 */
enum RKRStatus rkr_writer_extend(struct RKRConFrameWriter *writer_handle,
                                 const struct RKRConFrame *const *frame_handles,
                                 uintptr_t num_frames);

/**
 * Enable (`canonical != 0`) or disable campaign-stable CON serialization on an open writer.
 * Matches Rust `ConFrameWriter::canonical(true)` (deterministic metadata key order).
 *
 * # Safety
 * `writer_handle` must be valid or null (null → `RKR_STATUS_NULL_POINTER`).
 */
enum RKRStatus rkr_writer_set_canonical(struct RKRConFrameWriter *writer_handle,
                                        uint8_t canonical);

/**
 * Returns 1 if the writer is in canonical mode, 0 otherwise (or on null handle).
 */
uint8_t rkr_writer_is_canonical(const struct RKRConFrameWriter *writer_handle);

/**
 * Creates a new frame writer with custom floating-point precision.
 * The caller OWNS the returned pointer and MUST call `free_rkr_writer`.
 *
 * # Safety
 * filename_c must be valid. The caller takes ownership of the returned writer.
 */
struct RKRConFrameWriter *create_writer_from_path_with_precision_c(const char *filename_c,
                                                                   uint8_t precision);

/**
 * Attaches a velocity vector to the most recently added atom on a builder.
 * No-op if no atom has been added yet.
 *
 * # Safety
 * builder_handle must be valid. velocity must point to 3 contiguous f64 values.
 */
enum RKRStatus rkr_frame_builder_set_last_velocity(struct RKRConFrameBuilder *builder_handle,
                                                   const double *velocity);

/**
 * Attaches a force vector to the most recently added atom on a builder.
 * No-op if no atom has been added yet.
 *
 * # Safety
 * builder_handle must be valid. force must point to 3 contiguous f64 values.
 */
enum RKRStatus rkr_frame_builder_set_last_force(struct RKRConFrameBuilder *builder_handle,
                                                const double *force);

/**
 * Attaches a per-atom energy to the most recently added atom on a
 * builder. No-op if no atom has been added yet.
 *
 * Use this together with the per-frame `energy` metadata key when a
 * caller wants to round-trip an "Energies of Component" decomposition
 * alongside the total.
 *
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_last_energy(struct RKRConFrameBuilder *builder_handle,
                                                 double energy);

/**
 * Returns the number of atoms currently held in the builder.
 *
 * # Safety
 * builder_handle must be a valid pointer returned by rkr_frame_new and
 * not yet consumed by rkr_frame_builder_build / freed.
 * Returns 0 on NULL handle.
 */
uintptr_t rkr_frame_builder_atom_count(const struct RKRConFrameBuilder *builder_handle);

/**
 * Updates the Cartesian position of an existing atom.
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_atom_position(struct RKRConFrameBuilder *builder_handle,
                                                   uintptr_t index,
                                                   double x,
                                                   double y,
                                                   double z);

/**
 * Sets the velocity vector of an existing atom from 3 contiguous f64 values.
 * # Safety
 * builder_handle must be valid; velocity must point to 3 contiguous f64.
 */
enum RKRStatus rkr_frame_builder_set_atom_velocity(struct RKRConFrameBuilder *builder_handle,
                                                   uintptr_t index,
                                                   const double *velocity);

/**
 * Sets the force vector of an existing atom from 3 contiguous f64 values.
 * # Safety
 * builder_handle must be valid; force must point to 3 contiguous f64.
 */
enum RKRStatus rkr_frame_builder_set_atom_force(struct RKRConFrameBuilder *builder_handle,
                                                uintptr_t index,
                                                const double *force);

/**
 * Sets the per-atom energy contribution of an existing atom.
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_atom_energy(struct RKRConFrameBuilder *builder_handle,
                                                 uintptr_t index,
                                                 double energy);

/**
 * Updates per-direction fixed flags `[fixed_x, fixed_y, fixed_z]`.
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_atom_fixed(struct RKRConFrameBuilder *builder_handle,
                                                uintptr_t index,
                                                bool fixed_x,
                                                bool fixed_y,
                                                bool fixed_z);

/**
 * Updates the mass of an existing atom.
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_atom_mass(struct RKRConFrameBuilder *builder_handle,
                                               uintptr_t index,
                                               double mass);

/**
 * Updates the atom_id (pre-grouping index from .con column 5) of an
 * existing atom. The underlying `Array1<u64>` buffer pointer stays
 * stable; callers that hold a raw `*const u64` via
 * `rkr_frame_builder_atom_ids_data` do not need to refresh after this.
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_atom_id(struct RKRConFrameBuilder *builder_handle,
                                             uintptr_t index,
                                             uint64_t atom_id);

/**
 * Removes velocity / force / energy data from an existing atom.
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_clear_atom_velocity(struct RKRConFrameBuilder *builder_handle,
                                                     uintptr_t index);

/**
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_clear_atom_force(struct RKRConFrameBuilder *builder_handle,
                                                  uintptr_t index);

/**
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_clear_atom_energy(struct RKRConFrameBuilder *builder_handle,
                                                   uintptr_t index);

/**
 * Bulk-update positions for every atom from a flat row-major
 * `[x0,y0,z0,x1,y1,z1,...]` buffer of length `3 * atom_count()`.
 * # Safety
 * builder_handle must be valid; positions must point to `3 * len` f64.
 */
enum RKRStatus rkr_frame_builder_set_positions_from_flat(struct RKRConFrameBuilder *builder_handle,
                                                         const double *positions,
                                                         uintptr_t len);

/**
 * Bulk-update forces for every atom.
 * # Safety
 * builder_handle must be valid; forces must point to `3 * len` f64.
 */
enum RKRStatus rkr_frame_builder_set_forces_from_flat(struct RKRConFrameBuilder *builder_handle,
                                                      const double *forces,
                                                      uintptr_t len);

/**
 * Bulk-update per-atom energies (one f64 per atom).
 * # Safety
 * builder_handle must be valid; energies must point to `len` f64.
 */
enum RKRStatus rkr_frame_builder_set_atom_energies_from_flat(struct RKRConFrameBuilder *builder_handle,
                                                             const double *energies,
                                                             uintptr_t len);

/**
 * Reads the position of an existing atom into 3 contiguous f64 out values.
 * # Safety
 * builder_handle must be valid; out_xyz must point to 3 writable f64.
 */
enum RKRStatus rkr_frame_builder_get_atom_position(const struct RKRConFrameBuilder *builder_handle,
                                                   uintptr_t index,
                                                   double *out_xyz);

/**
 * Reads the velocity / force vector of an atom (if any) into 3 contiguous
 * f64. `*has_value` is set to `true` if the atom carries that vector,
 * `false` if it does not (in which case `out_xyz` is left untouched).
 *
 * # Safety
 * builder_handle, out_xyz, has_value must all be valid pointers.
 */
enum RKRStatus rkr_frame_builder_get_atom_velocity(const struct RKRConFrameBuilder *builder_handle,
                                                   uintptr_t index,
                                                   double *out_xyz,
                                                   bool *has_value);

/**
 * # Safety
 * builder_handle, out_xyz, has_value must all be valid pointers.
 */
enum RKRStatus rkr_frame_builder_get_atom_force(const struct RKRConFrameBuilder *builder_handle,
                                                uintptr_t index,
                                                double *out_xyz,
                                                bool *has_value);

/**
 * Reads the per-atom energy of an atom (if any). `*has_value` is set to
 * `true` if the atom carries an energy contribution, else `false` and
 * `*out_value` is left untouched.
 * # Safety
 * builder_handle, out_value, has_value must all be valid pointers.
 */
enum RKRStatus rkr_frame_builder_get_atom_energy(const struct RKRConFrameBuilder *builder_handle,
                                                 uintptr_t index,
                                                 double *out_value,
                                                 bool *has_value);

/**
 * Reads the mass of an existing atom.
 * # Safety
 * builder_handle and out_mass must be valid pointers.
 */
enum RKRStatus rkr_frame_builder_get_atom_mass(const struct RKRConFrameBuilder *builder_handle,
                                               uintptr_t index,
                                               double *out_mass);

/**
 * Export builder positions as a DLPack-managed tensor.
 *
 * On success the caller-supplied `*out_tensor` is set to a newly-
 * allocated `DLManagedTensorVersioned*` that owns a clone of the
 * builder's `(N, 3) f64` row-major positions buffer. The caller MUST
 * invoke `(*out_tensor)->deleter(*out_tensor)` to release it.
 *
 * # Safety
 * `builder_handle` must be a valid builder handle; `out_tensor` must
 * be a valid pointer to a writable `*mut DLManagedTensorVersioned`.
 */
enum RKRStatus rkr_frame_builder_positions_dlpack(const struct RKRConFrameBuilder *builder_handle,
                                                  RKRDLManagedTensorVersioned **out_tensor);

/**
 * Like [`rkr_frame_builder_positions_dlpack`] with explicit precision/device.
 *
 * `opts` may be NULL (float64 / CPU). See [`RKRDlpackExportOptions`].
 *
 * # Safety
 * Same as the non-`_ex` entry; `opts` must be null or point at a valid struct.
 */
enum RKRStatus rkr_frame_builder_positions_dlpack_ex(const struct RKRConFrameBuilder *builder_handle,
                                                     const struct RKRDlpackExportOptions *opts,
                                                     RKRDLManagedTensorVersioned **out_tensor);

/**
 * Export builder velocities as a DLPack-managed tensor.
 *
 * Returns `RKR_STATUS_SECTION_ABSENT` if the velocities section is not
 * declared; otherwise `(N, 3) f64`. See positions_dlpack for ownership
 * semantics.
 *
 * # Safety
 * `builder_handle` must be a valid builder handle; `out_tensor` must
 * be a valid pointer to a writable `*mut DLManagedTensorVersioned`.
 */
enum RKRStatus rkr_frame_builder_velocities_dlpack(const struct RKRConFrameBuilder *builder_handle,
                                                   RKRDLManagedTensorVersioned **out_tensor);

enum RKRStatus rkr_frame_builder_velocities_dlpack_ex(const struct RKRConFrameBuilder *builder_handle,
                                                      const struct RKRDlpackExportOptions *opts,
                                                      RKRDLManagedTensorVersioned **out_tensor);

/**
 * Export builder forces as a DLPack-managed tensor.
 *
 * Returns `RKR_STATUS_SECTION_ABSENT` if the forces section is not
 * declared.
 *
 * # Safety
 * `builder_handle` must be a valid builder handle; `out_tensor` must
 * be a valid pointer to a writable `*mut DLManagedTensorVersioned`.
 */
enum RKRStatus rkr_frame_builder_forces_dlpack(const struct RKRConFrameBuilder *builder_handle,
                                               RKRDLManagedTensorVersioned **out_tensor);

enum RKRStatus rkr_frame_builder_forces_dlpack_ex(const struct RKRConFrameBuilder *builder_handle,
                                                  const struct RKRDlpackExportOptions *opts,
                                                  RKRDLManagedTensorVersioned **out_tensor);

/**
 * Export builder per-atom energies as a DLPack-managed tensor.
 *
 * Returns `RKR_STATUS_SECTION_ABSENT` if the energies section is not
 * declared; otherwise `(N,) f64`.
 *
 * # Safety
 * `builder_handle` must be a valid builder handle; `out_tensor` must
 * be a valid pointer to a writable `*mut DLManagedTensorVersioned`.
 */
enum RKRStatus rkr_frame_builder_atom_energies_dlpack(const struct RKRConFrameBuilder *builder_handle,
                                                      RKRDLManagedTensorVersioned **out_tensor);

enum RKRStatus rkr_frame_builder_atom_energies_dlpack_ex(const struct RKRConFrameBuilder *builder_handle,
                                                         const struct RKRDlpackExportOptions *opts,
                                                         RKRDLManagedTensorVersioned **out_tensor);

/**
 * Export builder per-atom masses as a DLPack-managed tensor `(N,) f64`.
 *
 * # Safety
 * `builder_handle` must be a valid builder handle; `out_tensor` must
 * be a valid pointer to a writable `*mut DLManagedTensorVersioned`.
 */
enum RKRStatus rkr_frame_builder_masses_dlpack(const struct RKRConFrameBuilder *builder_handle,
                                               RKRDLManagedTensorVersioned **out_tensor);

enum RKRStatus rkr_frame_builder_masses_dlpack_ex(const struct RKRConFrameBuilder *builder_handle,
                                                  const struct RKRDlpackExportOptions *opts,
                                                  RKRDLManagedTensorVersioned **out_tensor);

/**
 * Export builder per-atom ids as a DLPack-managed tensor `(N,) u64`.
 *
 * # Safety
 * `builder_handle` must be a valid builder handle; `out_tensor` must
 * be a valid pointer to a writable `*mut DLManagedTensorVersioned`.
 */
enum RKRStatus rkr_frame_builder_atom_ids_dlpack(const struct RKRConFrameBuilder *builder_handle,
                                                 RKRDLManagedTensorVersioned **out_tensor);

enum RKRStatus rkr_frame_builder_atom_ids_dlpack_ex(const struct RKRConFrameBuilder *builder_handle,
                                                    const struct RKRDlpackExportOptions *opts,
                                                    RKRDLManagedTensorVersioned **out_tensor);

/**
 * Borrow the positions buffer as a raw `(N, 3) f64` row-major pointer.
 * Returns NULL on invalid handle. Pointer is valid until the builder
 * is dropped or `add_atom` reallocates.
 *
 * # Safety
 * builder_handle must be valid; the returned pointer must not be
 * dereferenced after a call to add_atom on the same builder.
 */
double *rkr_frame_builder_positions_data(struct RKRConFrameBuilder *builder_handle);

/**
 * Borrow the velocities buffer as a raw `(N, 3) f64` row-major pointer.
 * Returns NULL if the velocities section is absent or the handle is
 * invalid.
 *
 * # Safety
 * Same contract as rkr_frame_builder_positions_data.
 */
double *rkr_frame_builder_velocities_data(struct RKRConFrameBuilder *builder_handle);

/**
 * Borrow the forces buffer as a raw `(N, 3) f64` row-major pointer.
 * Returns NULL if the forces section is absent.
 *
 * # Safety
 * Same contract as rkr_frame_builder_positions_data.
 */
double *rkr_frame_builder_forces_data(struct RKRConFrameBuilder *builder_handle);

/**
 * Borrow the per-atom energies buffer as a raw `(N,) f64` pointer.
 * Returns NULL if the energies section is absent.
 *
 * # Safety
 * Same contract as rkr_frame_builder_positions_data.
 */
double *rkr_frame_builder_atom_energies_data(struct RKRConFrameBuilder *builder_handle);

/**
 * Borrow the per-atom masses buffer as a raw `(N,) f64` pointer.
 *
 * # Safety
 * Same contract as rkr_frame_builder_positions_data.
 */
double *rkr_frame_builder_masses_data(struct RKRConFrameBuilder *builder_handle);

/**
 * Borrow the per-atom atom_ids buffer as a raw `(N,) u64` pointer.
 *
 * # Safety
 * Same contract as rkr_frame_builder_positions_data.
 */
const uint64_t *rkr_frame_builder_atom_ids_data(const struct RKRConFrameBuilder *builder_handle);

/**
 * Adds an atom with optional per-axis fixed mask, velocity, and force vectors.
 *
 * `velocity` and `force` are pointers to 3 contiguous f64 values, or NULL if
 * absent. This is the unified entry point that replaces the eight
 * `rkr_frame_add_atom_*` convenience functions; callers may continue using
 * those for source compatibility.
 *
 * # Safety
 * builder_handle and symbol must be valid. velocity (if non-null) must point
 * to 3 contiguous f64 values, and force (if non-null) likewise.
 */
enum RKRStatus rkr_frame_add_atom_full(struct RKRConFrameBuilder *builder_handle,
                                       const char *symbol,
                                       double x,
                                       double y,
                                       double z,
                                       bool fixed_x,
                                       bool fixed_y,
                                       bool fixed_z,
                                       uint64_t atom_id,
                                       double mass,
                                       const double *velocity,
                                       const double *force);

/**
 * Creates a new frame builder with the given cell dimensions, angles,
 * and header lines.
 *
 * `prebox1` is accepted for source compatibility but ignored: the
 * JSON metadata line is regenerated by the writer from the builder's
 * `spec_version`, `metadata`, and `sections`. Pass NULL or any string.
 * The caller OWNS the returned pointer and MUST call
 * `free_rkr_frame_builder` or consume it via `rkr_frame_builder_build`.
 * Returns NULL on error.
 *
 * # Safety
 * cell and angles must point to 3 doubles. prebox0, postbox0, and
 * postbox1 must be NULL or valid null-terminated strings; prebox1 is
 * not dereferenced. The caller takes ownership of the returned
 * builder.
 */
struct RKRConFrameBuilder *rkr_frame_new(const double *cell,
                                         const double *angles,
                                         const char *prebox0,
                                         const char *prebox1,
                                         const char *postbox0,
                                         const char *postbox1);

/**
 * Parses and sets JSON metadata on an existing frame builder.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and metadata_json must be valid.
 */
enum RKRStatus rkr_frame_builder_set_metadata_json(struct RKRConFrameBuilder *builder_handle,
                                                   const char *metadata_json);

/**
 * Sets a numeric metadata key on an existing frame builder.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and key must be valid.
 */
enum RKRStatus rkr_frame_builder_set_scalar_metadata(struct RKRConFrameBuilder *builder_handle,
                                                     const char *key,
                                                     double value);

/**
 * Sets a string metadata key on an existing frame builder.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle, key, and value must be valid.
 */
enum RKRStatus rkr_frame_builder_set_string_metadata(struct RKRConFrameBuilder *builder_handle,
                                                     const char *key,
                                                     const char *value);

/**
 * Sets the per-frame total energy metadata on an existing frame builder.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_energy(struct RKRConFrameBuilder *builder_handle,
                                            double energy);

/**
 * Sets the zero-based frame index metadata on an existing frame builder.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_frame_index(struct RKRConFrameBuilder *builder_handle,
                                                 uint64_t idx);

/**
 * Sets the simulation time metadata on an existing frame builder.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_time(struct RKRConFrameBuilder *builder_handle,
                                          double time);

/**
 * Sets the timestep metadata on an existing frame builder.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_timestep(struct RKRConFrameBuilder *builder_handle,
                                              double dt);

/**
 * Sets the NEB bead index metadata on an existing frame builder.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_neb_bead(struct RKRConFrameBuilder *builder_handle,
                                              uint64_t bead);

/**
 * Sets the NEB band index metadata on an existing frame builder.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle must be valid.
 */
enum RKRStatus rkr_frame_builder_set_neb_band(struct RKRConFrameBuilder *builder_handle,
                                              uint64_t band);

/**
 * **Deprecated**: prefer `rkr_frame_add_atom_full` with NULL velocity
 * and force pointers. Adds an atom (no velocity, no forces) to the
 * builder using a single uniform fixed flag.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and symbol must be valid.
 */
enum RKRStatus rkr_frame_add_atom(struct RKRConFrameBuilder *builder_handle,
                                  const char *symbol,
                                  double x,
                                  double y,
                                  double z,
                                  bool is_fixed,
                                  uint64_t atom_id,
                                  double mass);

/**
 * **Deprecated**: prefer `rkr_frame_add_atom_full`. Adds an atom (no
 * velocity, no forces) using per-axis fixed flags.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and symbol must be valid.
 */
enum RKRStatus rkr_frame_add_atom_with_fixed_mask(struct RKRConFrameBuilder *builder_handle,
                                                  const char *symbol,
                                                  double x,
                                                  double y,
                                                  double z,
                                                  bool fixed_x,
                                                  bool fixed_y,
                                                  bool fixed_z,
                                                  uint64_t atom_id,
                                                  double mass);

/**
 * **Deprecated**: prefer `rkr_frame_add_atom_full`. Adds an atom with
 * a velocity vector and a single uniform fixed flag.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and symbol must be valid.
 */
enum RKRStatus rkr_frame_add_atom_with_velocity(struct RKRConFrameBuilder *builder_handle,
                                                const char *symbol,
                                                double x,
                                                double y,
                                                double z,
                                                bool is_fixed,
                                                uint64_t atom_id,
                                                double mass,
                                                double vx,
                                                double vy,
                                                double vz);

/**
 * **Deprecated**: prefer `rkr_frame_add_atom_full`. Adds an atom with
 * a velocity vector and per-axis fixed flags.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and symbol must be valid.
 */
enum RKRStatus rkr_frame_add_atom_with_velocity_fixed_mask(struct RKRConFrameBuilder *builder_handle,
                                                           const char *symbol,
                                                           double x,
                                                           double y,
                                                           double z,
                                                           bool fixed_x,
                                                           bool fixed_y,
                                                           bool fixed_z,
                                                           uint64_t atom_id,
                                                           double mass,
                                                           double vx,
                                                           double vy,
                                                           double vz);

/**
 * **Deprecated**: prefer `rkr_frame_add_atom_full`. Adds an atom with
 * a force vector and a single uniform fixed flag.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and symbol must be valid.
 */
enum RKRStatus rkr_frame_add_atom_with_forces(struct RKRConFrameBuilder *builder_handle,
                                              const char *symbol,
                                              double x,
                                              double y,
                                              double z,
                                              bool is_fixed,
                                              uint64_t atom_id,
                                              double mass,
                                              double fx,
                                              double fy,
                                              double fz);

/**
 * **Deprecated**: prefer `rkr_frame_add_atom_full`. Adds an atom with
 * a force vector and per-axis fixed flags.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and symbol must be valid.
 */
enum RKRStatus rkr_frame_add_atom_with_forces_fixed_mask(struct RKRConFrameBuilder *builder_handle,
                                                         const char *symbol,
                                                         double x,
                                                         double y,
                                                         double z,
                                                         bool fixed_x,
                                                         bool fixed_y,
                                                         bool fixed_z,
                                                         uint64_t atom_id,
                                                         double mass,
                                                         double fx,
                                                         double fy,
                                                         double fz);

/**
 * **Deprecated**: prefer `rkr_frame_add_atom_full`. Adds an atom with
 * both velocity and force vectors and a single uniform fixed flag.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and symbol must be valid.
 */
enum RKRStatus rkr_frame_add_atom_with_velocity_and_forces(struct RKRConFrameBuilder *builder_handle,
                                                           const char *symbol,
                                                           double x,
                                                           double y,
                                                           double z,
                                                           bool is_fixed,
                                                           uint64_t atom_id,
                                                           double mass,
                                                           double vx,
                                                           double vy,
                                                           double vz,
                                                           double fx,
                                                           double fy,
                                                           double fz);

/**
 * **Deprecated**: prefer `rkr_frame_add_atom_full`. Adds an atom with
 * both velocity and force vectors and per-axis fixed flags.
 * Returns `RKR_STATUS_SUCCESS` on success, or an error code.
 *
 * # Safety
 * builder_handle and symbol must be valid.
 */
enum RKRStatus rkr_frame_add_atom_with_velocity_and_forces_fixed_mask(struct RKRConFrameBuilder *builder_handle,
                                                                      const char *symbol,
                                                                      double x,
                                                                      double y,
                                                                      double z,
                                                                      bool fixed_x,
                                                                      bool fixed_y,
                                                                      bool fixed_z,
                                                                      uint64_t atom_id,
                                                                      double mass,
                                                                      double vx,
                                                                      double vy,
                                                                      double vz,
                                                                      double fx,
                                                                      double fy,
                                                                      double fz);

/**
 * Consumes the builder and returns a finalized RKRConFrame handle.
 * The builder handle is invalidated after this call.
 * The caller OWNS the returned frame and MUST call `free_rkr_frame`.
 * Returns NULL on error.
 *
 * # Safety
 * builder_handle must be valid. The caller takes ownership of the returned frame.
 */
struct RKRConFrame *rkr_frame_builder_build(struct RKRConFrameBuilder *builder_handle);

/**
 * Frees a frame builder without building.
 *
 * # Safety
 * builder_handle must be valid or null.
 */
void free_rkr_frame_builder(struct RKRConFrameBuilder *builder_handle);

/**
 * Cheap, copy-on-write clone of a frame builder. Returned handle owns
 * a new `ConFrameBuilder` whose per-atom buffers share storage with
 * the source via ArcArray; any subsequent mutation triggers a
 * per-buffer copy-on-write so writes do not leak across clones.
 *
 * Intended for downstream consumers (NEB image bulk allocation,
 * trajectory snapshots) that need many builders carrying the same
 * per-atom data without paying N copies up-front. Returns NULL on
 * NULL input.
 *
 * The caller OWNS the returned handle and MUST call
 * `free_rkr_frame_builder` (or consume via `rkr_frame_builder_build`).
 *
 * # Safety
 * `builder_handle` must be a valid pointer returned by `rkr_frame_new`
 * (or by an earlier `rkr_frame_builder_clone`) and not yet freed.
 */
struct RKRConFrameBuilder *rkr_frame_builder_clone(const struct RKRConFrameBuilder *builder_handle);

/**
 * Creates a new gzip-compressed frame writer for the specified file.
 * The caller OWNS the returned pointer and MUST call `free_rkr_writer`.
 *
 * # Safety
 * filename_c must be valid. The caller takes ownership of the returned writer.
 */
struct RKRConFrameWriter *create_writer_gzip_c(const char *filename_c);

/**
 * Creates a gzip-compressed frame writer with a custom floating-point
 * precision. The caller OWNS the returned pointer and MUST call
 * `free_rkr_writer`.
 *
 * # Safety
 * filename_c must be valid. The caller takes ownership of the returned writer.
 */
struct RKRConFrameWriter *create_writer_gzip_with_precision_c(const char *filename_c,
                                                              uint8_t precision);

#if defined(READCON_CORE_HAS_ZSTD)
/**
 * Creates a new zstd-compressed frame writer for the specified file.
 * The caller OWNS the returned pointer and MUST call `free_rkr_writer`.
 *
 * Only present when readcon-core is built with the `zstd` Cargo
 * feature; the C header guards the declaration with
 * `READCON_CORE_HAS_ZSTD`.
 *
 * # Safety
 * filename_c must be valid. The caller takes ownership of the returned writer.
 */
struct RKRConFrameWriter *create_writer_zstd_c(const char *filename_c);
#endif

#if defined(READCON_CORE_HAS_ZSTD)
/**
 * Creates a zstd-compressed frame writer with a custom floating-point
 * precision. The caller OWNS the returned pointer and MUST call
 * `free_rkr_writer`.
 *
 * Only present when readcon-core is built with the `zstd` Cargo
 * feature; the C header guards the declaration with
 * `READCON_CORE_HAS_ZSTD`.
 *
 * # Safety
 * filename_c must be valid. The caller takes ownership of the returned writer.
 */
struct RKRConFrameWriter *create_writer_zstd_with_precision_c(const char *filename_c,
                                                              uint8_t precision);
#endif

/**
 * Reads the first frame from a .con file.
 * Uses `read_to_string` for small files (< 64 KiB) and mmap for larger ones.
 * Stops after the first frame rather than parsing the entire file.
 * The caller OWNS the returned handle and MUST call `free_rkr_frame`.
 * Returns NULL on error.
 *
 * # Safety
 * filename_c must be valid. The caller takes ownership of the returned frame.
 */
struct RKRConFrame *rkr_read_first_frame(const char *filename_c);

/**
 * Reads all frames from a .con file using mmap.
 * Returns an array of frame handles and sets `num_frames` to the count.
 * The caller OWNS both the array and each frame handle.
 * Free frames with `free_rkr_frame` and the array with `free_rkr_frame_array`.
 * Returns NULL on error.
 *
 * # Safety
 * filename_c and num_frames must be valid. The caller takes ownership of the returned handles and array.
 */
struct RKRConFrame **rkr_read_all_frames(const char *filename_c,
                                         uintptr_t *num_frames);

/**
 * Frees an array of frame handles returned by `rkr_read_all_frames`.
 * Each frame is freed individually, then the array itself.
 *
 * # Safety
 * frames must be valid or null.
 */
void free_rkr_frame_array(struct RKRConFrame **frames, uintptr_t num_frames);

/**
 * Free only the outer pointer array from `rkr_read_all_frames` (not the frames).
 *
 * # Safety
 * `frames` null or from `rkr_read_all_frames` with length `num_frames`. Frame
 * pointers must be owned elsewhere (e.g. language wrappers).
 */
void free_rkr_frame_ptr_array(struct RKRConFrame **frames,
                              uintptr_t num_frames);

#if defined(READCON_CORE_HAS_METATENSOR)
/**
 * Free an owned block from `rkr_frame_metatensor_*_block`.
 * Prefer this or `mts_block_free` (metatensor.h) — not both on the same pointer.
 *
 * # Safety
 * `block` is NULL or an owning `mts_block_t*` from this library's transfer helper.
 */
void rkr_mts_block_free(struct mts_block_t *block);
#endif

#if defined(READCON_CORE_HAS_METATENSOR)
/**
 * Positions `[N,3]` TensorBlock. Caller frees with `rkr_mts_block_free` / `mts_block_free`.
 */
enum RKRStatus rkr_frame_metatensor_positions_block(const struct RKRConFrame *frame_handle,
                                                    struct mts_block_t **out_block);
#endif

#if defined(READCON_CORE_HAS_METATENSOR)
enum RKRStatus rkr_frame_metatensor_velocities_block(const struct RKRConFrame *frame_handle,
                                                     struct mts_block_t **out_block);
#endif

#if defined(READCON_CORE_HAS_METATENSOR)
enum RKRStatus rkr_frame_metatensor_forces_block(const struct RKRConFrame *frame_handle,
                                                 struct mts_block_t **out_block);
#endif

#if defined(READCON_CORE_HAS_METATENSOR)
enum RKRStatus rkr_frame_metatensor_atom_energies_block(const struct RKRConFrame *frame_handle,
                                                        struct mts_block_t **out_block);
#endif

#if !defined(READCON_CORE_HAS_ZSTD)
struct RKRConFrameWriter *create_writer_zstd_c(const char *_filename_c);
#endif

#if !defined(READCON_CORE_HAS_ZSTD)
struct RKRConFrameWriter *create_writer_zstd_with_precision_c(const char *_filename_c,
                                                              uint8_t _precision);
#endif

#if !defined(READCON_CORE_HAS_METATENSOR)
void rkr_mts_block_free(struct mts_block_t *_block);
#endif

#if !defined(READCON_CORE_HAS_METATENSOR)
enum RKRStatus rkr_frame_metatensor_positions_block(const struct RKRConFrame *_frame_handle,
                                                    struct mts_block_t **out_block);
#endif

#if !defined(READCON_CORE_HAS_METATENSOR)
enum RKRStatus rkr_frame_metatensor_velocities_block(const struct RKRConFrame *_frame_handle,
                                                     struct mts_block_t **out_block);
#endif

#if !defined(READCON_CORE_HAS_METATENSOR)
enum RKRStatus rkr_frame_metatensor_forces_block(const struct RKRConFrame *_frame_handle,
                                                 struct mts_block_t **out_block);
#endif

#if !defined(READCON_CORE_HAS_METATENSOR)
enum RKRStatus rkr_frame_metatensor_atom_energies_block(const struct RKRConFrame *_frame_handle,
                                                        struct mts_block_t **out_block);
#endif

/**
 * Number of atoms on the frame (atom_data order).
 */
uintptr_t rkr_frame_atom_count(const struct RKRConFrame *frame_handle);

/**
 * Copy positions as row-major `[x0,y0,z0,...]` into `out` (length >= 3*N).
 */
enum RKRStatus rkr_frame_copy_positions(const struct RKRConFrame *frame_handle,
                                        double *out,
                                        uintptr_t out_len);

enum RKRStatus rkr_frame_copy_velocities(const struct RKRConFrame *frame_handle,
                                         double *out,
                                         uintptr_t out_len);

enum RKRStatus rkr_frame_copy_forces(const struct RKRConFrame *frame_handle,
                                     double *out,
                                     uintptr_t out_len);

enum RKRStatus rkr_frame_copy_atom_energies(const struct RKRConFrame *frame_handle,
                                            double *out,
                                            uintptr_t out_len);

enum RKRStatus rkr_frame_copy_masses(const struct RKRConFrame *frame_handle,
                                     double *out,
                                     uintptr_t out_len);

enum RKRStatus rkr_frame_copy_atom_ids(const struct RKRConFrame *frame_handle,
                                       uint64_t *out,
                                       uintptr_t out_len);

/**
 * Metatensor-style: export positions as they are stored (CPU f64), with
 * explicit device request. Non-CPU → `FEATURE_DISABLED`. Prefer this over
 * dtype-cast `*_dlpack_ex` for new code.
 *
 * `stream` and `max_version_*` are accepted for ABI alignment with
 * metatensor `as_dlpack`; CPU ignores stream / version negotiation for now.
 *
 * # Safety
 * Handles and `out_tensor` must be valid.
 */
enum RKRStatus rkr_frame_positions_as_dlpack(const struct RKRConFrame *frame_handle,
                                             int32_t device_type,
                                             int32_t device_id,
                                             int64_t _stream,
                                             uint32_t _max_version_major,
                                             uint32_t _max_version_minor,
                                             RKRDLManagedTensorVersioned **out_tensor);

/**
 * Ingest positions from a DLManagedTensorVersioned (CPU float32/64, shape (N,3)
 * or length 3N). Metatensor-style write path symmetry.
 *
 * # Safety
 * `frame` must be a valid mutable frame; `tensor` non-null managed tensor.
 */
enum RKRStatus rkr_frame_positions_from_dlpack(struct RKRConFrame *frame_handle,
                                               const RKRDLManagedTensorVersioned *tensor);

/**
 * DLPack positions from a frame (default float64 / CPU). Prefer
 * [`rkr_frame_positions_as_dlpack`] for metatensor-style device negotiation.
 */
enum RKRStatus rkr_frame_positions_dlpack(const struct RKRConFrame *frame_handle,
                                          RKRDLManagedTensorVersioned **out_tensor);

/**
 * Frame positions with [`RKRDlpackExportOptions`] (`opts` NULL → f64/CPU).
 *
 * # Safety
 * `frame_handle` / `out_tensor` valid; `opts` null or valid.
 */
enum RKRStatus rkr_frame_positions_dlpack_ex(const struct RKRConFrame *frame_handle,
                                             const struct RKRDlpackExportOptions *opts,
                                             RKRDLManagedTensorVersioned **out_tensor);

/**
 * DLPack velocities from a frame, or `SECTION_ABSENT` if missing (f64/CPU).
 */
enum RKRStatus rkr_frame_velocities_dlpack(const struct RKRConFrame *frame_handle,
                                           RKRDLManagedTensorVersioned **out_tensor);

enum RKRStatus rkr_frame_velocities_dlpack_ex(const struct RKRConFrame *frame_handle,
                                              const struct RKRDlpackExportOptions *opts,
                                              RKRDLManagedTensorVersioned **out_tensor);

/**
 * DLPack forces from a frame, or `SECTION_ABSENT` if missing (f64/CPU default).
 */
enum RKRStatus rkr_frame_forces_dlpack(const struct RKRConFrame *frame_handle,
                                       RKRDLManagedTensorVersioned **out_tensor);

enum RKRStatus rkr_frame_forces_dlpack_ex(const struct RKRConFrame *frame_handle,
                                          const struct RKRDlpackExportOptions *opts,
                                          RKRDLManagedTensorVersioned **out_tensor);

/**
 * DLPack per-atom energies, or `SECTION_ABSENT` if missing (f64/CPU default).
 */
enum RKRStatus rkr_frame_atom_energies_dlpack(const struct RKRConFrame *frame_handle,
                                              RKRDLManagedTensorVersioned **out_tensor);

enum RKRStatus rkr_frame_atom_energies_dlpack_ex(const struct RKRConFrame *frame_handle,
                                                 const struct RKRDlpackExportOptions *opts,
                                                 RKRDLManagedTensorVersioned **out_tensor);

/**
 * Evaluate a chemfiles selection-language string on an `RKRConFrame`.
 *
 * On success writes a heap-allocated result handle to `*out_result` (caller
 * frees with [`rkr_selection_result_free`]). Returns
 * `RKR_STATUS_SELECTION_ERROR` for invalid grammar, evaluation failure, or
 * when this build was compiled without the `chemfiles` feature.
 *
 * # Safety
 * `frame_handle`, `selection`, and `out_result` must be non-null; `selection`
 * must point to a valid UTF-8 C string.
 */
enum RKRStatus rkr_frame_select(const struct RKRConFrame *frame_handle,
                                const char *selection,
                                struct RKRSelectionResult **out_result);

/**
 * Number of matches in a selection result.
 *
 * # Safety
 * `result_handle` must be a valid handle from [`rkr_frame_select`] or NULL.
 */
uint64_t rkr_selection_result_match_count(const struct RKRSelectionResult *result_handle);

/**
 * Selection context size (1=atom, 2=pair, 3=angle, 4=dihedral).
 *
 * # Safety
 * `result_handle` must be valid or NULL (returns 0).
 */
uint32_t rkr_selection_result_context_size(const struct RKRSelectionResult *result_handle);

/**
 * Copy match `match_index` atom indices into `out_atoms` (up to 4 slots).
 * Writes actual arity to `*out_size` when non-null.
 *
 * # Safety
 * Handles and `out_atoms` must be valid; `out_atoms` needs space for 4 `uint64_t`.
 */
enum RKRStatus rkr_selection_result_match_at(const struct RKRSelectionResult *result_handle,
                                             uint64_t match_index,
                                             uint64_t *out_atoms,
                                             uint32_t *out_size);

/**
 * Fill `out_indices` with primary atom indices for each match (length =
 * match count). Returns `RKR_STATUS_BUFFER_TOO_SMALL` if `capacity` is too small.
 *
 * # Safety
 * `result_handle` and `out_indices` must be valid when capacity > 0.
 */
enum RKRStatus rkr_selection_result_primary_indices(const struct RKRSelectionResult *result_handle,
                                                    uint64_t *out_indices,
                                                    uint64_t capacity,
                                                    uint64_t *out_written);

/**
 * Free a selection result from [`rkr_frame_select`]. Safe with NULL.
 *
 * # Safety
 * `result_handle` must be from `rkr_frame_select` or NULL.
 */
void rkr_selection_result_free(struct RKRSelectionResult *result_handle);

/**
 * Returns 1 when this library build includes chemfiles selection support.
 */
uint8_t rkr_has_chemfiles_support(void);

/**
 * Read the first frame from a chemfiles-supported path (XYZ, PDB, GRO, …).
 * Returns NULL on error or without the `chemfiles` feature. Caller: `free_rkr_frame`.
 *
 * # Safety
 * `path_c` must be a valid NUL-terminated UTF-8 path.
 */
struct RKRConFrame *rkr_read_chemfiles_first(const char *path_c);

/**
 * Read all frames from memory with chemfiles `format` (e.g. `"XYZ"`).
 * Sets `*num_frames`. Free frames with `free_rkr_frame` and the array with
 * `free_rkr_frame_array`. NULL on error / without chemfiles.
 *
 * # Safety
 * `data_c`, `format_c` valid UTF-8 C strings; `num_frames` non-null.
 */
struct RKRConFrame **rkr_read_chemfiles_memory(const char *data_c,
                                               const char *format_c,
                                               uintptr_t *num_frames);

/**
 * Free a DLPack tensor from `rkr_frame_builder_*_dlpack` (calls deleter). Safe with NULL.
 *
 * # Safety
 * `tensor` must be NULL or a pointer from a dlpack export of this library.
 */
void rkr_dlpack_delete(RKRDLManagedTensorVersioned *tensor);

#ifdef __cplusplus
}  // extern "C"
#endif  // __cplusplus

#ifdef __cplusplus
}  // namespace readcon
#endif  // __cplusplus

#endif  /* READCON_H */