animsmith-core 0.7.0

Engine-agnostic data model, sampling, measurements, and checks for the animsmith animation-clip linter
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
//! Bounded engine-neutral evidence for raw transform-path addressability.
//!
//! Format loaders project original node identities, parent chains, names, and
//! synthetic-helper classification into this contract before normalized
//! skeleton names can erase the distinction. The closed V1 grammar has no
//! escaping: a source segment which cannot be represented remains visible as
//! bounded row evidence and makes absence unprovable.

use crate::bounded_deserialize::{
    CappedSequence, deserialize_capped_option_string, deserialize_capped_sequence,
    deserialize_capped_string,
};
use crate::{InputIdentity, SourceFormatV1};
use serde::de::{Error as _, IgnoredAny, SeqAccess, Visitor};
use serde::{Deserialize, Serialize};
use std::fmt;

/// Immutable raw transform-path inventory contract identity.
pub const RAW_TRANSFORM_PATH_INVENTORY_V1_ID: &str = "urn:animsmith:raw-transform-path-inventory:1";
/// Maximum source-node rows retained by one inventory.
pub const RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS: usize = 4_096;
/// Maximum UTF-8 bytes in one addressable path segment.
pub const RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES: usize = 1_024;
/// Maximum UTF-8 bytes in one complete addressable path.
pub const RAW_TRANSFORM_PATH_V1_MAX_PATH_BYTES: usize = 4_096;
/// Maximum segments in one addressable path and parents in one retained chain.
pub const RAW_TRANSFORM_PATH_V1_MAX_DEPTH: usize = 256;
/// Maximum parent-index references retained across one inventory.
pub const RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_PARENT_REFERENCES: usize = 65_536;
/// Maximum aggregate UTF-8 bytes retained across names and materialized paths.
pub const RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_TEXT_BYTES: usize = 1024 * 1024;

/// One validated path in the closed, unescaped V1 grammar.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize)]
#[serde(transparent)]
pub struct RawTransformPathV1(String);

impl RawTransformPathV1 {
    /// Parse and validate one configured transform path.
    ///
    /// Matching never trims, normalizes Unicode, strips namespaces, or changes
    /// case. `/` is the only separator and has no escape syntax.
    ///
    /// # Errors
    ///
    /// Returns a typed syntax error for an empty path, an empty or reserved
    /// segment, a forbidden character, or a V1 length/depth overflow.
    pub fn parse(value: &str) -> Result<Self, RawTransformPathSyntaxErrorV1> {
        validate_path(value)?;
        Ok(Self(value.to_owned()))
    }

    /// Exact UTF-8 path spelling.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Exact case-sensitive segment sequence.
    pub fn segments(&self) -> impl Iterator<Item = &str> {
        self.0.split('/')
    }
}

impl<'de> Deserialize<'de> for RawTransformPathV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let value = deserialize_capped_string(deserializer, RAW_TRANSFORM_PATH_V1_MAX_PATH_BYTES)?;
        Self::parse(&value).map_err(serde::de::Error::custom)
    }
}

/// Why configured path syntax is invalid under the closed V1 grammar.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum RawTransformPathSyntaxErrorV1 {
    /// The aggregate configured path is empty.
    #[error("transform path must contain at least one nonempty segment")]
    EmptyPath,
    /// The path starts or ends with `/`, or contains `//`.
    #[error("transform path contains an empty segment")]
    EmptySegment,
    /// A segment is the reserved single-dot spelling.
    #[error("transform path contains reserved '.' segment")]
    DotSegment,
    /// A segment is the reserved double-dot spelling.
    #[error("transform path contains reserved '..' segment")]
    DotDotSegment,
    /// Backslash has no escaping or separator meaning in V1.
    #[error("transform path contains forbidden backslash")]
    Backslash,
    /// A literal slash cannot occur inside one source segment.
    #[error("transform path source segment contains forbidden slash")]
    Slash,
    /// A Unicode control character is forbidden.
    #[error("transform path contains a control character")]
    ControlCharacter,
    /// A Unicode format character is forbidden.
    #[error("transform path contains a format character")]
    FormatCharacter,
    /// One segment exceeded its independent UTF-8 byte bound.
    #[error("transform path segment exceeds the V1 byte bound")]
    SegmentTooLong,
    /// The aggregate path exceeded its independent UTF-8 byte bound.
    #[error("transform path exceeds the V1 byte bound")]
    PathTooLong,
    /// The segment sequence exceeded the V1 depth bound.
    #[error("transform path exceeds the V1 segment bound")]
    TooManySegments,
}

fn validate_path(value: &str) -> Result<(), RawTransformPathSyntaxErrorV1> {
    if value.is_empty() {
        return Err(RawTransformPathSyntaxErrorV1::EmptyPath);
    }
    if value.len() > RAW_TRANSFORM_PATH_V1_MAX_PATH_BYTES {
        return Err(RawTransformPathSyntaxErrorV1::PathTooLong);
    }
    let mut count = 0usize;
    for segment in value.split('/') {
        count = count.saturating_add(1);
        if count > RAW_TRANSFORM_PATH_V1_MAX_DEPTH {
            return Err(RawTransformPathSyntaxErrorV1::TooManySegments);
        }
        validate_segment(segment)?;
    }
    Ok(())
}

fn validate_segment(segment: &str) -> Result<(), RawTransformPathSyntaxErrorV1> {
    if segment.is_empty() {
        return Err(RawTransformPathSyntaxErrorV1::EmptySegment);
    }
    if segment == "." {
        return Err(RawTransformPathSyntaxErrorV1::DotSegment);
    }
    if segment == ".." {
        return Err(RawTransformPathSyntaxErrorV1::DotDotSegment);
    }
    if segment.len() > RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES {
        return Err(RawTransformPathSyntaxErrorV1::SegmentTooLong);
    }
    for character in segment.chars() {
        if character == '/' {
            return Err(RawTransformPathSyntaxErrorV1::Slash);
        }
        if character == '\\' {
            return Err(RawTransformPathSyntaxErrorV1::Backslash);
        }
        if character.is_control() {
            return Err(RawTransformPathSyntaxErrorV1::ControlCharacter);
        }
        if is_unicode_format_character(character) {
            return Err(RawTransformPathSyntaxErrorV1::FormatCharacter);
        }
    }
    Ok(())
}

// Unicode General_Category=Cf ranges from the Unicode scalar repertoire.
// `char::is_control()` separately covers General_Category=Cc.
fn is_unicode_format_character(character: char) -> bool {
    matches!(
        character as u32,
        0x00ad
            | 0x061c
            | 0x06dd
            | 0x070f
            | 0x0890..=0x0891
            | 0x08e2
            | 0x180e
            | 0x200b..=0x200f
            | 0x202a..=0x202e
            | 0x2060..=0x2064
            | 0x2066..=0x206f
            | 0xfeff
            | 0xfff9..=0xfffb
            | 0x0600..=0x0605
            | 0x110bd
            | 0x110cd
            | 0x13430..=0x1343f
            | 0x1bca0..=0x1bca3
            | 0x1d173..=0x1d17a
            | 0xe0001
            | 0xe0020..=0xe007f
    )
}

/// Original or synthetic role of one loader node.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RawTransformPathNodeKindV1 {
    /// Original source transform node eligible for exact matching.
    Source,
    /// ufbx's implicit scene root, never a configured path segment.
    ImplicitUfbxRoot,
    /// Synthetic helper generated for an FBX geometric transform.
    GeometryTransformHelper,
    /// Synthetic helper generated for scale compensation.
    ScaleCompensationHelper,
    /// A conservatively classified helper carrying both ufbx flags.
    GeometryAndScaleHelper,
}

impl RawTransformPathNodeKindV1 {
    /// Whether this row can satisfy a configured motion path.
    pub const fn is_matchable(self) -> bool {
        matches!(self, Self::Source)
    }
}

/// Addressability state of one retained source row.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RawTransformPathRowAddressabilityV1 {
    /// The exact path is retained and can match.
    Addressable,
    /// The implicit root is retained but excluded from path segments.
    ExcludedImplicitRoot,
    /// A synthetic helper is retained but excluded from path segments and matching.
    ExcludedSyntheticHelper,
    /// This source segment violates the closed grammar.
    UnrepresentableSourceSegment,
    /// An earlier source segment on this path violates the closed grammar.
    UnrepresentableAncestorSegment,
    /// The complete source path exceeded the independent aggregate byte bound.
    PathTooLong,
}

/// Coverage of the raw transform inventory and its exact-addressability proof.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(tag = "state", content = "reason", rename_all = "snake_case")]
pub enum RawTransformPathCoverageV1 {
    /// Every source row and addressability fact is retained.
    Complete,
    /// A canonical source prefix or all rows with incomplete addressability are retained.
    Partial(RawTransformPathCoverageReasonV1),
    /// No source rows are available.
    Unavailable(RawTransformPathCoverageReasonV1),
}

impl RawTransformPathCoverageV1 {
    /// Whether a zero-match search proves absence.
    pub const fn proves_absence(self) -> bool {
        matches!(self, Self::Complete)
    }
}

/// Why raw transform-path coverage is incomplete.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum RawTransformPathCoverageReasonV1 {
    /// At least one original source segment cannot be expressed in V1 grammar.
    UnrepresentableSourceSegment,
    /// Row, parent-chain, text, or path-depth projection work exceeded a V1 bound.
    ProjectionBudgetExceeded,
    /// The format loader could not expose raw transform nodes.
    LoaderEvidenceUnavailable,
}

/// Loader input for one source-order node.
#[derive(Debug, Clone, Copy)]
pub struct RawTransformPathNodeInputV1<'a> {
    /// Stable inventory node identity.
    ///
    /// Original and implicit-root rows use the raw-preserving loader index;
    /// synthetic helpers occupy a disjoint appended range.
    pub source_node_index: u64,
    /// Direct parent identity in the same inventory domain.
    pub parent_source_node_index: Option<u64>,
    /// Exact parser-projected UTF-8 source name; the implicit root uses `None`.
    pub source_name: Option<&'a str>,
    /// Same-load normalized document bone index for this node.
    ///
    /// An original node which could not be correlated by immutable parser
    /// element identity uses `None`.
    pub projected_bone_index: Option<u64>,
    /// Original or synthetic node role.
    pub kind: RawTransformPathNodeKindV1,
}

/// One bounded raw transform-path inventory row.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(deny_unknown_fields)]
pub struct RawTransformPathNodeRowV1 {
    source_node_index: u64,
    parent_source_node_index: Option<u64>,
    #[serde(deserialize_with = "deserialize_parent_chain")]
    parent_chain: Vec<u64>,
    source_name: Option<String>,
    source_name_utf8_bytes: u64,
    projected_bone_index: Option<u64>,
    kind: RawTransformPathNodeKindV1,
    addressability: RawTransformPathRowAddressabilityV1,
    addressable_path: Option<RawTransformPathV1>,
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawTransformPathNodeRowWireV1 {
    source_node_index: u64,
    parent_source_node_index: Option<u64>,
    #[serde(deserialize_with = "deserialize_parent_chain")]
    parent_chain: Vec<u64>,
    #[serde(deserialize_with = "deserialize_source_name")]
    source_name: Option<String>,
    source_name_utf8_bytes: u64,
    projected_bone_index: Option<u64>,
    kind: RawTransformPathNodeKindV1,
    addressability: RawTransformPathRowAddressabilityV1,
    addressable_path: Option<RawTransformPathV1>,
}

impl<'de> Deserialize<'de> for RawTransformPathNodeRowV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let wire = RawTransformPathNodeRowWireV1::deserialize(deserializer)?;
        Ok(Self {
            source_node_index: wire.source_node_index,
            parent_source_node_index: wire.parent_source_node_index,
            parent_chain: wire.parent_chain,
            source_name: wire.source_name,
            source_name_utf8_bytes: wire.source_name_utf8_bytes,
            projected_bone_index: wire.projected_bone_index,
            kind: wire.kind,
            addressability: wire.addressability,
            addressable_path: wire.addressable_path,
        })
    }
}

fn deserialize_source_name<'de, D>(deserializer: D) -> Result<Option<String>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    deserialize_capped_option_string(deserializer, RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES)
}

impl RawTransformPathNodeRowV1 {
    /// Stable raw-original or appended synthetic inventory identity.
    pub const fn source_node_index(&self) -> u64 {
        self.source_node_index
    }
    /// Direct parent identity in the same inventory domain.
    pub const fn parent_source_node_index(&self) -> Option<u64> {
        self.parent_source_node_index
    }
    /// Root-to-direct-parent chain, including excluded implicit/helper rows.
    pub fn parent_chain(&self) -> &[u64] {
        &self.parent_chain
    }
    /// Exact retained source name, or `None` when absent/redacted by a bound.
    pub fn source_name(&self) -> Option<&str> {
        self.source_name.as_deref()
    }
    /// Original UTF-8 byte length even when the spelling could not be retained.
    pub const fn source_name_utf8_bytes(&self) -> u64 {
        self.source_name_utf8_bytes
    }
    /// Same-load normalized document bone identity, when correlated exactly.
    pub const fn projected_bone_index(&self) -> Option<u64> {
        self.projected_bone_index
    }
    /// Original or synthetic node role.
    pub const fn kind(&self) -> RawTransformPathNodeKindV1 {
        self.kind
    }
    /// Exact-addressability state.
    pub const fn addressability(&self) -> RawTransformPathRowAddressabilityV1 {
        self.addressability
    }
    /// Exact path for a matchable original source node.
    pub const fn addressable_path(&self) -> Option<&RawTransformPathV1> {
        self.addressable_path.as_ref()
    }
}

/// Invalid source-order data supplied by a format projection.
#[derive(Debug, Clone, Copy, PartialEq, Eq, thiserror::Error)]
#[non_exhaustive]
pub enum RawTransformPathInventoryErrorV1 {
    /// The immutable semantic schema identity changed.
    #[error("raw transform inventory schema identity is invalid")]
    InvalidSchema,
    /// V1 is currently a raw FBX transform-path contract.
    #[error("raw transform inventory source format is not FBX")]
    UnsupportedSourceFormat,
    /// More rows than the V1 source-node ceiling were decoded.
    #[error("raw transform inventory exceeds its row bound")]
    TooManyRows,
    /// Aggregate parent-chain evidence exceeded the V1 ceiling.
    #[error("raw transform inventory exceeds its parent-reference bound")]
    TooManyParentReferences,
    /// Aggregate retained source/path text exceeded the V1 ceiling.
    #[error("raw transform inventory exceeds its text bound")]
    TooMuchText,
    /// Source node indices must be zero-based and contiguous.
    #[error("raw transform nodes are not in canonical source-index order")]
    NonCanonicalSourceNodeIndex,
    /// Parent identity must reference an earlier retained source node.
    #[error("raw transform node parent does not reference an earlier node")]
    InvalidParent,
    /// Exactly one first row must identify the implicit ufbx root.
    #[error("raw transform inventory has an invalid implicit-root classification")]
    InvalidImplicitRoot,
    /// Retained row spelling, path, or addressability disagrees with its ancestry.
    #[error("raw transform inventory row addressability is not canonical")]
    InvalidAddressability,
    /// A projected bone identity is outside the same-load normalized document.
    #[error("raw transform inventory projected bone index is out of range")]
    ProjectedBoneOutOfRange,
    /// Two original source nodes cannot claim the same normalized bone identity.
    #[error("raw transform inventory projected bone index is duplicated")]
    DuplicateProjectedBone,
    /// Coverage contradicts the retained source/addressability rows.
    #[error("raw transform inventory coverage contradicts its rows")]
    InvalidCoverage,
}

/// Bounded same-load raw transform-path inventory.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
pub struct RawTransformPathInventoryV1 {
    schema: String,
    primary_input: InputIdentity,
    source_format: SourceFormatV1,
    projected_bone_count: u64,
    coverage: RawTransformPathCoverageV1,
    rows: Vec<RawTransformPathNodeRowV1>,
}

impl RawTransformPathInventoryV1 {
    /// Project canonical source-order nodes into bounded path evidence.
    ///
    /// Synthetic helpers and the implicit ufbx root remain rows and parent-chain
    /// identities, but do not contribute path segments and cannot match.
    ///
    /// # Errors
    ///
    /// Returns an error when source indices, parents, or implicit-root
    /// classification violate the loader-order contract.
    pub fn from_nodes<'a>(
        primary_input: InputIdentity,
        source_format: SourceFormatV1,
        projected_bone_count: u64,
        nodes: impl IntoIterator<Item = RawTransformPathNodeInputV1<'a>>,
    ) -> Result<Self, RawTransformPathInventoryErrorV1> {
        let mut rows: Vec<RawTransformPathNodeRowV1> = Vec::new();
        let mut effective_paths: Vec<Option<String>> = Vec::new();
        let mut coverage = RawTransformPathCoverageV1::Complete;
        let mut parent_references = 0usize;
        let mut text_bytes = 0usize;

        for input in nodes {
            let expected = rows.len() as u64;
            if input.source_node_index != expected {
                return Err(RawTransformPathInventoryErrorV1::NonCanonicalSourceNodeIndex);
            }
            if rows.len() == RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS {
                coverage = RawTransformPathCoverageV1::Partial(
                    RawTransformPathCoverageReasonV1::ProjectionBudgetExceeded,
                );
                break;
            }
            if expected == 0 {
                if input.parent_source_node_index.is_some()
                    || input.kind != RawTransformPathNodeKindV1::ImplicitUfbxRoot
                {
                    return Err(RawTransformPathInventoryErrorV1::InvalidImplicitRoot);
                }
            } else if input.kind == RawTransformPathNodeKindV1::ImplicitUfbxRoot {
                return Err(RawTransformPathInventoryErrorV1::InvalidImplicitRoot);
            }
            let parent = match input.parent_source_node_index {
                Some(parent) if parent < expected => Some(parent as usize),
                Some(_) => return Err(RawTransformPathInventoryErrorV1::InvalidParent),
                None => None,
            };
            let mut parent_chain = parent.map_or_else(Vec::new, |parent| {
                let mut chain = rows[parent].parent_chain.clone();
                chain.push(parent as u64);
                chain
            });
            if parent_chain.len() > RAW_TRANSFORM_PATH_V1_MAX_DEPTH
                || parent_references.saturating_add(parent_chain.len())
                    > RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_PARENT_REFERENCES
            {
                coverage = RawTransformPathCoverageV1::Partial(
                    RawTransformPathCoverageReasonV1::ProjectionBudgetExceeded,
                );
                break;
            }
            parent_references += parent_chain.len();

            let source_name_bytes = input.source_name.map_or(0, str::len);
            let retained_name = input
                .source_name
                .filter(|name| name.len() <= RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES)
                .map(str::to_owned);
            let inherited = parent
                .and_then(|parent| effective_paths[parent].as_deref())
                .unwrap_or("");
            let (addressability, effective_path, addressable_path) = match input.kind {
                RawTransformPathNodeKindV1::ImplicitUfbxRoot => (
                    RawTransformPathRowAddressabilityV1::ExcludedImplicitRoot,
                    Some(String::new()),
                    None,
                ),
                RawTransformPathNodeKindV1::GeometryTransformHelper
                | RawTransformPathNodeKindV1::ScaleCompensationHelper
                | RawTransformPathNodeKindV1::GeometryAndScaleHelper => (
                    RawTransformPathRowAddressabilityV1::ExcludedSyntheticHelper,
                    parent.and_then(|parent| effective_paths[parent].clone()),
                    None,
                ),
                RawTransformPathNodeKindV1::Source => match input.source_name {
                    Some(name) if validate_segment(name).is_ok() => {
                        if parent.is_some() && effective_paths[parent.unwrap()].is_none() {
                            coverage = incomplete_addressability(coverage);
                            (
                                RawTransformPathRowAddressabilityV1::UnrepresentableAncestorSegment,
                                None,
                                None,
                            )
                        } else {
                            let path = if inherited.is_empty() {
                                name.to_owned()
                            } else {
                                format!("{inherited}/{name}")
                            };
                            if path.len() > RAW_TRANSFORM_PATH_V1_MAX_PATH_BYTES
                                || path.split('/').count() > RAW_TRANSFORM_PATH_V1_MAX_DEPTH
                            {
                                coverage = incomplete_addressability(coverage);
                                (RawTransformPathRowAddressabilityV1::PathTooLong, None, None)
                            } else {
                                let parsed = RawTransformPathV1(path.clone());
                                (
                                    RawTransformPathRowAddressabilityV1::Addressable,
                                    Some(path),
                                    Some(parsed),
                                )
                            }
                        }
                    }
                    _ => {
                        coverage = incomplete_addressability(coverage);
                        (
                            RawTransformPathRowAddressabilityV1::UnrepresentableSourceSegment,
                            None,
                            None,
                        )
                    }
                },
            };

            let row_text = retained_name.as_ref().map_or(0, String::len)
                + addressable_path
                    .as_ref()
                    .map_or(0, |path| path.as_str().len());
            if text_bytes.saturating_add(row_text) > RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_TEXT_BYTES
            {
                coverage = RawTransformPathCoverageV1::Partial(
                    RawTransformPathCoverageReasonV1::ProjectionBudgetExceeded,
                );
                break;
            }
            text_bytes += row_text;
            rows.push(RawTransformPathNodeRowV1 {
                source_node_index: input.source_node_index,
                parent_source_node_index: input.parent_source_node_index,
                parent_chain: std::mem::take(&mut parent_chain),
                source_name: retained_name,
                source_name_utf8_bytes: source_name_bytes as u64,
                projected_bone_index: input.projected_bone_index,
                kind: input.kind,
                addressability,
                addressable_path,
            });
            effective_paths.push(effective_path);
        }
        let value = Self {
            schema: RAW_TRANSFORM_PATH_INVENTORY_V1_ID.to_owned(),
            primary_input,
            source_format,
            projected_bone_count,
            coverage,
            rows,
        };
        value.validate()?;
        Ok(value)
    }

    /// Construct a typed empty unavailable inventory.
    pub fn unavailable(
        primary_input: InputIdentity,
        source_format: SourceFormatV1,
        projected_bone_count: u64,
    ) -> Self {
        Self {
            schema: RAW_TRANSFORM_PATH_INVENTORY_V1_ID.to_owned(),
            primary_input,
            source_format,
            projected_bone_count,
            coverage: RawTransformPathCoverageV1::Unavailable(
                RawTransformPathCoverageReasonV1::LoaderEvidenceUnavailable,
            ),
            rows: Vec::new(),
        }
    }

    /// Semantic inventory identifier.
    pub fn contract_id(&self) -> &str {
        &self.schema
    }
    /// Exact primary input identity for this loader pass.
    pub const fn primary_input(&self) -> &InputIdentity {
        &self.primary_input
    }
    /// Exact source format.
    pub const fn source_format(&self) -> SourceFormatV1 {
        self.source_format
    }
    /// Same-load normalized document bone count used to bound correlations.
    pub const fn projected_bone_count(&self) -> u64 {
        self.projected_bone_count
    }
    /// Coverage governing whether a zero match proves absence.
    pub const fn coverage(&self) -> RawTransformPathCoverageV1 {
        self.coverage
    }
    /// Canonical retained source-order rows.
    pub fn rows(&self) -> &[RawTransformPathNodeRowV1] {
        &self.rows
    }

    /// Aggregate UTF-8 bytes retained across source names and exact paths.
    pub fn retained_text_bytes(&self) -> Result<usize, RawTransformPathInventoryErrorV1> {
        self.rows
            .iter()
            .try_fold(0usize, |total, row| {
                total.checked_add(
                    row.source_name.as_ref().map_or(0, String::len)
                        + row
                            .addressable_path
                            .as_ref()
                            .map_or(0, |path| path.as_str().len()),
                )
            })
            .ok_or(RawTransformPathInventoryErrorV1::TooMuchText)
    }

    /// Re-check schema, format, bounds, source order, ancestry, path grammar,
    /// addressability, coverage, and same-load bone correlations.
    ///
    /// # Errors
    ///
    /// Returns a typed contract error for any forged or noncanonical state.
    pub fn validate(&self) -> Result<(), RawTransformPathInventoryErrorV1> {
        if self.schema != RAW_TRANSFORM_PATH_INVENTORY_V1_ID {
            return Err(RawTransformPathInventoryErrorV1::InvalidSchema);
        }
        if self.source_format != SourceFormatV1::Fbx {
            return Err(RawTransformPathInventoryErrorV1::UnsupportedSourceFormat);
        }
        if self.rows.len() > RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS {
            return Err(RawTransformPathInventoryErrorV1::TooManyRows);
        }
        let parent_references = self
            .rows
            .iter()
            .try_fold(0usize, |total, row| {
                total.checked_add(row.parent_chain.len())
            })
            .ok_or(RawTransformPathInventoryErrorV1::TooManyParentReferences)?;
        if parent_references > RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_PARENT_REFERENCES {
            return Err(RawTransformPathInventoryErrorV1::TooManyParentReferences);
        }
        if self.retained_text_bytes()? > RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_TEXT_BYTES {
            return Err(RawTransformPathInventoryErrorV1::TooMuchText);
        }
        if matches!(
            self.coverage,
            RawTransformPathCoverageV1::Unavailable(
                RawTransformPathCoverageReasonV1::LoaderEvidenceUnavailable
            )
        ) {
            return if self.rows.is_empty() {
                Ok(())
            } else {
                Err(RawTransformPathInventoryErrorV1::InvalidCoverage)
            };
        }
        if matches!(self.coverage, RawTransformPathCoverageV1::Unavailable(_)) {
            return Err(RawTransformPathInventoryErrorV1::InvalidCoverage);
        }
        if self.rows.is_empty() {
            return Err(RawTransformPathInventoryErrorV1::InvalidImplicitRoot);
        }

        let mut effective_paths: Vec<Option<String>> = Vec::with_capacity(self.rows.len());
        let mut saw_unrepresentable = false;
        let mut projected = std::collections::BTreeSet::new();
        for (expected, row) in self.rows.iter().enumerate() {
            if row.source_node_index != expected as u64 {
                return Err(RawTransformPathInventoryErrorV1::NonCanonicalSourceNodeIndex);
            }
            let parent = match row.parent_source_node_index {
                Some(parent) if parent < expected as u64 => Some(parent as usize),
                Some(_) => return Err(RawTransformPathInventoryErrorV1::InvalidParent),
                None => None,
            };
            let expected_chain = parent.map_or_else(Vec::new, |parent| {
                let mut chain = self.rows[parent].parent_chain.clone();
                chain.push(parent as u64);
                chain
            });
            if row.parent_chain != expected_chain
                || row.parent_chain.len() > RAW_TRANSFORM_PATH_V1_MAX_DEPTH
            {
                return Err(RawTransformPathInventoryErrorV1::InvalidParent);
            }
            if expected == 0 {
                if parent.is_some()
                    || row.kind != RawTransformPathNodeKindV1::ImplicitUfbxRoot
                    || row.source_name.is_some()
                    || row.source_name_utf8_bytes != 0
                    || row.projected_bone_index.is_some()
                {
                    return Err(RawTransformPathInventoryErrorV1::InvalidImplicitRoot);
                }
            } else if row.kind == RawTransformPathNodeKindV1::ImplicitUfbxRoot {
                return Err(RawTransformPathInventoryErrorV1::InvalidImplicitRoot);
            }
            if let Some(name) = &row.source_name {
                if name.len() as u64 != row.source_name_utf8_bytes
                    || name.len() > RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES
                {
                    return Err(RawTransformPathInventoryErrorV1::InvalidAddressability);
                }
            } else if row.kind.is_matchable()
                && row.source_name_utf8_bytes <= RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES as u64
            {
                // Matchable empty names are retained as Some(""); None means a
                // spelling was redacted only because it exceeded the segment cap.
                return Err(RawTransformPathInventoryErrorV1::InvalidAddressability);
            }
            match row.projected_bone_index {
                Some(index) if index >= self.projected_bone_count => {
                    return Err(RawTransformPathInventoryErrorV1::ProjectedBoneOutOfRange);
                }
                Some(index) if !projected.insert(index) => {
                    return Err(RawTransformPathInventoryErrorV1::DuplicateProjectedBone);
                }
                _ => {}
            }

            let inherited = parent
                .and_then(|parent| effective_paths[parent].as_deref())
                .unwrap_or("");
            let (expected_addressability, effective_path, expected_path) = match row.kind {
                RawTransformPathNodeKindV1::ImplicitUfbxRoot => (
                    RawTransformPathRowAddressabilityV1::ExcludedImplicitRoot,
                    Some(String::new()),
                    None,
                ),
                RawTransformPathNodeKindV1::GeometryTransformHelper
                | RawTransformPathNodeKindV1::ScaleCompensationHelper
                | RawTransformPathNodeKindV1::GeometryAndScaleHelper => (
                    RawTransformPathRowAddressabilityV1::ExcludedSyntheticHelper,
                    parent.and_then(|parent| effective_paths[parent].clone()),
                    None,
                ),
                RawTransformPathNodeKindV1::Source => match row.source_name.as_deref() {
                    Some(name) if validate_segment(name).is_ok() => {
                        if parent.is_some() && effective_paths[parent.unwrap()].is_none() {
                            saw_unrepresentable = true;
                            (
                                RawTransformPathRowAddressabilityV1::UnrepresentableAncestorSegment,
                                None,
                                None,
                            )
                        } else {
                            let path = if inherited.is_empty() {
                                name.to_owned()
                            } else {
                                format!("{inherited}/{name}")
                            };
                            if path.len() > RAW_TRANSFORM_PATH_V1_MAX_PATH_BYTES
                                || path.split('/').count() > RAW_TRANSFORM_PATH_V1_MAX_DEPTH
                            {
                                saw_unrepresentable = true;
                                (RawTransformPathRowAddressabilityV1::PathTooLong, None, None)
                            } else {
                                (
                                    RawTransformPathRowAddressabilityV1::Addressable,
                                    Some(path.clone()),
                                    Some(path),
                                )
                            }
                        }
                    }
                    _ => {
                        saw_unrepresentable = true;
                        (
                            RawTransformPathRowAddressabilityV1::UnrepresentableSourceSegment,
                            None,
                            None,
                        )
                    }
                },
            };
            if row.addressability != expected_addressability
                || row
                    .addressable_path
                    .as_ref()
                    .map(RawTransformPathV1::as_str)
                    != expected_path.as_deref()
            {
                return Err(RawTransformPathInventoryErrorV1::InvalidAddressability);
            }
            effective_paths.push(effective_path);
        }
        match self.coverage {
            RawTransformPathCoverageV1::Complete if saw_unrepresentable => {
                Err(RawTransformPathInventoryErrorV1::InvalidCoverage)
            }
            RawTransformPathCoverageV1::Partial(
                RawTransformPathCoverageReasonV1::UnrepresentableSourceSegment,
            ) if !saw_unrepresentable => Err(RawTransformPathInventoryErrorV1::InvalidCoverage),
            RawTransformPathCoverageV1::Partial(
                RawTransformPathCoverageReasonV1::LoaderEvidenceUnavailable,
            ) => Err(RawTransformPathInventoryErrorV1::InvalidCoverage),
            RawTransformPathCoverageV1::Unavailable(_) => unreachable!(),
            _ => Ok(()),
        }
    }

    /// Resolve one already-validated configured path byte-exactly.
    pub fn resolve(&self, path: &RawTransformPathV1) -> RawTransformPathResolutionV1 {
        let mut matches = self
            .rows
            .iter()
            .filter(|row| {
                row.kind.is_matchable()
                    && row
                        .addressable_path
                        .as_ref()
                        .map(RawTransformPathV1::as_str)
                        == Some(path.as_str())
            })
            .map(|row| RawTransformPathMatchV1 {
                source_node_index: row.source_node_index,
                projected_bone_index: row.projected_bone_index,
                parent_chain: row.parent_chain.clone(),
                path: path.clone(),
            });
        let Some(first) = matches.next() else {
            return if self.coverage.proves_absence() {
                RawTransformPathResolutionV1::NoMatch
            } else {
                RawTransformPathResolutionV1::CoverageIncomplete {
                    coverage: self.coverage,
                }
            };
        };
        let Some(second) = matches.next() else {
            return RawTransformPathResolutionV1::Exact(first);
        };
        let mut all = vec![first, second];
        all.extend(matches);
        RawTransformPathResolutionV1::Ambiguous { matches: all }
    }
}

fn incomplete_addressability(coverage: RawTransformPathCoverageV1) -> RawTransformPathCoverageV1 {
    match coverage {
        RawTransformPathCoverageV1::Complete => RawTransformPathCoverageV1::Partial(
            RawTransformPathCoverageReasonV1::UnrepresentableSourceSegment,
        ),
        other => other,
    }
}

/// One exact original-node match, suitable for rig-role identity comparison.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct RawTransformPathMatchV1 {
    source_node_index: u64,
    projected_bone_index: Option<u64>,
    #[serde(deserialize_with = "deserialize_parent_chain")]
    parent_chain: Vec<u64>,
    path: RawTransformPathV1,
}

fn deserialize_parent_chain<'de, D>(deserializer: D) -> Result<Vec<u64>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let values: CappedSequence<u64> =
        deserialize_capped_sequence(deserializer, RAW_TRANSFORM_PATH_V1_MAX_DEPTH)?;
    if values.overflowed {
        return Err(serde::de::Error::custom(
            "raw transform parent chain exceeded the V1 depth bound",
        ));
    }
    Ok(values.values)
}

fn deserialize_rows<'de, D>(deserializer: D) -> Result<Vec<RawTransformPathNodeRowV1>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    struct RowsVisitor;

    impl<'de> Visitor<'de> for RowsVisitor {
        type Value = Vec<RawTransformPathNodeRowV1>;

        fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
            formatter.write_str("a bounded raw transform-path inventory row sequence")
        }

        fn visit_seq<A>(self, mut sequence: A) -> Result<Self::Value, A::Error>
        where
            A: SeqAccess<'de>,
        {
            let mut rows = Vec::with_capacity(
                sequence
                    .size_hint()
                    .unwrap_or(0)
                    .min(RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS),
            );
            let mut parent_references = 0usize;
            let mut text_bytes = 0usize;
            while rows.len() < RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS {
                let Some(row) = sequence.next_element::<RawTransformPathNodeRowV1>()? else {
                    return Ok(rows);
                };
                parent_references = parent_references
                    .checked_add(row.parent_chain.len())
                    .ok_or_else(|| {
                        A::Error::custom(
                            "raw transform inventory parent-reference count overflowed",
                        )
                    })?;
                if parent_references > RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_PARENT_REFERENCES {
                    return Err(A::Error::custom(
                        "raw transform inventory exceeded its parent-reference bound",
                    ));
                }
                let retained_text = row.source_name.as_ref().map_or(0, String::len)
                    + row
                        .addressable_path
                        .as_ref()
                        .map_or(0, |path| path.as_str().len());
                text_bytes = text_bytes.checked_add(retained_text).ok_or_else(|| {
                    A::Error::custom("raw transform inventory retained-text count overflowed")
                })?;
                if text_bytes > RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_TEXT_BYTES {
                    return Err(A::Error::custom(
                        "raw transform inventory exceeded its text bound",
                    ));
                }
                rows.push(row);
            }
            if sequence.next_element::<IgnoredAny>()?.is_some() {
                return Err(A::Error::custom(
                    "raw transform inventory exceeded the V1 row bound",
                ));
            }
            Ok(rows)
        }
    }

    deserializer.deserialize_seq(RowsVisitor)
}

fn deserialize_schema<'de, D>(deserializer: D) -> Result<String, D::Error>
where
    D: serde::Deserializer<'de>,
{
    deserialize_capped_string(deserializer, RAW_TRANSFORM_PATH_INVENTORY_V1_ID.len())
}

fn deserialize_source_format<'de, D>(deserializer: D) -> Result<SourceFormatV1, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let value = deserialize_capped_string(deserializer, "gltf_json".len())?;
    match value.as_str() {
        "fbx" => Ok(SourceFormatV1::Fbx),
        other => Err(serde::de::Error::custom(format!(
            "unknown V1 source format {other:?}"
        ))),
    }
}

fn deserialize_primary_input<'de, D>(deserializer: D) -> Result<InputIdentity, D::Error>
where
    D: serde::Deserializer<'de>,
{
    #[derive(Deserialize)]
    #[serde(deny_unknown_fields)]
    struct WireIdentity {
        #[serde(deserialize_with = "deserialize_sha256")]
        sha256: String,
        bytes: u64,
    }

    let wire = WireIdentity::deserialize(deserializer)?;
    let bytes = wire.sha256.as_bytes();
    if bytes.len() != 64
        || !bytes
            .iter()
            .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(byte))
    {
        return Err(serde::de::Error::custom(
            "input identity sha256 must be exactly 64 lowercase hexadecimal digits",
        ));
    }
    let mut digest = [0_u8; 32];
    for (index, pair) in bytes.as_chunks::<2>().0.iter().enumerate() {
        let high = hex_nibble(pair[0]).expect("validated hexadecimal");
        let low = hex_nibble(pair[1]).expect("validated hexadecimal");
        digest[index] = (high << 4) | low;
    }
    Ok(InputIdentity::from_sha256_digest(digest, wire.bytes))
}

fn deserialize_sha256<'de, D>(deserializer: D) -> Result<String, D::Error>
where
    D: serde::Deserializer<'de>,
{
    deserialize_capped_string(deserializer, 64)
}

fn hex_nibble(byte: u8) -> Option<u8> {
    match byte {
        b'0'..=b'9' => Some(byte - b'0'),
        b'a'..=b'f' => Some(byte - b'a' + 10),
        _ => None,
    }
}

#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct RawTransformPathInventoryWireV1 {
    #[serde(deserialize_with = "deserialize_schema")]
    schema: String,
    #[serde(deserialize_with = "deserialize_primary_input")]
    primary_input: InputIdentity,
    #[serde(deserialize_with = "deserialize_source_format")]
    source_format: SourceFormatV1,
    projected_bone_count: u64,
    coverage: RawTransformPathCoverageV1,
    #[serde(deserialize_with = "deserialize_rows")]
    rows: Vec<RawTransformPathNodeRowV1>,
}

impl<'de> Deserialize<'de> for RawTransformPathInventoryV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        let wire = RawTransformPathInventoryWireV1::deserialize(deserializer)?;
        let value = Self {
            schema: wire.schema,
            primary_input: wire.primary_input,
            source_format: wire.source_format,
            projected_bone_count: wire.projected_bone_count,
            coverage: wire.coverage,
            rows: wire.rows,
        };
        value.validate().map_err(serde::de::Error::custom)?;
        Ok(value)
    }
}

impl RawTransformPathMatchV1 {
    /// Exact original source node identity.
    pub const fn source_node_index(&self) -> u64 {
        self.source_node_index
    }
    /// Same-load normalized bone identity used for exact rig-role comparison.
    pub const fn projected_bone_index(&self) -> Option<u64> {
        self.projected_bone_index
    }
    /// Root-to-direct-parent original identity chain.
    pub fn parent_chain(&self) -> &[u64] {
        &self.parent_chain
    }
    /// Exact configured/source path which matched.
    pub const fn path(&self) -> &RawTransformPathV1 {
        &self.path
    }
}

/// Byte-exact transform-path resolution result.
#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
#[serde(rename_all = "snake_case")]
pub enum RawTransformPathResolutionV1 {
    /// Exactly one original non-helper source node matched.
    Exact(RawTransformPathMatchV1),
    /// Complete coverage proves no original source node matched.
    NoMatch,
    /// Two or more original non-helper source nodes have the same path.
    Ambiguous {
        /// All bounded matching original identities in source order.
        matches: Vec<RawTransformPathMatchV1>,
    },
    /// Zero retained matches cannot prove absence under incomplete coverage.
    CoverageIncomplete {
        /// The exact inventory/addressability coverage state.
        coverage: RawTransformPathCoverageV1,
    },
}

#[derive(Deserialize)]
#[serde(rename_all = "snake_case")]
enum RawTransformPathResolutionWireV1 {
    Exact(RawTransformPathMatchV1),
    NoMatch,
    Ambiguous {
        #[serde(deserialize_with = "deserialize_matches")]
        matches: Vec<RawTransformPathMatchV1>,
    },
    CoverageIncomplete {
        coverage: RawTransformPathCoverageV1,
    },
}

fn deserialize_matches<'de, D>(deserializer: D) -> Result<Vec<RawTransformPathMatchV1>, D::Error>
where
    D: serde::Deserializer<'de>,
{
    let values =
        deserialize_capped_sequence(deserializer, RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS)?;
    if values.overflowed {
        return Err(serde::de::Error::custom(
            "raw transform ambiguity exceeds the V1 row bound",
        ));
    }
    Ok(values.values)
}

impl<'de> Deserialize<'de> for RawTransformPathResolutionV1 {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        match RawTransformPathResolutionWireV1::deserialize(deserializer)? {
            RawTransformPathResolutionWireV1::Exact(value) => Ok(Self::Exact(value)),
            RawTransformPathResolutionWireV1::NoMatch => Ok(Self::NoMatch),
            RawTransformPathResolutionWireV1::Ambiguous { matches } => {
                Ok(Self::Ambiguous { matches })
            }
            RawTransformPathResolutionWireV1::CoverageIncomplete { coverage } => {
                Ok(Self::CoverageIncomplete { coverage })
            }
        }
    }
}

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

    fn identity() -> InputIdentity {
        InputIdentity::from_bytes(b"raw-transform-path-test")
    }

    fn input<'a>(
        index: u64,
        parent: Option<u64>,
        name: Option<&'a str>,
        kind: RawTransformPathNodeKindV1,
    ) -> RawTransformPathNodeInputV1<'a> {
        RawTransformPathNodeInputV1 {
            source_node_index: index,
            parent_source_node_index: parent,
            source_name: name,
            projected_bone_index: kind.is_matchable().then_some(index),
            kind,
        }
    }

    fn path_with_bytes(bytes: usize) -> String {
        let mut path = String::new();
        while path.len() < bytes {
            if !path.is_empty() {
                path.push('/');
            }
            let remaining = bytes - path.len();
            let segment_bytes = remaining.min(RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES);
            path.push_str(&"a".repeat(segment_bytes));
        }
        path
    }

    fn inventory_with_maximum_source_name() -> RawTransformPathInventoryV1 {
        let name = "a".repeat(RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES);
        RawTransformPathInventoryV1::from_nodes(
            identity(),
            SourceFormatV1::Fbx,
            2,
            [
                input(0, None, None, RawTransformPathNodeKindV1::ImplicitUfbxRoot),
                input(1, Some(0), Some(&name), RawTransformPathNodeKindV1::Source),
            ],
        )
        .unwrap()
    }

    #[test]
    fn grammar_is_closed_unescaped_and_byte_exact() {
        for invalid in [
            "",
            "/Root",
            "Root/",
            "Root//Bone",
            ".",
            "..",
            "A/./B",
            "A\\B",
            "A\nB",
            "A\u{200d}B",
        ] {
            assert!(RawTransformPathV1::parse(invalid).is_err(), "{invalid:?}");
        }
        let path = RawTransformPathV1::parse("Róot/Bone.01").unwrap();
        assert_eq!(path.segments().collect::<Vec<_>>(), ["Róot", "Bone.01"]);
        assert_ne!(path, RawTransformPathV1::parse("róot/Bone.01").unwrap());
    }

    #[test]
    fn resolution_is_byte_exact_without_transforming_source_or_configured_segments() {
        let cases = [
            ("trimming", &[" Root "][..], "Root"),
            ("NFC normalization", &["\u{0065}\u{0301}"][..], "\u{00e9}"),
            ("namespace stripping", &["Armature:Root"][..], "Root"),
            ("prefix matching", &["Rig", "Rooted"][..], "Rig/Root"),
        ];
        for (label, source_segments, configured_path) in cases {
            let mut nodes = vec![input(
                0,
                None,
                None,
                RawTransformPathNodeKindV1::ImplicitUfbxRoot,
            )];
            for (index, segment) in source_segments.iter().enumerate() {
                nodes.push(input(
                    index as u64 + 1,
                    Some(index as u64),
                    Some(segment),
                    RawTransformPathNodeKindV1::Source,
                ));
            }
            let inventory = RawTransformPathInventoryV1::from_nodes(
                identity(),
                SourceFormatV1::Fbx,
                source_segments.len() as u64 + 1,
                nodes,
            )
            .unwrap();
            assert_eq!(
                inventory.resolve(&RawTransformPathV1::parse(configured_path).unwrap()),
                RawTransformPathResolutionV1::NoMatch,
                "{label} must not turn a non-identical path into a match",
            );
        }
    }

    #[test]
    fn path_deserialization_enforces_byte_bounds_before_retention_for_direct_and_escaped_json() {
        let maximum = path_with_bytes(RAW_TRANSFORM_PATH_V1_MAX_PATH_BYTES);
        let direct = serde_json::to_string(&maximum).unwrap();
        assert_eq!(
            serde_json::from_str::<RawTransformPathV1>(&direct)
                .unwrap()
                .as_str(),
            maximum
        );
        let escaped = format!("\"{}\"", maximum.replace('a', "\\u0061"));
        assert_eq!(
            serde_json::from_str::<RawTransformPathV1>(&escaped)
                .unwrap()
                .as_str(),
            maximum
        );

        let oversized = path_with_bytes(RAW_TRANSFORM_PATH_V1_MAX_PATH_BYTES + 1);
        assert!(
            serde_json::from_str::<RawTransformPathV1>(&serde_json::to_string(&oversized).unwrap())
                .is_err()
        );
        let escaped_oversized = format!("\"{}\"", oversized.replace('a', "\\u0061"));
        assert!(serde_json::from_str::<RawTransformPathV1>(&escaped_oversized).is_err());
    }

    #[test]
    fn helper_and_implicit_root_are_retained_but_skipped_for_matching() {
        let inventory = RawTransformPathInventoryV1::from_nodes(
            identity(),
            SourceFormatV1::Fbx,
            4,
            [
                input(0, None, None, RawTransformPathNodeKindV1::ImplicitUfbxRoot),
                input(1, Some(0), Some("Rig"), RawTransformPathNodeKindV1::Source),
                input(
                    2,
                    Some(1),
                    Some("helper"),
                    RawTransformPathNodeKindV1::GeometryTransformHelper,
                ),
                input(3, Some(2), Some("Root"), RawTransformPathNodeKindV1::Source),
            ],
        )
        .unwrap();
        assert_eq!(inventory.coverage(), RawTransformPathCoverageV1::Complete);
        assert_eq!(inventory.rows()[2].parent_chain(), &[0, 1]);
        assert_eq!(inventory.rows()[3].parent_chain(), &[0, 1, 2]);
        let exact = inventory.resolve(&RawTransformPathV1::parse("Rig/Root").unwrap());
        let RawTransformPathResolutionV1::Exact(exact) = exact else {
            panic!("expected exact match");
        };
        assert_eq!(exact.source_node_index(), 3);
        assert_eq!(exact.parent_chain(), &[0, 1, 2]);
        assert_eq!(
            inventory.resolve(&RawTransformPathV1::parse("Rig/helper").unwrap()),
            RawTransformPathResolutionV1::NoMatch
        );
    }

    #[test]
    fn unrepresentable_source_segment_prevents_proven_no_match_without_guessing() {
        let inventory = RawTransformPathInventoryV1::from_nodes(
            identity(),
            SourceFormatV1::Fbx,
            4,
            [
                input(0, None, None, RawTransformPathNodeKindV1::ImplicitUfbxRoot),
                input(
                    1,
                    Some(0),
                    Some("bad/name"),
                    RawTransformPathNodeKindV1::Source,
                ),
                input(2, Some(1), Some("Root"), RawTransformPathNodeKindV1::Source),
                input(3, Some(0), Some("Good"), RawTransformPathNodeKindV1::Source),
            ],
        )
        .unwrap();
        assert!(matches!(
            inventory.coverage(),
            RawTransformPathCoverageV1::Partial(_)
        ));
        assert_eq!(inventory.rows()[1].source_name(), Some("bad/name"));
        assert!(matches!(
            inventory.resolve(&RawTransformPathV1::parse("missing").unwrap()),
            RawTransformPathResolutionV1::CoverageIncomplete { .. }
        ));
        assert!(matches!(
            inventory.resolve(&RawTransformPathV1::parse("Good").unwrap()),
            RawTransformPathResolutionV1::Exact(_)
        ));
    }

    #[test]
    fn every_forbidden_source_segment_class_makes_coverage_incomplete() {
        let oversized = "a".repeat(RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES + 1);
        let cases = vec![
            ("empty", Some(String::new())),
            ("dot", Some(".".to_owned())),
            ("dot-dot", Some("..".to_owned())),
            ("slash", Some("bad/name".to_owned())),
            ("backslash", Some("bad\\name".to_owned())),
            ("control", Some("bad\nname".to_owned())),
            ("format", Some("bad\u{200d}name".to_owned())),
            ("too long", Some(oversized)),
        ];
        for (label, source_name) in cases {
            let inventory = RawTransformPathInventoryV1::from_nodes(
                identity(),
                SourceFormatV1::Fbx,
                2,
                [
                    input(0, None, None, RawTransformPathNodeKindV1::ImplicitUfbxRoot),
                    input(
                        1,
                        Some(0),
                        source_name.as_deref(),
                        RawTransformPathNodeKindV1::Source,
                    ),
                ],
            )
            .unwrap();
            assert_eq!(
                inventory.coverage(),
                RawTransformPathCoverageV1::Partial(
                    RawTransformPathCoverageReasonV1::UnrepresentableSourceSegment
                ),
                "{label} source segment must make absence unprovable",
            );
            assert!(matches!(
                inventory.resolve(&RawTransformPathV1::parse("Missing").unwrap()),
                RawTransformPathResolutionV1::CoverageIncomplete { .. }
            ));
        }
    }

    #[test]
    fn duplicate_non_helper_paths_are_ambiguous() {
        let inventory = RawTransformPathInventoryV1::from_nodes(
            identity(),
            SourceFormatV1::Fbx,
            3,
            [
                input(0, None, None, RawTransformPathNodeKindV1::ImplicitUfbxRoot),
                input(1, Some(0), Some("Root"), RawTransformPathNodeKindV1::Source),
                input(2, Some(0), Some("Root"), RawTransformPathNodeKindV1::Source),
            ],
        )
        .unwrap();
        let RawTransformPathResolutionV1::Ambiguous { matches } =
            inventory.resolve(&RawTransformPathV1::parse("Root").unwrap())
        else {
            panic!("expected ambiguity");
        };
        assert_eq!(
            matches
                .iter()
                .map(RawTransformPathMatchV1::source_node_index)
                .collect::<Vec<_>>(),
            [1, 2]
        );
    }

    #[test]
    fn row_overflow_and_unavailable_inventory_never_prove_no_match() {
        let mut nodes = Vec::with_capacity(RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS + 1);
        nodes.push(input(
            0,
            None,
            None,
            RawTransformPathNodeKindV1::ImplicitUfbxRoot,
        ));
        for index in 1..=RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS as u64 {
            nodes.push(input(
                index,
                Some(0),
                Some("Node"),
                RawTransformPathNodeKindV1::Source,
            ));
        }
        let partial = RawTransformPathInventoryV1::from_nodes(
            identity(),
            SourceFormatV1::Fbx,
            nodes.len() as u64,
            nodes,
        )
        .unwrap();
        assert_eq!(
            partial.rows().len(),
            RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS
        );
        assert_eq!(
            partial.coverage(),
            RawTransformPathCoverageV1::Partial(
                RawTransformPathCoverageReasonV1::ProjectionBudgetExceeded
            )
        );
        assert!(matches!(
            partial.resolve(&RawTransformPathV1::parse("Missing").unwrap()),
            RawTransformPathResolutionV1::CoverageIncomplete { .. }
        ));

        let unavailable =
            RawTransformPathInventoryV1::unavailable(identity(), SourceFormatV1::Fbx, 0);
        unavailable.validate().unwrap();
        assert!(matches!(
            unavailable.resolve(&RawTransformPathV1::parse("Missing").unwrap()),
            RawTransformPathResolutionV1::CoverageIncomplete { .. }
        ));
    }

    #[test]
    fn readback_rejects_forged_schema_parent_path_coverage_and_bone_mapping() {
        let inventory = RawTransformPathInventoryV1::from_nodes(
            identity(),
            SourceFormatV1::Fbx,
            3,
            [
                input(0, None, None, RawTransformPathNodeKindV1::ImplicitUfbxRoot),
                input(1, Some(0), Some("Rig"), RawTransformPathNodeKindV1::Source),
                input(2, Some(1), Some("Root"), RawTransformPathNodeKindV1::Source),
            ],
        )
        .unwrap();
        let canonical = serde_json::to_value(&inventory).unwrap();
        let round_trip: RawTransformPathInventoryV1 =
            serde_json::from_value(canonical.clone()).unwrap();
        assert_eq!(round_trip, inventory);

        let mutations: [fn(&mut serde_json::Value); 5] = [
            |value| value["schema"] = "forged".into(),
            |value| value["rows"][2]["parent_chain"] = serde_json::json!([0]),
            |value| value["rows"][2]["addressable_path"] = "Rig/Other".into(),
            |value| {
                value["coverage"] = serde_json::json!({
                    "state": "partial",
                    "reason": "unrepresentable_source_segment"
                });
            },
            |value| value["rows"][2]["projected_bone_index"] = 1.into(),
        ];
        for mutation in mutations {
            let mut forged = canonical.clone();
            mutation(&mut forged);
            assert!(serde_json::from_value::<RawTransformPathInventoryV1>(forged).is_err());
        }
    }

    #[test]
    fn inventory_deserialization_rejects_oversized_nested_strings() {
        let inventory = inventory_with_maximum_source_name();
        let canonical = serde_json::to_value(&inventory).unwrap();
        let canonical_json = serde_json::to_string(&canonical).unwrap();
        assert_eq!(
            serde_json::from_str::<RawTransformPathInventoryV1>(&canonical_json).unwrap(),
            inventory
        );

        let oversized_name = "a".repeat(RAW_TRANSFORM_PATH_V1_MAX_SEGMENT_BYTES + 1);
        let mut name = canonical.clone();
        name["rows"][1]["source_name"] = oversized_name.clone().into();
        let direct_name = serde_json::to_string(&name).unwrap();
        assert!(serde_json::from_str::<RawTransformPathInventoryV1>(&direct_name).is_err());
        let escaped_name = direct_name.replacen(
            &serde_json::to_string(&oversized_name).unwrap(),
            &format!("\"{}\"", "\\u0061".repeat(oversized_name.len())),
            1,
        );
        assert!(serde_json::from_str::<RawTransformPathInventoryV1>(&escaped_name).is_err());

        let mut schema = canonical.clone();
        schema["schema"] = format!("{RAW_TRANSFORM_PATH_INVENTORY_V1_ID}x").into();
        assert!(
            serde_json::from_str::<RawTransformPathInventoryV1>(
                &serde_json::to_string(&schema).unwrap()
            )
            .is_err()
        );

        let mut primary_sha256 = canonical;
        primary_sha256["primary_input"]["sha256"] = "a".repeat(65).into();
        assert!(
            serde_json::from_str::<RawTransformPathInventoryV1>(
                &serde_json::to_string(&primary_sha256).unwrap()
            )
            .is_err()
        );
    }

    #[test]
    fn ambiguity_deserialization_is_capped_at_the_inventory_row_limit() {
        let inventory = RawTransformPathInventoryV1::from_nodes(
            identity(),
            SourceFormatV1::Fbx,
            3,
            [
                input(0, None, None, RawTransformPathNodeKindV1::ImplicitUfbxRoot),
                input(1, Some(0), Some("Root"), RawTransformPathNodeKindV1::Source),
                input(2, Some(0), Some("Root"), RawTransformPathNodeKindV1::Source),
            ],
        )
        .unwrap();
        let resolution = inventory.resolve(&RawTransformPathV1::parse("Root").unwrap());
        let mut wire = serde_json::to_value(resolution).unwrap();
        let matches = wire["ambiguous"]["matches"].as_array_mut().unwrap();
        let sample = matches[0].clone();
        matches.clear();
        matches.extend(std::iter::repeat_n(
            sample,
            RAW_TRANSFORM_PATH_INVENTORY_V1_MAX_ROWS,
        ));
        let exact_limit = serde_json::to_string(&wire).unwrap();
        assert!(serde_json::from_str::<RawTransformPathResolutionV1>(&exact_limit).is_ok());

        wire["ambiguous"]["matches"]
            .as_array_mut()
            .unwrap()
            .push(serde_json::json!({
                "source_node_index": 1,
                "projected_bone_index": 1,
                "parent_chain": [0],
                "path": "Root"
            }));
        assert!(
            serde_json::from_str::<RawTransformPathResolutionV1>(
                &serde_json::to_string(&wire).unwrap()
            )
            .is_err()
        );
    }
}