ison-graph 1.4.1

ISONGraph - Token-efficient property graph store with ISON persistence for Rust
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
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
//! ISONGraph - A Token-Efficient Graph Store for Rust
//!
//! A property graph implementation with ISON persistence.
//! Supports multi-hop traversal, path finding, and fluent API.
//!
//! # Example
//!
//! ```rust
//! use ison_graph::{ISONGraph, NodeRef, Direction, props};
//!
//! let mut graph = ISONGraph::new("social");
//! // Properties keep their type: 30 is a number, "30" would be a string,
//! // and the two serialize differently.
//! graph.add_node("person", "1", props![("name", "Alice"), ("age", 30i64)]).unwrap();
//! graph.add_node("person", "2", props![("name", "Bob"), ("age", 25i64)]).unwrap();
//! graph.add_edge("KNOWS", ("person", "1"), ("person", "2"), props![("since", 2020i64)]).unwrap();
//!
//! let friends = graph.neighbors(&("person", "1"), Some("KNOWS"), Direction::Out);
//! let fof = graph.multi_hop(&("person", "1"), Some("KNOWS"), 2, Direction::Out);
//! ```

use std::collections::{HashMap, HashSet, VecDeque};
use std::fs;
use std::path::Path as StdPath;
use std::sync::OnceLock;
use ison_rs::{
    dumps_canonical, dumps_canonical_isonl, loads, loads_isonl,
    Block as IsonBlock, Document as IsonDocument, Reference as IsonReference,
};

/// Converts a Rust primitive into an ISON property value.
///
/// `ison_rs::Value` has no `From` impls and the orphan rule stops this crate
/// adding them, so the conversions live here. This is sugar only - the value
/// stored is always `ison_rs::Value`, never a parallel model.
pub trait IntoValue {
    fn into_value(self) -> Value;
}

impl IntoValue for Value {
    fn into_value(self) -> Value { self }
}
impl IntoValue for &str {
    fn into_value(self) -> Value { Value::String(self.to_string()) }
}
impl IntoValue for String {
    fn into_value(self) -> Value { Value::String(self) }
}
impl IntoValue for i64 {
    fn into_value(self) -> Value { Value::Int(self) }
}
impl IntoValue for i32 {
    fn into_value(self) -> Value { Value::Int(self as i64) }
}
impl IntoValue for f64 {
    fn into_value(self) -> Value { Value::Float(self) }
}
impl IntoValue for bool {
    fn into_value(self) -> Value { Value::Bool(self) }
}

/// Build a property list with mixed value types.
///
/// A `Vec` is homogeneous, so a literal mixing an int and a string cannot be
/// written directly; this converts each value before collecting, which is
/// what makes `props![("staff", 42), ("name", "Acme")]` compile.
#[macro_export]
macro_rules! props {
    () => { ::std::vec::Vec::<(&str, $crate::Value)>::new() };
    ($(($k:expr, $v:expr)),* $(,)?) => {
        ::std::vec![$(($k, $crate::IntoValue::into_value($v))),*]
    };
}

/// Render a property value as text - the form every value had before
/// properties carried types. Query string operators and schema validation
/// use it so their behaviour is unchanged by typing the storage.
pub fn value_to_text(value: &Value) -> String {
    match value {
        Value::Null => String::new(),
        Value::Bool(b) => b.to_string(),
        Value::Int(i) => i.to_string(),
        Value::Float(f) => f.to_string(),
        Value::String(s) => s.clone(),
        Value::Reference(r) => r.to_ison(),
    }
}

/// Property values are ISON values, re-exported so callers do not need a
/// direct ison-rs dependency. Storing anything else would mean a second
/// value model beside the parser's, which is what made this port emit every
/// value as a quoted string and disagree with the typed ports.
pub use ison_rs::Value;
use ison_rs::Value as IsonValue;
use thiserror::Error;
use regex::Regex;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

/// Version of the library
pub const VERSION: &str = env!("CARGO_PKG_VERSION");

// Query module (ISONQL)
pub mod query;

// Schema validation module
pub mod schema;

// =============================================================================
// Types
// =============================================================================

/// Node reference: (type, id)
pub type NodeRef<'a> = (&'a str, &'a str);

/// Owned node reference
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct NodeId {
    pub node_type: String,
    pub id: String,
}

impl NodeId {
    pub fn new(node_type: impl Into<String>, id: impl Into<String>) -> Self {
        Self {
            node_type: node_type.into(),
            id: id.into(),
        }
    }

    pub fn as_ref(&self) -> NodeRef<'_> {
        (&self.node_type, &self.id)
    }

    pub fn to_key(&self) -> String {
        format!("{}:{}", self.node_type, self.id)
    }

    pub fn to_ison_ref(&self) -> String {
        format!(":{}:{}", self.node_type, self.id)
    }
}

impl From<(&str, &str)> for NodeId {
    fn from(tuple: (&str, &str)) -> Self {
        NodeId::new(tuple.0, tuple.1)
    }
}

/// Traversal direction
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub enum Direction {
    Out,
    In,
    Both,
}

// =============================================================================
// Data Structures
// =============================================================================

/// Represents a graph node with properties
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Node {
    pub node_type: String,
    pub id: String,
    pub properties: HashMap<String, Value>,
}

impl Node {
    pub fn new(node_type: impl Into<String>, id: impl Into<String>) -> Self {
        Self {
            node_type: node_type.into(),
            id: id.into(),
            properties: HashMap::new(),
        }
    }

    pub fn with_properties(mut self, props: Vec<(&str, Value)>) -> Self {
        for (k, v) in props {
            self.properties.insert(k.to_string(), v);
        }
        self
    }

    pub fn node_id(&self) -> NodeId {
        NodeId::new(&self.node_type, &self.id)
    }

    pub fn to_key(&self) -> String {
        format!("{}:{}", self.node_type, self.id)
    }
}

/// Represents a graph edge with properties
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Edge {
    pub rel_type: String,
    pub source: NodeId,
    pub target: NodeId,
    pub properties: HashMap<String, Value>,
}

impl Edge {
    pub fn new(rel_type: impl Into<String>, source: NodeId, target: NodeId) -> Self {
        Self {
            rel_type: rel_type.into(),
            source,
            target,
            properties: HashMap::new(),
        }
    }

    pub fn with_properties(mut self, props: Vec<(&str, Value)>) -> Self {
        for (k, v) in props {
            self.properties.insert(k.to_string(), v);
        }
        self
    }

    pub fn to_key(&self) -> String {
        format!(
            "{}:{}:{}",
            self.rel_type,
            self.source.to_key(),
            self.target.to_key()
        )
    }
}

/// Represents a path through the graph
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
pub struct Path {
    pub nodes: Vec<NodeId>,
    pub edges: Vec<Edge>,
}

impl Path {
    pub fn new(nodes: Vec<NodeId>, edges: Vec<Edge>) -> Self {
        Self { nodes, edges }
    }

    /// Number of hops in the path
    pub fn length(&self) -> usize {
        self.edges.len()
    }

    /// Starting node
    pub fn start(&self) -> Option<&NodeId> {
        self.nodes.first()
    }

    /// Ending node
    pub fn end(&self) -> Option<&NodeId> {
        self.nodes.last()
    }
}

// =============================================================================
// Errors
// =============================================================================

#[derive(Error, Debug)]
pub enum GraphError {
    #[error("Node not found: :{0}:{1}")]
    NodeNotFound(String, String),

    #[error("Edge not found: {0} -> {1}")]
    EdgeNotFound(String, String),

    #[error("Duplicate node: :{0}:{1}")]
    DuplicateNode(String, String),

    #[error("Duplicate edge: {0}")]
    DuplicateEdge(String),

    #[error("Parse error: {0}")]
    ParseError(String),

    #[error("{0}")]
    InvalidIdentifier(String),

    /// A property value cannot survive a serialization round trip. ISON has
    /// no spelling for a non-finite float: NaN and infinity are written bare
    /// and read back as the *strings* "nan" / "inf".
    #[error("{0}")]
    InvalidPropertyValue(String),

    #[error("I/O error: {0}")]
    Io(#[from] std::io::Error),
}

pub type Result<T> = std::result::Result<T, GraphError>;

/// Written into `meta.graph` by the Python `MultiGraph`. This port has no
/// multigraph, so loading such a document would silently drop its parallel
/// edges; it is refused instead.
pub const MULTIGRAPH_FORMAT: &str = "ison-graph-multigraph-v1";

// =============================================================================
// ISONGraph - Main Graph Structure
// =============================================================================

/// In-memory property graph store with ISON persistence.
///
/// Features:
/// - Property graph model (nodes and edges with properties)
/// - Multiple node types and relationship types
/// - O(1) node lookup by (type, id)
/// - Multi-hop traversal
/// - Shortest path finding (BFS)
/// - All paths finding (DFS)
/// - ISON/ISONL persistence
#[derive(Debug)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serde", serde(try_from = "GraphData"))]
pub struct ISONGraph {
    /// Graph name
    pub name: String,

    /// Whether edges are directed
    pub directed: bool,

    /// Node storage: type -> id -> Node
    nodes: HashMap<String, HashMap<String, Node>>,

    /// Edge storage: rel_type -> Vec<Edge>
    edges: HashMap<String, Vec<Edge>>,

    /// Index: outgoing edges per node (node_key -> edges).
    /// Derived from `edges`; not serialized, rebuilt on deserialization.
    #[cfg_attr(feature = "serde", serde(skip))]
    out_edges: HashMap<String, Vec<Edge>>,

    /// Index: incoming edges per node (node_key -> edges).
    /// Derived from `edges`; not serialized, rebuilt on deserialization.
    #[cfg_attr(feature = "serde", serde(skip))]
    in_edges: HashMap<String, Vec<Edge>>,

    /// Edge uniqueness set.
    /// Derived from `edges`; not serialized, rebuilt on deserialization.
    #[cfg_attr(feature = "serde", serde(skip))]
    edge_set: HashSet<String>,
}

/// Serde shadow struct: the persisted form of [`ISONGraph`] without the
/// derived indexes. Indexes are rebuilt after deserialization.
#[cfg(feature = "serde")]
#[derive(Deserialize)]
struct GraphData {
    name: String,
    directed: bool,
    nodes: HashMap<String, HashMap<String, Node>>,
    edges: HashMap<String, Vec<Edge>>,
}

#[cfg(feature = "serde")]
impl TryFrom<GraphData> for ISONGraph {
    type Error = GraphError;

    /// Deserialization bypasses `add_node`/`add_edge` (it reconstructs the
    /// index maps directly from untrusted JSON), so the ':' / '|'
    /// identifier guard has to be re-applied here explicitly - otherwise a
    /// crafted or corrupted payload could smuggle in identifiers that
    /// `add_node`/`add_edge` would have rejected, and the graph would only
    /// fail later, confusingly, on to_ison()/to_isonl().
    fn try_from(data: GraphData) -> Result<Self> {
        for (node_type, type_nodes) in &data.nodes {
            ISONGraph::check_identifier("Node type", node_type)?;
            for node_id in type_nodes.keys() {
                ISONGraph::check_identifier("Node id", node_id)?;
            }
        }
        for (rel_type, edges) in &data.edges {
            ISONGraph::check_identifier("Relationship type", rel_type)?;
            for edge in edges {
                ISONGraph::check_identifier("Node type", &edge.source.node_type)?;
                ISONGraph::check_identifier("Node id", &edge.source.id)?;
                ISONGraph::check_identifier("Node type", &edge.target.node_type)?;
                ISONGraph::check_identifier("Node id", &edge.target.id)?;
            }
        }

        let mut graph = ISONGraph {
            name: data.name,
            directed: data.directed,
            nodes: data.nodes,
            edges: data.edges,
            out_edges: HashMap::new(),
            in_edges: HashMap::new(),
            edge_set: HashSet::new(),
        };
        graph.rebuild_indexes();
        Ok(graph)
    }
}

impl Default for ISONGraph {
    fn default() -> Self {
        Self::new("graph")
    }
}

impl ISONGraph {
    /// Create a new graph
    pub fn new(name: impl Into<String>) -> Self {
        Self {
            name: name.into(),
            directed: true,
            nodes: HashMap::new(),
            edges: HashMap::new(),
            out_edges: HashMap::new(),
            in_edges: HashMap::new(),
            edge_set: HashSet::new(),
        }
    }

    /// Create an undirected graph
    pub fn undirected(name: impl Into<String>) -> Self {
        let mut graph = Self::new(name);
        graph.directed = false;
        graph
    }

    /// Rebuild the derived indexes (`out_edges`, `in_edges`, `edge_set`)
    /// from the canonical `edges` storage.
    #[cfg(feature = "serde")]
    fn rebuild_indexes(&mut self) {
        self.out_edges.clear();
        self.in_edges.clear();
        self.edge_set.clear();

        for edges in self.edges.values() {
            for edge in edges {
                let edge_key = edge.to_key();
                if !self.edge_set.insert(edge_key) {
                    continue;
                }
                self.out_edges
                    .entry(edge.source.to_key())
                    .or_default()
                    .push(edge.clone());
                self.in_edges
                    .entry(edge.target.to_key())
                    .or_default()
                    .push(edge.clone());
            }
        }
    }

    // =========================================================================
    // Node Operations
    // =========================================================================

    /// Add a node to the graph.
    ///
    /// Node keys are formed as `type:id`, so neither the type nor the id may
    /// contain `':'` (it would make keys ambiguous).
    /// Reject ':' or '|' in a node type, node id, or relationship type. Both
    /// are structurally significant to the wire format (':' delimits
    /// `:type:id` references, '|' delimits ISONL columns); allowing them
    /// here would build a graph that fails on to_ison()/to_isonl()
    /// round-trip instead of at construction.
    fn check_identifier(kind: &str, value: &str) -> Result<()> {
        // Types become block names, which ISONL additionally cannot quote;
        // ids only have to survive a reference, which has no quoting at all.
        let is_type = kind.to_lowercase().contains("type");
        let bad = value.is_empty()
            || value.chars().any(|ch| {
                ch.is_whitespace()
                    || ch == ':'
                    || ch == '|'
                    || (is_type && (ch == '"' || ch == '\\'))
            });
        if bad {
            return Err(GraphError::InvalidIdentifier(format!(
                "{} {:?} is not representable: it must not be empty or contain \
                 whitespace, ':' or '|'{}",
                kind,
                value,
                if is_type { ", '\"' or '\\'" } else { "" }
            )));
        }
        Ok(())
    }

    /// Reject property names that cannot survive a serialization round trip.
    ///
    /// Property names become ISON field names, and the format quotes values
    /// but not names: a space splits the field in two, ':' has the remainder
    /// eaten as a type annotation, '.' is a dot path, and a leading '#' turns
    /// the header into a comment and loses the whole block.
    fn check_property_names<'a, I>(kind: &str, names: I) -> Result<()>
    where
        I: IntoIterator<Item = &'a str>,
    {
        for name in names {
            let bad = name.is_empty()
                || name.starts_with('#')
                || name.chars().any(|ch| {
                    ch.is_whitespace()
                        || ch == ':'
                        || ch == '|'
                        || ch == '"'
                        || ch == '\\'
                        || ch == '.'
                });
            if bad {
                return Err(GraphError::InvalidIdentifier(format!(
                    "{} name {:?} cannot be encoded: property names must not be \
                     empty, start with '#', or contain whitespace, ':', '|', \
                     '\"', '\\' or '.'",
                    kind, name
                )));
            }
        }
        Ok(())
    }

    /// Reject property values that change type on a round trip; see
    /// [`GraphError::InvalidPropertyValue`].
    fn check_property_values<'a>(
        kind: &str,
        properties: impl Iterator<Item = (&'a str, &'a Value)>,
    ) -> Result<()> {
        for (name, value) in properties {
            if let Value::Float(f) = value {
                if !f.is_finite() {
                    return Err(GraphError::InvalidPropertyValue(format!(
                        "{} {:?} = {} cannot be encoded: a float must be finite \
                         (NaN and infinity are read back as strings).",
                        kind, name, f
                    )));
                }
            }
        }
        Ok(())
    }

    pub fn add_node(
        &mut self,
        node_type: &str,
        node_id: &str,
        properties: Vec<(&str, Value)>,
    ) -> Result<&Node> {
        Self::check_identifier("Node type", node_type)?;
        Self::check_identifier("Node id", node_id)?;
        Self::check_property_names("Node property", properties.iter().map(|(k, _)| *k))?;
        Self::check_property_values("Node property", properties.iter().map(|(k, v)| (*k, v)))?;

        let type_nodes = self.nodes.entry(node_type.to_string()).or_default();

        if type_nodes.contains_key(node_id) {
            return Err(GraphError::DuplicateNode(
                node_type.to_string(),
                node_id.to_string(),
            ));
        }

        let node = Node::new(node_type, node_id).with_properties(properties);
        type_nodes.insert(node_id.to_string(), node);
        Ok(type_nodes.get(node_id).unwrap())
    }

    /// Get a node by type and ID
    pub fn get_node(&self, node_type: &str, node_id: &str) -> Result<&Node> {
        self.nodes
            .get(node_type)
            .and_then(|m| m.get(node_id))
            .ok_or_else(|| GraphError::NodeNotFound(node_type.to_string(), node_id.to_string()))
    }

    /// Get a node by NodeId
    pub fn get_node_by_id(&self, node_id: &NodeId) -> Result<&Node> {
        self.get_node(&node_id.node_type, &node_id.id)
    }

    /// Check if node exists
    pub fn has_node(&self, node_type: &str, node_id: &str) -> bool {
        self.nodes
            .get(node_type)
            .map(|m| m.contains_key(node_id))
            .unwrap_or(false)
    }

    /// Remove a node and all its edges
    pub fn remove_node(&mut self, node_type: &str, node_id: &str) -> Result<()> {
        if !self.has_node(node_type, node_id) {
            return Err(GraphError::NodeNotFound(
                node_type.to_string(),
                node_id.to_string(),
            ));
        }

        let node_key = format!("{}:{}", node_type, node_id);

        // Collect edges to remove
        let out_edges: Vec<Edge> = self.out_edges.get(&node_key).cloned().unwrap_or_default();
        let in_edges: Vec<Edge> = self.in_edges.get(&node_key).cloned().unwrap_or_default();

        for edge in out_edges.iter().chain(in_edges.iter()) {
            self.remove_edge_internal(edge);
        }

        // Remove node
        if let Some(type_nodes) = self.nodes.get_mut(node_type) {
            type_nodes.remove(node_id);
            if type_nodes.is_empty() {
                self.nodes.remove(node_type);
            }
        }

        Ok(())
    }

    /// Update node properties
    pub fn update_node(
        &mut self,
        node_type: &str,
        node_id: &str,
        properties: Vec<(&str, Value)>,
    ) -> Result<()> {
        Self::check_property_names("Node property", properties.iter().map(|(k, _)| *k))?;
        Self::check_property_values("Node property", properties.iter().map(|(k, v)| (*k, v)))?;
        let node = self
            .nodes
            .get_mut(node_type)
            .and_then(|m| m.get_mut(node_id))
            .ok_or_else(|| GraphError::NodeNotFound(node_type.to_string(), node_id.to_string()))?;

        for (k, v) in properties {
            node.properties.insert(k.to_string(), v);
        }
        Ok(())
    }

    /// Iterate over all nodes
    pub fn nodes(&self) -> impl Iterator<Item = &Node> {
        self.nodes.values().flat_map(|m| m.values())
    }

    /// Iterate over nodes of a specific type
    pub fn nodes_of_type(&self, node_type: &str) -> impl Iterator<Item = &Node> {
        self.nodes
            .get(node_type)
            .into_iter()
            .flat_map(|m| m.values())
    }

    /// Count all nodes
    pub fn node_count(&self) -> usize {
        self.nodes.values().map(|m| m.len()).sum()
    }

    /// Count nodes of a specific type
    pub fn node_count_of_type(&self, node_type: &str) -> usize {
        self.nodes.get(node_type).map(|m| m.len()).unwrap_or(0)
    }

    /// Get all node types
    pub fn node_types(&self) -> Vec<&String> {
        self.nodes.keys().collect()
    }

    // =========================================================================
    // Edge Operations
    // =========================================================================

    /// Add an edge to the graph
    pub fn add_edge(
        &mut self,
        rel_type: &str,
        source: (&str, &str),
        target: (&str, &str),
        properties: Vec<(&str, Value)>,
    ) -> Result<()> {
        Self::check_identifier("Relationship type", rel_type)?;
        Self::check_property_names("Edge property", properties.iter().map(|(k, _)| *k))?;
        Self::check_property_values("Edge property", properties.iter().map(|(k, v)| (*k, v)))?;

        // Validate nodes exist
        if !self.has_node(source.0, source.1) {
            return Err(GraphError::NodeNotFound(
                source.0.to_string(),
                source.1.to_string(),
            ));
        }
        if !self.has_node(target.0, target.1) {
            return Err(GraphError::NodeNotFound(
                target.0.to_string(),
                target.1.to_string(),
            ));
        }

        let source_id = NodeId::new(source.0, source.1);
        let target_id = NodeId::new(target.0, target.1);
        let edge = Edge::new(rel_type, source_id.clone(), target_id.clone()).with_properties(properties.clone());
        let edge_key = edge.to_key();

        if self.edge_set.contains(&edge_key) {
            return Err(GraphError::DuplicateEdge(edge_key));
        }

        // Add to storage
        self.edges
            .entry(rel_type.to_string())
            .or_default()
            .push(edge.clone());

        let source_key = source_id.to_key();
        let target_key = target_id.to_key();

        self.out_edges
            .entry(source_key.clone())
            .or_default()
            .push(edge.clone());
        self.in_edges
            .entry(target_key.clone())
            .or_default()
            .push(edge.clone());

        self.edge_set.insert(edge_key);

        // For undirected graphs, add reverse edge
        if !self.directed {
            let reverse_edge = Edge::new(rel_type, target_id.clone(), source_id.clone())
                .with_properties(properties);
            let reverse_key = reverse_edge.to_key();

            if !self.edge_set.contains(&reverse_key) {
                self.edges
                    .entry(rel_type.to_string())
                    .or_default()
                    .push(reverse_edge.clone());
                self.out_edges
                    .entry(target_key)
                    .or_default()
                    .push(reverse_edge.clone());
                self.in_edges
                    .entry(source_key)
                    .or_default()
                    .push(reverse_edge);
                self.edge_set.insert(reverse_key);
            }
        }

        Ok(())
    }

    fn remove_edge_internal(&mut self, edge: &Edge) {
        let edge_key = edge.to_key();
        if !self.edge_set.contains(&edge_key) {
            return;
        }

        self.edge_set.remove(&edge_key);

        // Remove from edges
        if let Some(rel_edges) = self.edges.get_mut(&edge.rel_type) {
            rel_edges.retain(|e| e.to_key() != edge_key);
        }

        // Remove from out_edges
        let source_key = edge.source.to_key();
        if let Some(out) = self.out_edges.get_mut(&source_key) {
            out.retain(|e| e.to_key() != edge_key);
        }

        // Remove from in_edges
        let target_key = edge.target.to_key();
        if let Some(in_e) = self.in_edges.get_mut(&target_key) {
            in_e.retain(|e| e.to_key() != edge_key);
        }
    }

    /// Remove an edge
    pub fn remove_edge(
        &mut self,
        rel_type: &str,
        source: (&str, &str),
        target: (&str, &str),
    ) -> Result<()> {
        let source_id = NodeId::new(source.0, source.1);
        let target_id = NodeId::new(target.0, target.1);
        let edge = Edge::new(rel_type, source_id, target_id);
        let edge_key = edge.to_key();

        if !self.edge_set.contains(&edge_key) {
            return Err(GraphError::EdgeNotFound(
                format!("{}:{}", source.0, source.1),
                format!("{}:{}", target.0, target.1),
            ));
        }

        self.remove_edge_internal(&edge);
        Ok(())
    }

    /// Check if edge exists
    pub fn has_edge(&self, rel_type: &str, source: (&str, &str), target: (&str, &str)) -> bool {
        let source_id = NodeId::new(source.0, source.1);
        let target_id = NodeId::new(target.0, target.1);
        let edge = Edge::new(rel_type, source_id, target_id);
        self.edge_set.contains(&edge.to_key())
    }

    /// Count all edges
    pub fn edge_count(&self) -> usize {
        self.edge_set.len()
    }

    /// Count edges of a specific type
    pub fn edge_count_of_type(&self, rel_type: &str) -> usize {
        self.edges.get(rel_type).map(|v| v.len()).unwrap_or(0)
    }

    /// Get all edge types
    pub fn edge_types(&self) -> Vec<&String> {
        self.edges.keys().collect()
    }

    /// Get all edges of a specific type
    pub fn edges_of_type(&self, rel_type: &str) -> impl Iterator<Item = &Edge> {
        self.edges.get(rel_type).into_iter().flatten()
    }

    /// Check if the graph has a cycle for edges of a specific type
    pub fn has_cycle(&self, rel_type: Option<&str>) -> bool {
        // Iterative DFS-based cycle detection
        let mut visited = HashSet::new();

        for node_types in self.nodes.values() {
            for node in node_types.values() {
                let node_key = format!("{}:{}", node.node_type, node.id);
                if !visited.contains(&node_key)
                    && self.has_cycle_from(&node_key, &mut visited, rel_type)
                {
                    return true;
                }
            }
        }
        false
    }

    /// Outgoing `(target_key, rel_type)` pairs for cycle detection.
    ///
    /// For undirected graphs, the single auto-reverse of the edge used to
    /// enter this node (`skip`) is excluded, so a lone undirected edge does
    /// not read as a cycle. Only one matching reverse edge is skipped:
    /// a genuine parallel edge back to the parent still counts.
    fn cycle_successors(
        &self,
        node_key: &str,
        rel_type: Option<&str>,
        skip: Option<(&str, &str)>,
    ) -> Vec<(String, String)> {
        let mut result = Vec::new();
        let mut skip_remaining = if self.directed { None } else { skip };

        if let Some(edges) = self.out_edges.get(node_key) {
            for edge in edges {
                if rel_type.is_some() && rel_type != Some(edge.rel_type.as_str()) {
                    continue;
                }
                let target_key = edge.target.to_key();
                if let Some((skip_rel, skip_key)) = skip_remaining {
                    if edge.rel_type == skip_rel && target_key == skip_key {
                        skip_remaining = None;
                        continue;
                    }
                }
                result.push((target_key, edge.rel_type.clone()));
            }
        }
        result
    }

    fn has_cycle_from(
        &self,
        start_key: &str,
        visited: &mut HashSet<String>,
        rel_type: Option<&str>,
    ) -> bool {
        struct Frame {
            key: String,
            successors: Vec<(String, String)>,
            idx: usize,
        }

        let mut rec_stack: HashSet<String> = HashSet::new();
        visited.insert(start_key.to_string());
        rec_stack.insert(start_key.to_string());

        let mut stack = vec![Frame {
            key: start_key.to_string(),
            successors: self.cycle_successors(start_key, rel_type, None),
            idx: 0,
        }];

        while !stack.is_empty() {
            let next = {
                let frame = stack.last_mut().unwrap();
                if frame.idx < frame.successors.len() {
                    let (target_key, edge_rel) = frame.successors[frame.idx].clone();
                    frame.idx += 1;
                    Some((target_key, edge_rel, frame.key.clone()))
                } else {
                    None
                }
            };

            match next {
                Some((target_key, edge_rel, from_key)) => {
                    if !visited.contains(&target_key) {
                        visited.insert(target_key.clone());
                        rec_stack.insert(target_key.clone());
                        let successors =
                            self.cycle_successors(&target_key, rel_type, Some((&edge_rel, &from_key)));
                        stack.push(Frame {
                            key: target_key,
                            successors,
                            idx: 0,
                        });
                    } else if rec_stack.contains(&target_key) {
                        return true;
                    }
                }
                None => {
                    let frame = stack.pop().unwrap();
                    rec_stack.remove(&frame.key);
                }
            }
        }

        false
    }

    // =========================================================================
    // Traversal Operations
    // =========================================================================

    /// Get neighboring nodes.
    ///
    /// Results are deduplicated while preserving first-occurrence order:
    /// a node connected through multiple edges (e.g. the forward and
    /// auto-reverse edges of an undirected graph with `Direction::Both`)
    /// appears only once.
    pub fn neighbors(
        &self,
        node_ref: &NodeRef,
        rel_type: Option<&str>,
        direction: Direction,
    ) -> Vec<NodeId> {
        let mut neighbors = Vec::new();
        let mut seen: HashSet<String> = HashSet::new();
        let node_key = format!("{}:{}", node_ref.0, node_ref.1);

        if direction == Direction::Out || direction == Direction::Both {
            if let Some(edges) = self.out_edges.get(&node_key) {
                for edge in edges {
                    if (rel_type.is_none() || rel_type == Some(&edge.rel_type))
                        && seen.insert(edge.target.to_key())
                    {
                        neighbors.push(edge.target.clone());
                    }
                }
            }
        }

        if direction == Direction::In || direction == Direction::Both {
            if let Some(edges) = self.in_edges.get(&node_key) {
                for edge in edges {
                    if (rel_type.is_none() || rel_type == Some(&edge.rel_type))
                        && seen.insert(edge.source.to_key())
                    {
                        neighbors.push(edge.source.clone());
                    }
                }
            }
        }

        neighbors
    }

    /// Get nodes N hops away
    pub fn multi_hop(
        &self,
        start: &NodeRef,
        rel_type: Option<&str>,
        hops: usize,
        direction: Direction,
    ) -> Vec<NodeId> {
        if hops == 0 {
            return vec![NodeId::new(start.0, start.1)];
        }

        let start_key = format!("{}:{}", start.0, start.1);
        let mut current: HashSet<String> = HashSet::new();
        current.insert(start_key.clone());

        let mut visited: HashSet<String> = HashSet::new();
        visited.insert(start_key);

        let mut ref_map: HashMap<String, NodeId> = HashMap::new();
        ref_map.insert(
            format!("{}:{}", start.0, start.1),
            NodeId::new(start.0, start.1),
        );

        for _ in 0..hops {
            let mut next_level: HashSet<String> = HashSet::new();

            for key in &current {
                if let Some(node_id) = ref_map.get(key) {
                    let neighbors = self.neighbors(&node_id.as_ref(), rel_type, direction);
                    for neighbor in neighbors {
                        let neighbor_key = neighbor.to_key();
                        if !visited.contains(&neighbor_key) {
                            next_level.insert(neighbor_key.clone());
                            ref_map.insert(neighbor_key.clone(), neighbor);
                        }
                    }
                }
            }

            for key in &next_level {
                visited.insert(key.clone());
            }
            current = next_level;
        }

        current
            .iter()
            .filter_map(|key| ref_map.get(key).cloned())
            .collect()
    }

    /// Get nodes within a range of hops
    pub fn multi_hop_range(
        &self,
        start: &NodeRef,
        rel_type: Option<&str>,
        min_hops: usize,
        max_hops: usize,
        direction: Direction,
    ) -> Vec<NodeId> {
        let start_key = format!("{}:{}", start.0, start.1);
        let mut result: HashSet<String> = HashSet::new();
        let mut current: HashSet<String> = HashSet::new();
        current.insert(start_key.clone());

        let mut visited: HashSet<String> = HashSet::new();
        visited.insert(start_key);

        let mut ref_map: HashMap<String, NodeId> = HashMap::new();
        ref_map.insert(
            format!("{}:{}", start.0, start.1),
            NodeId::new(start.0, start.1),
        );

        for hop in 1..=max_hops {
            let mut next_level: HashSet<String> = HashSet::new();

            for key in &current {
                if let Some(node_id) = ref_map.get(key) {
                    let neighbors = self.neighbors(&node_id.as_ref(), rel_type, direction);
                    for neighbor in neighbors {
                        let neighbor_key = neighbor.to_key();
                        if !visited.contains(&neighbor_key) {
                            next_level.insert(neighbor_key.clone());
                            ref_map.insert(neighbor_key.clone(), neighbor);
                            if hop >= min_hops {
                                result.insert(neighbor_key);
                            }
                        }
                    }
                }
            }

            for key in &next_level {
                visited.insert(key.clone());
            }
            current = next_level;

            if current.is_empty() {
                break;
            }
        }

        result
            .iter()
            .filter_map(|key| ref_map.get(key).cloned())
            .collect()
    }

    // =========================================================================
    // Path Finding
    // =========================================================================

    /// Find shortest path between two nodes using BFS
    pub fn shortest_path(
        &self,
        start: &NodeRef,
        end: &NodeRef,
        rel_type: Option<&str>,
        max_hops: usize,
        direction: Direction,
    ) -> Option<Path> {
        let start_id = NodeId::new(start.0, start.1);
        let end_id = NodeId::new(end.0, end.1);

        if start_id == end_id {
            return Some(Path::new(vec![start_id], vec![]));
        }

        let end_key = end_id.to_key();
        let mut visited: HashSet<String> = HashSet::new();
        visited.insert(start_id.to_key());

        let mut queue: VecDeque<(NodeId, Vec<NodeId>, Vec<Edge>)> = VecDeque::new();
        queue.push_back((start_id.clone(), vec![start_id], vec![]));

        while let Some((current, path_nodes, path_edges)) = queue.pop_front() {
            if path_nodes.len() > max_hops.saturating_add(1) {
                continue;
            }

            let current_key = current.to_key();

            if direction != Direction::In {
                if let Some(edges) = self.out_edges.get(&current_key) {
                    for edge in edges {
                        if rel_type.is_some() && rel_type != Some(&edge.rel_type) {
                            continue;
                        }

                        let target_key = edge.target.to_key();
                        if target_key == end_key {
                            let mut nodes = path_nodes.clone();
                            nodes.push(edge.target.clone());
                            let mut edges = path_edges.clone();
                            edges.push(edge.clone());
                            return Some(Path::new(nodes, edges));
                        }

                        if !visited.contains(&target_key) {
                            visited.insert(target_key);
                            let mut nodes = path_nodes.clone();
                            nodes.push(edge.target.clone());
                            let mut edges = path_edges.clone();
                            edges.push(edge.clone());
                            queue.push_back((edge.target.clone(), nodes, edges));
                        }
                    }
                }
            }

            if direction == Direction::In || direction == Direction::Both {
                if let Some(edges) = self.in_edges.get(&current_key) {
                    for edge in edges {
                        if rel_type.is_some() && rel_type != Some(&edge.rel_type) {
                            continue;
                        }

                        let source_key = edge.source.to_key();
                        if source_key == end_key {
                            let mut nodes = path_nodes.clone();
                            nodes.push(edge.source.clone());
                            let mut edges = path_edges.clone();
                            edges.push(edge.clone());
                            return Some(Path::new(nodes, edges));
                        }

                        if !visited.contains(&source_key) {
                            visited.insert(source_key);
                            let mut nodes = path_nodes.clone();
                            nodes.push(edge.source.clone());
                            let mut edges = path_edges.clone();
                            edges.push(edge.clone());
                            queue.push_back((edge.source.clone(), nodes, edges));
                        }
                    }
                }
            }
        }

        None
    }

    /// Check if a path exists between two nodes in the given direction
    pub fn path_exists(
        &self,
        start: &NodeRef,
        end: &NodeRef,
        rel_type: Option<&str>,
        max_hops: usize,
        direction: Direction,
    ) -> bool {
        self.shortest_path(start, end, rel_type, max_hops, direction)
            .is_some()
    }

    // =========================================================================
    // Graph Analysis
    // =========================================================================

    /// Count incoming edges for a node
    pub fn in_degree(&self, node_ref: &NodeRef) -> usize {
        let key = format!("{}:{}", node_ref.0, node_ref.1);
        self.in_edges.get(&key).map(|v| v.len()).unwrap_or(0)
    }

    /// Count outgoing edges for a node
    pub fn out_degree(&self, node_ref: &NodeRef) -> usize {
        let key = format!("{}:{}", node_ref.0, node_ref.1);
        self.out_edges.get(&key).map(|v| v.len()).unwrap_or(0)
    }

    /// Total degree for a node
    pub fn degree(&self, node_ref: &NodeRef) -> usize {
        self.in_degree(node_ref) + self.out_degree(node_ref)
    }

    /// Check if graph is connected
    pub fn is_connected(&self) -> bool {
        let all_nodes: Vec<_> = self.nodes().collect();
        if all_nodes.is_empty() {
            return true;
        }

        let start = &all_nodes[0];
        let start_key = start.to_key();
        let mut visited: HashSet<String> = HashSet::new();
        visited.insert(start_key.clone());

        let mut queue: VecDeque<String> = VecDeque::new();
        queue.push_back(start_key);

        while let Some(current_key) = queue.pop_front() {
            // Get node from key
            let parts: Vec<&str> = current_key.split(':').collect();
            if parts.len() == 2 {
                let neighbors = self.neighbors(&(parts[0], parts[1]), None, Direction::Both);
                for neighbor in neighbors {
                    let neighbor_key = neighbor.to_key();
                    if !visited.contains(&neighbor_key) {
                        visited.insert(neighbor_key.clone());
                        queue.push_back(neighbor_key);
                    }
                }
            }
        }

        visited.len() == all_nodes.len()
    }

    // =========================================================================
    // Serialization
    // =========================================================================

    /// Serialize graph to ISON format.
    ///
    /// Serialization is canonical: node type blocks, node rows within a
    /// block, edge type blocks, and edge rows within a block are all sorted
    /// (ordinal string comparison), independent of insertion order. The
    /// same logical graph always serializes to the same bytes, regardless
    /// of construction history.
    /// Build the ISON Document this graph serializes to.
    ///
    /// ISONGraph decides *what* to emit - which blocks, which rows, which
    /// columns - and hands every byte-level decision to ison-rs's canonical
    /// writer: quoting, escaping, delimiters, column layout, and the final
    /// block and row order.
    ///
    /// This used to be hand-rolled here, which meant ISON syntax had two
    /// implementations per language that had to agree forever. They did not:
    /// the id column was written unquoted, so a node id containing a space
    /// produced output that would not reparse.
    fn document(&self) -> IsonDocument {
        let mut doc = IsonDocument::new();

        // Directedness is graph semantics: an undirected graph that reloads
        // as directed silently changes traversal, degree and cycle results,
        // and its two mirror rows become two distinct edges. Written only
        // when it departs from the default so every directed graph keeps
        // its bytes.
        if !self.directed {
            let mut block = IsonBlock::new("meta", "graph");
            block.fields = vec!["directed".to_string()];
            let mut row: HashMap<String, IsonValue> = HashMap::new();
            row.insert("directed".to_string(), IsonValue::Bool(false));
            block.rows.push(row);
            doc.blocks.push(block);
        }

        let mut node_types: Vec<_> = self.nodes.keys().collect();
        node_types.sort();
        for node_type in node_types {
            let type_nodes = &self.nodes[node_type];
            if type_nodes.is_empty() {
                continue;
            }

            let mut prop_keys: Vec<&String> = type_nodes
                .values()
                .flat_map(|n| n.properties.keys())
                .collect::<HashSet<_>>()
                .into_iter()
                .collect();
            prop_keys.sort();

            let mut block = IsonBlock::new("nodes", node_type.as_str());
            block.fields = std::iter::once("id".to_string())
                .chain(prop_keys.iter().map(|k| (*k).clone()))
                .collect();

            for node in type_nodes.values() {
                let mut row: HashMap<String, IsonValue> = HashMap::new();
                row.insert("id".to_string(), IsonValue::String(node.id.clone()));
                for key in &prop_keys {
                    let value = match node.properties.get(*key) {
                        Some(v) => v.clone(),
                        None => IsonValue::Null,
                    };
                    row.insert((*key).clone(), value);
                }
                block.rows.push(row);
            }
            doc.blocks.push(block);
        }

        let mut edge_types: Vec<_> = self.edges.keys().collect();
        edge_types.sort();
        for rel_type in edge_types {
            let edges = &self.edges[rel_type];
            if edges.is_empty() {
                continue;
            }

            let mut prop_keys: Vec<&String> = edges
                .iter()
                .flat_map(|e| e.properties.keys())
                .collect::<HashSet<_>>()
                .into_iter()
                .collect();
            prop_keys.sort();

            let mut block = IsonBlock::new("edges", rel_type.as_str());
            block.fields = ["source".to_string(), "target".to_string()]
                .into_iter()
                .chain(prop_keys.iter().map(|k| (*k).clone()))
                .collect();

            for edge in edges {
                let mut row: HashMap<String, IsonValue> = HashMap::new();
                row.insert(
                    "source".to_string(),
                    IsonValue::Reference(IsonReference::with_type(
                        edge.source.id.clone(),
                        edge.source.node_type.clone(),
                    )),
                );
                row.insert(
                    "target".to_string(),
                    IsonValue::Reference(IsonReference::with_type(
                        edge.target.id.clone(),
                        edge.target.node_type.clone(),
                    )),
                );
                for key in &prop_keys {
                    let value = match edge.properties.get(*key) {
                        Some(v) => v.clone(),
                        None => IsonValue::Null,
                    };
                    row.insert((*key).clone(), value);
                }
                block.rows.push(row);
            }
            doc.blocks.push(block);
        }

        doc
    }

    /// Serialize graph to canonical ISON (ISONCS).
    ///
    /// Output is canonical: the same logical graph always serializes to the
    /// same bytes regardless of construction order. Block, row and column
    /// order are the canonical writer's, not ISONGraph's.
    pub fn to_ison(&self) -> String {
        dumps_canonical(&self.document()).unwrap_or_default()
    }

    /// Serialize graph to canonical ISONL. Row order matches [`to_ison`].
    pub fn to_isonl(&self) -> String {
        dumps_canonical_isonl(&self.document()).unwrap_or_default()
    }


    /// Node ids are identifiers rather than values, so they are always text.
    /// Property values keep whatever type the parser produced.
    fn id_text(value: &IsonValue) -> Option<String> {
        match value {
            IsonValue::Null => None,
            IsonValue::Bool(b) => Some(b.to_string()),
            IsonValue::Int(i) => Some(i.to_string()),
            IsonValue::Float(f) => Some(f.to_string()),
            IsonValue::String(s) => Some(s.clone()),
            IsonValue::Reference(r) => Some(r.to_ison()),
        }
    }

    fn build_from_document(doc: &IsonDocument, name: &str) -> Result<Self> {
        // `meta.graph` carries graph semantics: `directed` (written by every
        // port for an undirected graph) and `format` (written by the Python
        // MultiGraph, which this port cannot represent).
        let mut directed = true;
        for block in &doc.blocks {
            match block.kind.as_str() {
                "nodes" | "edges" => {}
                "meta" if block.name == "graph" => {
                    for row in &block.rows {
                        if let Some(IsonValue::String(format)) = row.get("format") {
                            if format == MULTIGRAPH_FORMAT {
                                return Err(GraphError::ParseError(format!(
                                    "This is a {} document, which may contain multiple \
                                     edges per (rel_type, source, target) triple; this \
                                     port has no MultiGraph and would silently drop them.",
                                    MULTIGRAPH_FORMAT
                                )));
                            }
                        }
                        if let Some(IsonValue::Bool(b)) = row.get("directed") {
                            directed = *b;
                        }
                    }
                }
                other => {
                    return Err(GraphError::ParseError(format!(
                        "unknown block kind {:?} (expected 'nodes', 'edges' or 'meta')",
                        other
                    )))
                }
            }
        }

        let mut graph = ISONGraph::new(name);
        graph.directed = directed;

        // Nodes first regardless of block order - the canonical writer sorts
        // blocks by name, which puts 'edges' ahead of 'nodes'.
        for block in &doc.blocks {
            if block.kind != "nodes" {
                continue;
            }
            for row in &block.rows {
                // The id is an identifier, not a property, so it stays text;
                // everything else keeps the type the parser gave it.
                let id = row
                    .get("id")
                    .and_then(Self::id_text)
                    .ok_or_else(|| GraphError::ParseError("row has no id".into()))?;
                let props: Vec<(String, Value)> = row
                    .iter()
                    .filter(|(k, _)| k.as_str() != "id")
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect();
                let borrowed: Vec<(&str, Value)> =
                    props.iter().map(|(k, v)| (k.as_str(), v.clone())).collect();
                graph.add_node(&block.name, &id, borrowed)?;
            }
        }

        for block in &doc.blocks {
            if block.kind != "edges" {
                continue;
            }
            for row in &block.rows {
                let (source, target) = match (row.get("source"), row.get("target")) {
                    (Some(IsonValue::Reference(s)), Some(IsonValue::Reference(t))) => (
                        (s.ref_type.clone().unwrap_or_default(), s.id.clone()),
                        (t.ref_type.clone().unwrap_or_default(), t.id.clone()),
                    ),
                    _ => {
                        return Err(GraphError::ParseError(
                            "edge row is missing a source or target reference".into(),
                        ))
                    }
                };
                let props: Vec<(String, Value)> = row
                    .iter()
                    .filter(|(k, _)| k.as_str() != "source" && k.as_str() != "target")
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect();
                let borrowed: Vec<(&str, Value)> =
                    props.iter().map(|(k, v)| (k.as_str(), v.clone())).collect();
                // An undirected graph serializes both an edge and the mirror
                // it auto-created; re-adding the mirror would collide, so
                // skip what is already reconstructed. A directed graph still
                // fails on a genuine duplicate.
                if !graph.directed
                    && graph.has_edge(&block.name, (&source.0, &source.1), (&target.0, &target.1))
                {
                    continue;
                }
                graph.add_edge(
                    &block.name,
                    (&source.0, &source.1),
                    (&target.0, &target.1),
                    borrowed,
                )?;
            }
        }

        Ok(graph)
    }

    /// Parse a graph from ISON.
    pub fn from_ison(text: &str, name: Option<&str>) -> Result<Self> {
        let doc = loads(text)
            .map_err(|e| GraphError::ParseError(format!("{}", e)))?;
        Self::build_from_document(&doc, name.unwrap_or("graph"))
    }

    /// Parse a graph from ISONL.
    pub fn from_isonl(text: &str, name: Option<&str>) -> Result<Self> {
        let doc = loads_isonl(text)
            .map_err(|e| GraphError::ParseError(format!("{}", e)))?;
        Self::build_from_document(&doc, name.unwrap_or("graph"))
    }

    pub fn save<P: AsRef<StdPath>>(&self, path: P, format: Option<&str>) -> Result<()> {
        let path = path.as_ref();
        let actual_format = format.unwrap_or_else(|| {
            if path.extension().map(|e| e == "isonl").unwrap_or(false) {
                "isonl"
            } else {
                "ison"
            }
        });

        let content = if actual_format == "isonl" {
            self.to_isonl()
        } else {
            self.to_ison()
        };

        fs::write(path, content).map_err(GraphError::Io)
    }

    /// Load graph from file
    pub fn load<P: AsRef<StdPath>>(path: P, format: Option<&str>) -> Result<Self> {
        let path = path.as_ref();
        let content = fs::read_to_string(path).map_err(GraphError::Io)?;

        let actual_format = format.unwrap_or_else(|| {
            if path.extension().map(|e| e == "isonl").unwrap_or(false) {
                "isonl"
            } else {
                "ison"
            }
        });

        let graph_name = path.file_stem().and_then(|s| s.to_str()).unwrap_or("graph");

        if actual_format == "isonl" {
            Self::from_isonl(&content, Some(graph_name))
        } else {
            Self::from_ison(&content, Some(graph_name))
        }
    }

    // =========================================================================
    // Additional Edge Operations
    // =========================================================================

    /// Get an edge by its components
    pub fn get_edge(&self, rel_type: &str, source: (&str, &str), target: (&str, &str)) -> Result<&Edge> {
        let source_id = NodeId::new(source.0, source.1);
        let target_id = NodeId::new(target.0, target.1);

        if let Some(edges) = self.edges.get(rel_type) {
            for edge in edges {
                if edge.source == source_id && edge.target == target_id {
                    return Ok(edge);
                }
            }
        }

        Err(GraphError::EdgeNotFound(
            format!("{}:{}", source.0, source.1),
            format!("{}:{}", target.0, target.1),
        ))
    }

    // =========================================================================
    // Additional Graph Analysis
    // =========================================================================

    /// Get all connected components
    pub fn connected_components(&self) -> Vec<HashSet<NodeId>> {
        let mut visited: HashSet<String> = HashSet::new();
        let mut components: Vec<HashSet<NodeId>> = Vec::new();

        for node in self.nodes() {
            let node_key = node.to_key();
            if !visited.contains(&node_key) {
                let mut component: HashSet<NodeId> = HashSet::new();
                let mut queue: VecDeque<NodeId> = VecDeque::new();
                queue.push_back(node.node_id());

                while let Some(current) = queue.pop_front() {
                    let current_key = current.to_key();
                    if visited.contains(&current_key) {
                        continue;
                    }
                    visited.insert(current_key);
                    component.insert(current.clone());

                    // Get neighbors in both directions
                    let neighbors = self.neighbors(&current.as_ref(), None, Direction::Both);
                    for neighbor in neighbors {
                        if !visited.contains(&neighbor.to_key()) {
                            queue.push_back(neighbor);
                        }
                    }
                }

                components.push(component);
            }
        }

        components
    }

    /// Edges to follow from a node for path enumeration
    fn edges_to_follow(
        &self,
        node: &NodeId,
        rel_type: Option<&str>,
        direction: Direction,
    ) -> Vec<(Edge, NodeId)> {
        let node_key = node.to_key();
        let mut edges_to_follow: Vec<(Edge, NodeId)> = Vec::new();

        if direction == Direction::Out || direction == Direction::Both {
            if let Some(out_edges) = self.out_edges.get(&node_key) {
                for edge in out_edges {
                    if rel_type.is_none() || rel_type == Some(&edge.rel_type) {
                        edges_to_follow.push((edge.clone(), edge.target.clone()));
                    }
                }
            }
        }

        if direction == Direction::In || direction == Direction::Both {
            if let Some(in_edges) = self.in_edges.get(&node_key) {
                for edge in in_edges {
                    if rel_type.is_none() || rel_type == Some(&edge.rel_type) {
                        edges_to_follow.push((edge.clone(), edge.source.clone()));
                    }
                }
            }
        }

        edges_to_follow
    }

    /// Find all paths between two nodes using iterative DFS
    pub fn all_paths(
        &self,
        start: &NodeRef,
        end: &NodeRef,
        rel_type: Option<&str>,
        max_hops: usize,
        direction: Direction,
    ) -> Vec<Path> {
        let start_id = NodeId::new(start.0, start.1);
        let end_id = NodeId::new(end.0, end.1);
        let mut paths: Vec<Path> = Vec::new();

        let mut visited: HashSet<String> = HashSet::new();
        visited.insert(start_id.to_key());
        let mut path_nodes = vec![start_id.clone()];
        let mut path_edges: Vec<Edge> = Vec::new();

        if start_id == end_id {
            paths.push(Path::new(path_nodes, path_edges));
            return paths;
        }

        struct Frame {
            options: Vec<(Edge, NodeId)>,
            idx: usize,
        }

        let mut stack = vec![Frame {
            options: self.edges_to_follow(&start_id, rel_type, direction),
            idx: 0,
        }];

        while !stack.is_empty() {
            let next = {
                let frame = stack.last_mut().unwrap();
                if frame.idx < frame.options.len() {
                    let option = frame.options[frame.idx].clone();
                    frame.idx += 1;
                    Some(option)
                } else {
                    None
                }
            };

            match next {
                Some((edge, next_node)) => {
                    let next_key = next_node.to_key();
                    if visited.contains(&next_key) {
                        continue;
                    }

                    visited.insert(next_key.clone());
                    path_nodes.push(next_node.clone());
                    path_edges.push(edge);

                    if path_nodes.len() > max_hops.saturating_add(1) {
                        // Too deep: backtrack immediately
                        path_nodes.pop();
                        path_edges.pop();
                        visited.remove(&next_key);
                        continue;
                    }

                    if next_node == end_id {
                        paths.push(Path::new(path_nodes.clone(), path_edges.clone()));
                        path_nodes.pop();
                        path_edges.pop();
                        visited.remove(&next_key);
                        continue;
                    }

                    stack.push(Frame {
                        options: self.edges_to_follow(&next_node, rel_type, direction),
                        idx: 0,
                    });
                }
                None => {
                    stack.pop();
                    // Backtrack the path entry owned by the popped frame
                    // (the root frame owns the start node, which stays).
                    if !stack.is_empty() {
                        if let Some(popped) = path_nodes.pop() {
                            path_edges.pop();
                            visited.remove(&popped.to_key());
                        }
                    }
                }
            }
        }

        paths
    }

    // =========================================================================
    // Pattern-based Traversal
    // =========================================================================

    /// Traverse graph following a pattern of relationship types
    pub fn traverse(
        &self,
        start: &NodeRef,
        pattern: &[(String, Direction)],
        filter_fn: Option<&dyn Fn(&Node) -> bool>,
    ) -> Vec<NodeId> {
        let start_id = NodeId::new(start.0, start.1);
        let mut current: HashSet<String> = HashSet::new();
        current.insert(start_id.to_key());

        let mut ref_map: HashMap<String, NodeId> = HashMap::new();
        ref_map.insert(start_id.to_key(), start_id);

        for (rel_type, direction) in pattern {
            let mut next_level: HashSet<String> = HashSet::new();

            for key in &current {
                if let Some(node_id) = ref_map.get(key) {
                    let neighbors = self.neighbors(&node_id.as_ref(), Some(rel_type), *direction);
                    for neighbor in neighbors {
                        let neighbor_key = neighbor.to_key();

                        let should_include = match filter_fn {
                            None => true,
                            Some(f) => {
                                if let Ok(node) = self.get_node_by_id(&neighbor) {
                                    f(node)
                                } else {
                                    false
                                }
                            }
                        };

                        if should_include {
                            next_level.insert(neighbor_key.clone());
                            ref_map.insert(neighbor_key, neighbor);
                        }
                    }
                }
            }

            current = next_level;
            if current.is_empty() {
                break;
            }
        }

        current
            .iter()
            .filter_map(|key| ref_map.get(key).cloned())
            .collect()
    }

    // =========================================================================
    // Query Pattern Syntax
    // =========================================================================

    /// Execute a simple pattern query
    ///
    /// Pattern syntax:
    ///   :type:id -[:REL]-> *           (single hop)
    ///   :type:id -[:REL*N]-> *         (N hops)
    ///   :type:id -[:REL*1..3]-> *      (1-3 hops range)
    pub fn query(&self, pattern: &str) -> Result<Vec<NodeId>> {
        static HOP_REGEX: OnceLock<Regex> = OnceLock::new();
        static RANGE_REGEX: OnceLock<Regex> = OnceLock::new();
        static SIMPLE_REGEX: OnceLock<Regex> = OnceLock::new();

        let p = pattern.trim();

        // Match: :type:id -[:REL*N]-> *
        let hop_regex = HOP_REGEX.get_or_init(|| {
            Regex::new(r"^:(\w+):(\w+)\s*-\[:(\w+)\*(\d+)\]->\s*\*$").unwrap()
        });
        if let Some(caps) = hop_regex.captures(p) {
            let node_type = caps.get(1).unwrap().as_str();
            let node_id = caps.get(2).unwrap().as_str();
            let rel_type = caps.get(3).unwrap().as_str();
            let hops: usize = caps.get(4).unwrap().as_str().parse().unwrap_or(1);
            return Ok(self.multi_hop(&(node_type, node_id), Some(rel_type), hops, Direction::Out));
        }

        // Match: :type:id -[:REL*N..M]-> *
        let range_regex = RANGE_REGEX.get_or_init(|| {
            Regex::new(r"^:(\w+):(\w+)\s*-\[:(\w+)\*(\d+)\.\.(\d+)\]->\s*\*$").unwrap()
        });
        if let Some(caps) = range_regex.captures(p) {
            let node_type = caps.get(1).unwrap().as_str();
            let node_id = caps.get(2).unwrap().as_str();
            let rel_type = caps.get(3).unwrap().as_str();
            let min_hops: usize = caps.get(4).unwrap().as_str().parse().unwrap_or(1);
            let max_hops: usize = caps.get(5).unwrap().as_str().parse().unwrap_or(3);
            return Ok(self.multi_hop_range(
                &(node_type, node_id),
                Some(rel_type),
                min_hops,
                max_hops,
                Direction::Out,
            ));
        }

        // Match: :type:id -[:REL]-> *
        let simple_regex = SIMPLE_REGEX
            .get_or_init(|| Regex::new(r"^:(\w+):(\w+)\s*-\[:(\w+)\]->\s*\*$").unwrap());
        if let Some(caps) = simple_regex.captures(p) {
            let node_type = caps.get(1).unwrap().as_str();
            let node_id = caps.get(2).unwrap().as_str();
            let rel_type = caps.get(3).unwrap().as_str();
            return Ok(self.neighbors(&(node_type, node_id), Some(rel_type), Direction::Out));
        }

        Err(GraphError::ParseError(format!("Invalid query pattern: {}", pattern)))
    }

    // =========================================================================
    // Fluent API Entry Point
    // =========================================================================

    /// Start a fluent traversal from a node
    pub fn start(&self, node_ref: &NodeRef) -> GraphTraversal<'_> {
        GraphTraversal::new(self, NodeId::new(node_ref.0, node_ref.1))
    }
}

// =============================================================================
// Fluent Traversal API
// =============================================================================

/// Fluent API for graph traversal
pub struct GraphTraversal<'a> {
    graph: &'a ISONGraph,
    current: HashSet<String>,
    visited: HashSet<String>,
    ref_map: HashMap<String, NodeId>,
}

impl<'a> GraphTraversal<'a> {
    pub fn new(graph: &'a ISONGraph, start: NodeId) -> Self {
        let start_key = start.to_key();
        let mut current = HashSet::new();
        current.insert(start_key.clone());

        let mut visited = HashSet::new();
        visited.insert(start_key.clone());

        let mut ref_map = HashMap::new();
        ref_map.insert(start_key, start);

        Self {
            graph,
            current,
            visited,
            ref_map,
        }
    }

    /// Traverse one hop following edges
    pub fn hop(mut self, rel_type: Option<&str>, direction: Direction) -> Self {
        let mut next_level: HashSet<String> = HashSet::new();

        for key in &self.current {
            if let Some(node_id) = self.ref_map.get(key) {
                let neighbors = self.graph.neighbors(&node_id.as_ref(), rel_type, direction);
                for neighbor in neighbors {
                    let neighbor_key = neighbor.to_key();
                    if !self.visited.contains(&neighbor_key) {
                        next_level.insert(neighbor_key.clone());
                        self.ref_map.insert(neighbor_key, neighbor);
                    }
                }
            }
        }

        for key in &next_level {
            self.visited.insert(key.clone());
        }
        self.current = next_level;
        self
    }

    /// Traverse N hops
    pub fn hops(mut self, n: usize, rel_type: Option<&str>, direction: Direction) -> Self {
        for _ in 0..n {
            self = self.hop(rel_type, direction);
        }
        self
    }

    /// Filter current nodes
    pub fn filter<F>(mut self, f: F) -> Self
    where
        F: Fn(&Node) -> bool,
    {
        let filtered: HashSet<String> = self
            .current
            .iter()
            .filter(|key| {
                if let Some(node_id) = self.ref_map.get(*key) {
                    if let Ok(node) = self.graph.get_node_by_id(node_id) {
                        return f(node);
                    }
                }
                false
            })
            .cloned()
            .collect();
        self.current = filtered;
        self
    }

    /// Return current nodes as NodeId vector
    pub fn collect(self) -> Vec<NodeId> {
        self.current
            .iter()
            .filter_map(|key| self.ref_map.get(key).cloned())
            .collect()
    }

    /// Return current nodes as Node objects
    pub fn collect_nodes(self) -> Vec<Node> {
        self.current
            .iter()
            .filter_map(|key| {
                self.ref_map
                    .get(key)
                    .and_then(|id| self.graph.get_node_by_id(id).ok().cloned())
            })
            .collect()
    }

    /// Count current nodes
    pub fn count(&self) -> usize {
        self.current.len()
    }

    /// Get first node or None
    pub fn first(&self) -> Option<NodeId> {
        self.current
            .iter()
            .next()
            .and_then(|key| self.ref_map.get(key).cloned())
    }
}

// =============================================================================
// Tests
// =============================================================================

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

    #[test]
    fn test_add_and_get_node() {
        let mut graph = ISONGraph::new("test");
        graph
            .add_node("person", "1", props![("name", "Alice"), ("age", "30")])
            .unwrap();

        let node = graph.get_node("person", "1").unwrap();
        assert_eq!(node.node_type, "person");
        assert_eq!(node.id, "1");
        assert_eq!(crate::value_to_text(node.properties.get("name").unwrap()), "Alice");
    }

    #[test]
    fn test_duplicate_node() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        assert!(graph.add_node("person", "1", props![]).is_err());
    }

    #[test]
    fn test_add_node_rejects_colon() {
        let mut graph = ISONGraph::new("test");
        assert!(matches!(
            graph.add_node("per:son", "1", props![]),
            Err(GraphError::InvalidIdentifier(_))
        ));
        assert!(matches!(
            graph.add_node("person", "1:2", props![]),
            Err(GraphError::InvalidIdentifier(_))
        ));
        assert_eq!(graph.node_count(), 0);
    }

    // ':' and '|' are structurally significant to the wire format ('type:id'
    // references, ISONL columns); allowing them in a node type, node id, or
    // relationship type builds a graph that fails on to_ison()/to_isonl()
    // round-trip instead of failing loudly at construction.
    #[test]
    fn test_add_node_rejects_pipe() {
        let mut graph = ISONGraph::new("test");
        assert!(matches!(
            graph.add_node("per|son", "1", props![]),
            Err(GraphError::InvalidIdentifier(_))
        ));
        assert!(matches!(
            graph.add_node("person", "1|2", props![]),
            Err(GraphError::InvalidIdentifier(_))
        ));
        assert_eq!(graph.node_count(), 0);
    }

    #[test]
    fn test_add_edge_rejects_colon_and_pipe_in_rel_type() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        assert!(matches!(
            graph.add_edge("KN:OWS", ("person", "1"), ("person", "2"), props![]),
            Err(GraphError::InvalidIdentifier(_))
        ));
        assert!(matches!(
            graph.add_edge("KN|OWS", ("person", "1"), ("person", "2"), props![]),
            Err(GraphError::InvalidIdentifier(_))
        ));
        assert_eq!(graph.edge_count(), 0);
    }

    #[test]
    fn test_unrepresentable_identifiers_are_rejected() {
        // Whitespace and empty used to be accepted and then blow up on round
        // trip: ':a b:1' splits mid-reference, and an id column holding 'a b'
        // reparses as two values. Rejected at construction instead.
        for bad in ["a b", "a\tb", "a\nb", ""] {
            let mut graph = ISONGraph::new("test");
            assert!(graph.add_node(bad, "1", props![]).is_err(), "type {:?}", bad);
            assert!(graph.add_node("p", bad, props![]).is_err(), "id {:?}", bad);
        }

        // ISONL cannot quote a block name, so types are stricter than ids.
        for bad in ["a\"b", "a\\b"] {
            let mut graph = ISONGraph::new("test");
            assert!(graph.add_node(bad, "1", props![]).is_err(), "type {:?}", bad);
            assert!(graph.add_node("p", bad, props![]).is_ok(), "id {:?}", bad);
        }
    }

    #[test]
    fn test_valid_identifiers_still_allowed() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("a-type", "an-id-1", props![]).unwrap();
        graph.add_node("b.c", "#2", props![]).unwrap();
        graph
            .add_edge("SHARES_BORDER_WITH", ("a-type", "an-id-1"), ("b.c", "#2"), props![])
            .unwrap();
        assert_eq!(graph.edge_count(), 1);

        let restored = ISONGraph::from_ison(&graph.to_ison(), None).unwrap();
        assert_eq!(restored.node_count(), 2);
        assert_eq!(restored.edge_count(), 1);
    }

    #[test]
    fn test_no_longer_crashes_on_ison_roundtrip() {
        // Previously: add_node("a:b", "1") succeeded, then to_ison() ->
        // from_ison() raised NodeNotFound - the actual bug this closes.
        let mut graph = ISONGraph::new("test");
        assert!(graph.add_node("a:b", "1", props![]).is_err());
    }

    #[test]
    fn test_no_longer_crashes_on_isonl_roundtrip() {
        let mut graph = ISONGraph::new("test");
        assert!(graph.add_node("rel|type", "1", props![]).is_err());
    }

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde_deserialize_rejects_bad_identifiers() {
        // Deserialization bypasses add_node/add_edge entirely (it rebuilds
        // the index maps directly from the payload), so a crafted or
        // corrupted JSON blob could smuggle in a ':' or '|' identifier that
        // the public API would have rejected - verify the TryFrom impl
        // re-applies the same guard.
        let bad = r#"{"name":"t","directed":true,"nodes":{"a:b":{"1":{"node_type":"a:b","id":"1","properties":{}}}},"edges":{}}"#;
        let result: std::result::Result<ISONGraph, _> = serde_json::from_str(bad);
        assert!(result.is_err());

        let good = r#"{"name":"t","directed":true,"nodes":{"a":{"1":{"node_type":"a","id":"1","properties":{}}}},"edges":{}}"#;
        let graph: ISONGraph = serde_json::from_str(good).unwrap();
        assert_eq!(graph.node_count(), 1);
    }

    #[test]
    fn test_add_and_has_edge() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();

        assert!(graph.has_edge("KNOWS", ("person", "1"), ("person", "2")));
        assert!(!graph.has_edge("KNOWS", ("person", "2"), ("person", "1")));
    }

    #[test]
    fn test_neighbors() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph.add_node("person", "3", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "3"), props![])
            .unwrap();

        let friends = graph.neighbors(&("person", "1"), Some("KNOWS"), Direction::Out);
        assert_eq!(friends.len(), 2);
    }

    #[test]
    fn test_multi_hop() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph.add_node("person", "3", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();
        graph
            .add_edge("KNOWS", ("person", "2"), ("person", "3"), props![])
            .unwrap();

        let two_hops = graph.multi_hop(&("person", "1"), Some("KNOWS"), 2, Direction::Out);
        assert_eq!(two_hops.len(), 1);
        assert_eq!(two_hops[0].id, "3");
    }

    #[test]
    fn test_shortest_path() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph.add_node("person", "3", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();
        graph
            .add_edge("KNOWS", ("person", "2"), ("person", "3"), props![])
            .unwrap();

        let path = graph
            .shortest_path(&("person", "1"), &("person", "3"), Some("KNOWS"), 10, Direction::Out)
            .unwrap();
        assert_eq!(path.length(), 2);
        assert_eq!(path.nodes.len(), 3);
    }

    #[test]
    fn test_node_count() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph.add_node("company", "100", props![]).unwrap();

        assert_eq!(graph.node_count(), 3);
        assert_eq!(graph.node_count_of_type("person"), 2);
        assert_eq!(graph.node_count_of_type("company"), 1);
    }

    #[test]
    fn test_to_ison() {
        let mut graph = ISONGraph::new("test");
        graph
            .add_node("person", "1", props![("name", "Alice")])
            .unwrap();
        graph
            .add_node("person", "2", props![("name", "Bob")])
            .unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();

        let ison = graph.to_ison();
        assert!(ison.contains("nodes.person"));
        assert!(ison.contains("edges.KNOWS"));
        assert!(ison.contains("Alice"));
    }

    fn special_values_graph() -> ISONGraph {
        let mut graph = ISONGraph::new("test");
        graph
            .add_node(
                "person",
                "1",
                props![
                    ("name", "Alice Smith"),
                    ("bio", "a|b"),
                    ("note", "line1\nline2"),
                    ("empty", ""),
                ],
            )
            .unwrap();
        graph
            .add_node(
                "person",
                "2",
                props![
                    ("name", "Bob"),
                    ("bio", "he said \"hi\""),
                    ("note", "x"),
                    ("empty", "y"),
                ],
            )
            .unwrap();
        graph
            .add_edge(
                "KNOWS",
                ("person", "1"),
                ("person", "2"),
                props![("where", "New York"), ("tag", "")],
            )
            .unwrap();
        graph
    }

    fn assert_special_values(restored: &ISONGraph) {
        assert_eq!(restored.node_count(), 2);
        assert_eq!(restored.edge_count(), 1);
        let n1 = restored.get_node("person", "1").unwrap();
        assert_eq!(crate::value_to_text(n1.properties.get("name").unwrap()), "Alice Smith");
        assert_eq!(crate::value_to_text(n1.properties.get("bio").unwrap()), "a|b");
        assert_eq!(crate::value_to_text(n1.properties.get("note").unwrap()), "line1\nline2");
        assert_eq!(crate::value_to_text(n1.properties.get("empty").unwrap()), "");
        let n2 = restored.get_node("person", "2").unwrap();
        assert_eq!(crate::value_to_text(n2.properties.get("bio").unwrap()), "he said \"hi\"");
        let e = restored
            .get_edge("KNOWS", ("person", "1"), ("person", "2"))
            .unwrap();
        assert_eq!(crate::value_to_text(e.properties.get("where").unwrap()), "New York");
        assert_eq!(crate::value_to_text(e.properties.get("tag").unwrap()), "");
    }

    #[test]
    fn test_ison_round_trip_special_values() {
        let graph = special_values_graph();
        let restored = ISONGraph::from_ison(&graph.to_ison(), Some("test")).unwrap();
        assert_special_values(&restored);
    }

    #[test]
    fn test_isonl_round_trip_special_values() {
        let graph = special_values_graph();
        let restored = ISONGraph::from_isonl(&graph.to_isonl(), Some("test")).unwrap();
        assert_special_values(&restored);
    }

    #[test]
    fn test_is_connected() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();

        assert!(graph.is_connected());

        graph.add_node("person", "3", props![]).unwrap();
        assert!(!graph.is_connected());
    }

    // =========================================================================
    // Deserialization Tests
    // =========================================================================

    #[test]
    fn test_from_ison() {
        let ison_text = r#"
nodes.person
id name age
alice Alice 30
bob Bob 25

edges.KNOWS
source target since
:person:alice :person:bob 2020
"#;

        let graph = ISONGraph::from_ison(ison_text, Some("test")).unwrap();
        assert_eq!(graph.node_count(), 2);
        assert_eq!(graph.edge_count(), 1);
        assert!(graph.has_node("person", "alice"));
        assert!(graph.has_node("person", "bob"));
        assert!(graph.has_edge("KNOWS", ("person", "alice"), ("person", "bob")));
    }

    #[test]
    fn test_from_isonl() {
        let isonl_text = "nodes.person|id name age|alice Alice 30\n\
            nodes.person|id name age|bob Bob 25\n\
            edges.KNOWS|source target since|:person:alice :person:bob 2020\n";

        let graph = ISONGraph::from_isonl(isonl_text, Some("test")).unwrap();
        assert_eq!(graph.node_count(), 2);
        assert_eq!(graph.edge_count(), 1);
        assert!(graph.has_node("person", "alice"));
        assert!(graph.has_edge("KNOWS", ("person", "alice"), ("person", "bob")));
    }

    #[test]
    fn test_from_ison_malformed_errors() {
        // Row with 3 values against 2 fields
        let bad_row = "nodes.person\nid name\nalice Alice Extra";
        assert!(matches!(
            ISONGraph::from_ison(bad_row, None),
            Err(GraphError::ParseError(_))
        ));

        // Header without '.'
        let bad_header = "people\nid name\nalice Alice";
        assert!(matches!(
            ISONGraph::from_ison(bad_header, None),
            Err(GraphError::ParseError(_))
        ));

        // Unknown block kind
        let bad_kind = "vertices.person\nid name\nalice Alice";
        assert!(matches!(
            ISONGraph::from_ison(bad_kind, None),
            Err(GraphError::ParseError(_))
        ));
    }

    #[test]
    fn test_from_isonl_malformed_errors() {
        // Missing values section
        assert!(matches!(
            ISONGraph::from_isonl("nodes.person|id name", None),
            Err(GraphError::ParseError(_))
        ));
        // A short row is padded with null by the shared ISON parser rather
        // than rejected - every port behaves this way now that emission and
        // parsing both go through it, so this is no longer an error here.
        let padded = ISONGraph::from_isonl("nodes.person|id name|alice", None).unwrap();
        assert_eq!(padded.node_count(), 1);
        // Header without '.'
        assert!(matches!(
            ISONGraph::from_isonl("nodesperson|id|alice", None),
            Err(GraphError::ParseError(_))
        ));
    }

    // =========================================================================
    // Additional Analysis Tests
    // =========================================================================

    #[test]
    fn test_connected_components() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph.add_node("person", "3", props![]).unwrap();
        graph.add_node("person", "4", props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "2"), props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "3"), ("person", "4"), props![]).unwrap();

        let components = graph.connected_components();
        assert_eq!(components.len(), 2);
    }

    #[test]
    fn test_all_paths() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph.add_node("person", "3", props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "2"), props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "2"), ("person", "3"), props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "3"), props![]).unwrap();

        let paths = graph.all_paths(&("person", "1"), &("person", "3"), None, 5, Direction::Out);
        assert_eq!(paths.len(), 2); // Direct path and via person 2
    }

    #[test]
    fn test_get_edge() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "2"), props![("since", "2020")]).unwrap();

        let edge = graph.get_edge("KNOWS", ("person", "1"), ("person", "2")).unwrap();
        assert_eq!(edge.rel_type, "KNOWS");
        assert_eq!(crate::value_to_text(edge.properties.get("since").unwrap()), "2020");
    }

    // =========================================================================
    // Pattern Query Tests
    // =========================================================================

    #[test]
    fn test_query_pattern() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph.add_node("person", "3", props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "2"), props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "2"), ("person", "3"), props![]).unwrap();

        let result = graph.query(":person:1 -[:KNOWS]-> *").unwrap();
        assert_eq!(result.len(), 1);
        assert_eq!(result[0].id, "2");

        let result2 = graph.query(":person:1 -[:KNOWS*2]-> *").unwrap();
        assert_eq!(result2.len(), 1);
        assert_eq!(result2[0].id, "3");
    }

    // =========================================================================
    // Fluent API Tests
    // =========================================================================

    #[test]
    fn test_start_method() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![("name", "Alice")]).unwrap();
        graph.add_node("person", "2", props![("name", "Bob")]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "2"), props![]).unwrap();

        let result = graph.start(&("person", "1"))
            .hop(Some("KNOWS"), Direction::Out)
            .collect();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].id, "2");
    }

    #[test]
    fn test_fluent_collect_nodes() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![("name", "Alice")]).unwrap();
        graph.add_node("person", "2", props![("name", "Bob")]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "2"), props![]).unwrap();

        let nodes = graph.start(&("person", "1"))
            .hop(Some("KNOWS"), Direction::Out)
            .collect_nodes();

        assert_eq!(nodes.len(), 1);
        assert_eq!(crate::value_to_text(nodes[0].properties.get("name").unwrap()), "Bob");
    }

    #[test]
    fn test_fluent_filter() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![("name", "Alice")]).unwrap();
        graph.add_node("person", "2", props![("name", "Bob")]).unwrap();
        graph.add_node("person", "3", props![("name", "Charlie")]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "2"), props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "3"), props![]).unwrap();

        let result = graph.start(&("person", "1"))
            .hop(Some("KNOWS"), Direction::Out)
            .filter(|n| n.properties.get("name").map(|v| crate::value_to_text(v) == "Bob").unwrap_or(false))
            .collect();

        assert_eq!(result.len(), 1);
        assert_eq!(result[0].id, "2");
    }

    // =========================================================================
    // Cycle / Direction / Dedup Tests
    // =========================================================================

    #[test]
    fn test_undirected_single_edge_no_cycle() {
        let mut graph = ISONGraph::undirected("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();
        // A single undirected edge must not be reported as a cycle
        assert!(!graph.has_cycle(None));

        // A chain of two undirected edges is still acyclic
        graph.add_node("person", "3", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "2"), ("person", "3"), props![])
            .unwrap();
        assert!(!graph.has_cycle(None));

        // Closing the triangle creates a real cycle
        graph
            .add_edge("KNOWS", ("person", "3"), ("person", "1"), props![])
            .unwrap();
        assert!(graph.has_cycle(None));
    }

    #[test]
    fn test_directed_cycle_detection() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("t", "a", props![]).unwrap();
        graph.add_node("t", "b", props![]).unwrap();
        graph.add_edge("R", ("t", "a"), ("t", "b"), props![]).unwrap();
        assert!(!graph.has_cycle(None));
        graph.add_edge("R", ("t", "b"), ("t", "a"), props![]).unwrap();
        assert!(graph.has_cycle(None));
        // Filtering on a rel type with no cycle
        assert!(!graph.has_cycle(Some("OTHER")));
    }

    #[test]
    fn test_neighbors_both_undirected_dedup() {
        let mut graph = ISONGraph::undirected("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();

        let neighbors = graph.neighbors(&("person", "1"), None, Direction::Both);
        assert_eq!(neighbors.len(), 1);
        assert_eq!(neighbors[0].id, "2");
    }

    #[test]
    fn test_path_exists_direction() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();

        assert!(graph.path_exists(&("person", "1"), &("person", "2"), None, 5, Direction::Out));
        assert!(!graph.path_exists(&("person", "2"), &("person", "1"), None, 5, Direction::Out));
        assert!(graph.path_exists(&("person", "2"), &("person", "1"), None, 5, Direction::In));
        assert!(graph.path_exists(&("person", "2"), &("person", "1"), None, 5, Direction::Both));
    }

    #[test]
    fn test_max_hops_usize_max_no_overflow() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![])
            .unwrap();

        assert!(graph.path_exists(
            &("person", "1"),
            &("person", "2"),
            None,
            usize::MAX,
            Direction::Out
        ));
        let paths = graph.all_paths(&("person", "1"), &("person", "2"), None, usize::MAX, Direction::Out);
        assert_eq!(paths.len(), 1);
    }

    #[test]
    fn test_all_paths_start_equals_end() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        let paths = graph.all_paths(&("person", "1"), &("person", "1"), None, 5, Direction::Out);
        assert_eq!(paths.len(), 1);
        assert_eq!(paths[0].length(), 0);
    }

    // =========================================================================
    // Serde Tests
    // =========================================================================

    #[cfg(feature = "serde")]
    #[test]
    fn test_serde_round_trip() {
        let mut graph = ISONGraph::new("test");
        graph
            .add_node("person", "1", props![("name", "Alice")])
            .unwrap();
        graph
            .add_node("person", "2", props![("name", "Bob")])
            .unwrap();
        graph
            .add_edge("KNOWS", ("person", "1"), ("person", "2"), props![("since", "2020")])
            .unwrap();

        let json = serde_json::to_string(&graph).unwrap();
        // Internal indexes must not be serialized (every edge only once)
        assert!(!json.contains("out_edges"));
        assert!(!json.contains("in_edges"));
        assert!(!json.contains("edge_set"));

        let restored: ISONGraph = serde_json::from_str(&json).unwrap();
        assert_eq!(restored.name, "test");
        assert_eq!(restored.node_count(), 2);
        assert_eq!(restored.edge_count(), 1);
        assert!(restored.has_edge("KNOWS", ("person", "1"), ("person", "2")));

        // Indexes are rebuilt: traversal works on the deserialized graph
        let neighbors = restored.neighbors(&("person", "1"), Some("KNOWS"), Direction::Out);
        assert_eq!(neighbors.len(), 1);
        assert_eq!(neighbors[0].id, "2");
        assert_eq!(restored.in_degree(&("person", "2")), 1);
    }

    #[test]
    fn test_fluent_hops() {
        let mut graph = ISONGraph::new("test");
        graph.add_node("person", "1", props![]).unwrap();
        graph.add_node("person", "2", props![]).unwrap();
        graph.add_node("person", "3", props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "1"), ("person", "2"), props![]).unwrap();
        graph.add_edge("KNOWS", ("person", "2"), ("person", "3"), props![]).unwrap();

        let count = graph.start(&("person", "1"))
            .hops(2, Some("KNOWS"), Direction::Out)
            .count();

        assert_eq!(count, 1);
    }

    // =========================================================================
    // Canonical Serialization
    //
    // to_ison()/to_isonl() must be byte-identical for the same logical graph
    // regardless of insertion order (node type blocks, node rows, edge type
    // blocks, and edge rows are all sorted, ordinal string comparison). The
    // golden fixture below is shared verbatim across every ISONGraph
    // language port's test suite (Python, JS, TS, Rust, C++, C#) as the
    // cross-language byte-identity contract - if it changes here, it must
    // change everywhere.
    // =========================================================================

    const CANONICAL_GOLDEN_ISON: &str = r#"edges.KNOWS
since source target weight
"2020" :person:1 :person:2 1.75
"2021" :person:10 :person:1 0.5

edges.WORKS_AT
role source target
"Lead Engineer" :person:1 :company:acme

edges.WORKS_ON
hours source target
3 :person:10 :project:p1
5 :person:2 :project:p1

nodes.company
id listed name staff
acme false "Acme Corp" 42

nodes.person
id active age name score
"1" true "30" Alice 9.5
"10" false "22" Jax -2.25
"2" null "25" Bob null

nodes.project
id budget name tag
p1 1250 Alpha a|b"#;

    const CANONICAL_GOLDEN_ISONL: &str = r#"edges.KNOWS|since source target weight|"2020" :person:1 :person:2 1.75
edges.KNOWS|since source target weight|"2021" :person:10 :person:1 0.5
edges.WORKS_AT|role source target|"Lead Engineer" :person:1 :company:acme
edges.WORKS_ON|hours source target|3 :person:10 :project:p1
edges.WORKS_ON|hours source target|5 :person:2 :project:p1
nodes.company|id listed name staff|acme false "Acme Corp" 42
nodes.person|id active age name score|"1" true "30" Alice 9.5
nodes.person|id active age name score|"10" false "22" Jax -2.25
nodes.person|id active age name score|"2" null "25" Bob null
nodes.project|id budget name tag|p1 1250 Alpha "a\|b""#;

    #[allow(clippy::type_complexity)]
    fn build_canonical_fixture(order: usize) -> ISONGraph {
        let mut g = ISONGraph::new("fixture");
        // Mixed types on purpose: the string ids and string ages pin that a
        // value which would reparse as a number stays quoted, while the int,
        // float and bool pin that a real number does not. No integral float
        // appears - JavaScript cannot originate one, so a shared fixture must
        // not ask for it.
        let nodes: Vec<(&str, &str, Vec<(&str, Value)>)> = vec![
            ("company", "acme", props![("name", "Acme Corp"), ("staff", 42i64), ("listed", false)]),
            ("person", "1", props![("name", "Alice"), ("age", "30"), ("score", 9.5f64), ("active", true)]),
            ("person", "10", props![("name", "Jax"), ("age", "22"), ("score", -2.25f64), ("active", false)]),
            ("person", "2", props![("name", "Bob"), ("age", "25")]),
            ("project", "p1", props![("name", "Alpha"), ("tag", "a|b"), ("budget", 1250i64)]),
        ];
        let edges: Vec<(&str, (&str, &str), (&str, &str), Vec<(&str, Value)>)> = vec![
            ("KNOWS", ("person", "10"), ("person", "1"), props![("since", "2021"), ("weight", 0.5f64)]),
            ("KNOWS", ("person", "1"), ("person", "2"), props![("since", "2020"), ("weight", 1.75f64)]),
            ("WORKS_AT", ("person", "1"), ("company", "acme"), props![("role", "Lead Engineer")]),
            ("WORKS_ON", ("person", "2"), ("project", "p1"), props![("hours", 5i64)]),
            ("WORKS_ON", ("person", "10"), ("project", "p1"), props![("hours", 3i64)]),
        ];
        let mut n = nodes;
        let mut e = edges;
        if order != 0 {
            n.reverse();
            e.reverse();
        }
        for (t, i, props) in n {
            g.add_node(t, i, props).unwrap();
        }
        for (rel, s, t, props) in e {
            g.add_edge(rel, s, t, props).unwrap();
        }
        g
    }

    #[test]
    fn test_to_ison_independent_of_insertion_order() {
        assert_eq!(build_canonical_fixture(0).to_ison(), build_canonical_fixture(1).to_ison());
    }

    #[test]
    fn test_to_isonl_independent_of_insertion_order() {
        assert_eq!(build_canonical_fixture(0).to_isonl(), build_canonical_fixture(1).to_isonl());
    }

    #[test]
    fn test_to_ison_matches_golden_fixture() {
        assert_eq!(build_canonical_fixture(0).to_ison(), CANONICAL_GOLDEN_ISON);
        assert_eq!(build_canonical_fixture(1).to_ison(), CANONICAL_GOLDEN_ISON);
    }

    #[test]
    fn test_to_isonl_matches_golden_fixture() {
        assert_eq!(build_canonical_fixture(0).to_isonl(), CANONICAL_GOLDEN_ISONL);
        assert_eq!(build_canonical_fixture(1).to_isonl(), CANONICAL_GOLDEN_ISONL);
    }

    #[test]
    fn test_ordinal_not_numeric_sort() {
        // "1" < "10" < "2" ordinally.
        let ison = build_canonical_fixture(0).to_ison();
        let person_block = ison.split("nodes.person
").nth(1).unwrap().split("

").next().unwrap();
        // Ids are quoted because the fixture uses string ids - the canonical
        // writer quotes a string that would otherwise reparse as a number, so
        // the round trip stays lossless. Strip that to read the order.
        let ids: Vec<&str> = person_block
            .lines()
            .skip(1)
            .map(|l| l.split(' ').next().unwrap().trim_matches('"'))
            .collect();
        assert_eq!(ids, vec!["1", "10", "2"]);
    }

    #[test]
    fn test_roundtrip_preserves_canonical_form() {
        let g = build_canonical_fixture(0);
        let reloaded = ISONGraph::from_ison(&g.to_ison(), None).unwrap();
        assert_eq!(reloaded.to_ison(), g.to_ison());
    }

    // =========================================================================
    // Identity contract: directedness survives dump -> load, a Python
    // MultiGraph document is refused rather than silently flattened, and a
    // value the wire format cannot spell is refused before it enters the
    // graph. UNDIRECTED_FIXTURE is the exact bytes the Python port writes
    // for the same graph (string ids, so every port can represent it).
    // =========================================================================

    const UNDIRECTED_FIXTURE: &str = "edges.R\nsource target w\n:p:1 :p:2 1\n:p:2 :p:1 1\n\n\
                                      meta.graph\ndirected\nfalse\n\n\
                                      nodes.p\nid name\n\"1\" A\n\"2\" null";

    fn build_undirected() -> ISONGraph {
        let mut g = ISONGraph::undirected("u");
        g.add_node("p", "1", props![("name", "A")]).unwrap();
        g.add_node("p", "2", props![]).unwrap();
        g.add_edge("R", ("p", "1"), ("p", "2"), props![("w", 1i64)]).unwrap();
        g
    }

    #[test]
    fn test_undirected_writes_meta_and_matches_python_bytes() {
        assert_eq!(build_undirected().to_ison(), UNDIRECTED_FIXTURE);
    }

    #[test]
    fn test_directed_output_has_no_meta_block() {
        assert!(!build_canonical_fixture(0).to_ison().contains("meta.graph"));
        assert!(!build_canonical_fixture(0).to_isonl().contains("meta.graph"));
    }

    #[test]
    fn test_undirected_roundtrip() {
        let g = build_undirected();
        let r = ISONGraph::from_ison(&g.to_ison(), None).unwrap();
        assert!(!r.directed);
        assert_eq!(r.edge_count(), g.edge_count());
        assert!(r.has_edge("R", ("p", "1"), ("p", "2")));
        assert!(r.has_edge("R", ("p", "2"), ("p", "1")));
        assert_eq!(r.to_ison(), g.to_ison());

        let rl = ISONGraph::from_isonl(&g.to_isonl(), None).unwrap();
        assert!(!rl.directed);
        assert_eq!(rl.to_isonl(), g.to_isonl());
    }

    #[test]
    fn test_directed_duplicate_row_still_fails() {
        let text = "edges.R\nsource target\n:p:1 :p:2\n:p:1 :p:2\n\nnodes.p\nid\n\"1\"\n\"2\"";
        assert!(matches!(
            ISONGraph::from_ison(text, None),
            Err(GraphError::DuplicateEdge(_))
        ));
    }

    #[test]
    fn test_explicit_directed_true_is_honoured() {
        let g = ISONGraph::from_ison("meta.graph\ndirected\ntrue\n\nnodes.p\nid\n\"1\"", None).unwrap();
        assert!(g.directed);
    }

    #[test]
    fn test_multigraph_document_is_refused() {
        let text = "meta.graph\ndirected format version\ntrue ison-graph-multigraph-v1 1\n\n\
                    nodes.p\nid\n\"1\"";
        match ISONGraph::from_ison(text, None) {
            Err(GraphError::ParseError(msg)) => assert!(msg.contains("MultiGraph"), "{}", msg),
            other => panic!("expected ParseError, got {:?}", other.map(|_| ())),
        }
    }

    #[test]
    fn test_non_finite_float_properties_are_refused() {
        let mut g = ISONGraph::new("t");
        assert!(matches!(
            g.add_node("p", "1", props![("x", f64::NAN)]),
            Err(GraphError::InvalidPropertyValue(_))
        ));
        g.add_node("p", "1", props![]).unwrap();
        g.add_node("p", "2", props![]).unwrap();
        assert!(matches!(
            g.update_node("p", "1", props![("x", f64::INFINITY)]),
            Err(GraphError::InvalidPropertyValue(_))
        ));
        assert!(g.get_node("p", "1").unwrap().properties.is_empty());
        assert!(matches!(
            g.add_edge("R", ("p", "1"), ("p", "2"), props![("w", f64::NEG_INFINITY)]),
            Err(GraphError::InvalidPropertyValue(_))
        ));
        assert_eq!(g.edge_count(), 0);
        // Finite floats are untouched.
        g.add_edge("R", ("p", "1"), ("p", "2"), props![("w", 1.5f64)]).unwrap();
    }
}