renamite-validate 0.2.10

Project validation and diagnostics for renamite.
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
//! Project validation and diagnostics for renamite.
//!
//! Deterministic checks over a [`RenFile`]: document tree integrity,
//! asset references, animation keyframe hygiene, clip/machine sanity, and
//! export-readiness warnings. Use [`validate`] to produce a
//! [`ValidationReport`]; [`ValidationReport::has_errors`] tells you whether the
//! project is safe to save/render/export.

use glam::DVec2;
use renamite_animation::{Angle, Animated, AnimatedTransform, Frame};
use renamite_geometry::VectorPath;
use renamite_io_ren::RenFile;
use renamite_machine::{
    Condition, InputKind, ListenerAction, Machine, MachineId, StateKind, Transition,
};
use renamite_model::{
    Asset, Color, CompId, Document, GradientStops, ModifierKind, Node, NodeId, NodeKind, PropRef,
    ShapeKind, StyleKind, StylePaint, Value,
};
use serde::{Deserialize, Serialize};
use std::collections::HashSet;

#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub enum Severity {
    Error,
    Warning,
    Info,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)]
pub struct Diagnostic {
    pub severity: Severity,
    pub path: String,
    pub message: String,
}

impl Diagnostic {
    pub fn error(path: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            severity: Severity::Error,
            path: path.into(),
            message: message.into(),
        }
    }

    pub fn warning(path: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            severity: Severity::Warning,
            path: path.into(),
            message: message.into(),
        }
    }

    pub fn info(path: impl Into<String>, message: impl Into<String>) -> Self {
        Self {
            severity: Severity::Info,
            path: path.into(),
            message: message.into(),
        }
    }
}

#[derive(Clone, Debug, Default, Serialize, Deserialize)]
pub struct ValidationReport {
    pub diagnostics: Vec<Diagnostic>,
}

impl ValidationReport {
    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|d| d.severity == Severity::Error)
    }

    pub fn error_count(&self) -> usize {
        self.diagnostics
            .iter()
            .filter(|d| d.severity == Severity::Error)
            .count()
    }

    pub fn warning_count(&self) -> usize {
        self.diagnostics
            .iter()
            .filter(|d| d.severity == Severity::Warning)
            .count()
    }

    pub fn push(&mut self, d: Diagnostic) {
        self.diagnostics.push(d);
    }
}

pub fn validate(file: &RenFile) -> ValidationReport {
    let mut v = Validator {
        file,
        report: ValidationReport::default(),
    };
    v.run();
    v.report
}

struct Validator<'a> {
    file: &'a RenFile,
    report: ValidationReport,
}

impl<'a> Validator<'a> {
    fn run(&mut self) {
        self.validate_compositions();
        self.validate_document_tree();
        self.validate_assets();
        self.validate_animations();
        self.validate_scope();
        self.validate_precomps();
        self.validate_clips();
        self.validate_machines();
        self.validate_export_readiness();
    }

    fn err(&mut self, path: impl Into<String>, message: impl Into<String>) {
        self.report.push(Diagnostic::error(path, message));
    }

    fn warn(&mut self, path: impl Into<String>, message: impl Into<String>) {
        self.report.push(Diagnostic::warning(path, message));
    }

    fn validate_compositions(&mut self) {
        let doc = &self.file.document;

        if !doc.compositions.contains_key(doc.main) {
            self.err("document.main", "main composition does not exist");
        }

        for (id, comp) in &doc.compositions {
            if comp.rate.num == 0 || comp.rate.den == 0 {
                self.err(format!("composition/{id:?}/rate"), "invalid frame rate");
            }
            if comp.range.1 <= comp.range.0 {
                self.err(
                    format!("composition/{id:?}/range"),
                    "out frame must be after in frame",
                );
            }
            if comp.size.0 == 0 || comp.size.1 == 0 {
                self.warn(
                    format!("composition/{id:?}/size"),
                    "composition size is zero",
                );
            }
            for (index, child) in comp.children.iter().enumerate() {
                if !doc.nodes.contains_key(*child) {
                    self.err(
                        format!("composition/{id:?}/children/{index}"),
                        "child node does not exist",
                    );
                }
            }
        }
    }

    fn validate_document_tree(&mut self) {
        let doc = &self.file.document;
        let mut seen = HashSet::new();

        for (comp_id, comp) in &doc.compositions {
            for &root in &comp.children {
                self.walk_node_tree(
                    root,
                    format!("composition/{comp_id:?}"),
                    &mut seen,
                    Vec::new(),
                );
            }
        }

        for id in doc.nodes.keys() {
            if !seen.contains(&id) {
                self.warn(
                    format!("node/{id:?}"),
                    "detached arena node will be pruned on save",
                );
            }
        }
    }

    fn walk_node_tree(
        &mut self,
        id: NodeId,
        path: String,
        seen: &mut HashSet<NodeId>,
        mut stack: Vec<NodeId>,
    ) {
        if stack.contains(&id) {
            self.err(format!("{path}/node/{id:?}"), "cycle in node tree");
            return;
        }

        let Some(node) = self.file.document.nodes.get(id) else {
            self.err(path, format!("node {id:?} does not exist"));
            return;
        };

        seen.insert(id);
        stack.push(id);

        for (index, &child) in node.children.iter().enumerate() {
            match self.file.document.nodes.get(child) {
                Some(child_node) => {
                    if child_node.parent != Some(id) {
                        self.err(
                            format!("node/{id:?}/children/{index}"),
                            "child parent pointer does not point back to this node",
                        );
                    }
                    self.walk_node_tree(
                        child,
                        format!("node/{id:?}/children/{index}"),
                        seen,
                        stack.clone(),
                    );
                }
                None => {
                    self.err(
                        format!("node/{id:?}/children/{index}"),
                        "child node does not exist",
                    );
                }
            }
        }
    }

    fn validate_assets(&mut self) {
        let doc = &self.file.document;

        let mut seen = HashSet::new();
        for (i, &id) in doc.asset_order.iter().enumerate() {
            if !doc.assets.contains_key(id) {
                self.err(format!("assets/order/{i}"), "asset id does not exist");
            }
            if !seen.insert(id) {
                self.err(
                    format!("assets/order/{i}"),
                    "duplicate asset id in asset_order",
                );
            }
        }

        for id in doc.assets.keys() {
            if !seen.contains(&id) {
                self.warn(
                    format!("asset/{id:?}"),
                    "asset exists but is not attached in asset_order",
                );
            }
        }

        for (id, node) in &doc.nodes {
            match &node.kind {
                NodeKind::Image(img) => match doc.assets.get(img.asset()) {
                    Some(Asset::Image(img)) => {
                        if img.width == 0 || img.height == 0 {
                            self.err(
                                format!("node/{id:?}/image"),
                                "image dimensions must be nonzero",
                            );
                        }
                        if img.bytes.is_empty() {
                            self.err(format!("node/{id:?}/image"), "image asset has no bytes");
                        }
                    }
                    Some(_) => self.err(
                        format!("node/{id:?}/image"),
                        "referenced asset is not an image",
                    ),
                    None => self.err(format!("node/{id:?}/image"), "image asset is missing"),
                },
                NodeKind::Text(text) => {
                    if let Some(family) = &text.font
                        && family != "default"
                        && doc.font_asset_for_family(family).is_none()
                    {
                        self.warn(
                            format!("node/{id:?}/text/font"),
                            format!(
                                "font family `{family}` not found; bundled default will be used"
                            ),
                        );
                    }
                }
                _ => {}
            }
        }

        for (id, asset) in &doc.assets {
            match asset {
                Asset::Image(img) => {
                    if img.width == 0 || img.height == 0 {
                        self.err(format!("asset/{id:?}"), "image dimensions must be nonzero");
                    }
                    if img.bytes.is_empty() {
                        self.warn(format!("asset/{id:?}"), "image asset has empty bytes");
                    }
                }
                Asset::Font(font) => {
                    if font.bytes.is_empty() {
                        self.err(format!("asset/{id:?}"), "font has no bytes");
                    }
                    if font.family.trim().is_empty() {
                        self.err(format!("asset/{id:?}"), "font family is empty");
                    }
                }
            }
        }

        self.validate_asset_usage(doc);
    }

    fn validate_asset_usage(&mut self, doc: &Document) {
        for (id, asset) in &doc.assets {
            match asset {
                Asset::Image(_) => {
                    let used = doc
                        .nodes
                        .values()
                        .any(|n| matches!(&n.kind, NodeKind::Image(img) if img.asset() == id));
                    if !used {
                        self.warn(
                            format!("asset/{id:?}"),
                            "image asset is not used by any image layer",
                        );
                    }
                }
                Asset::Font(font) => {
                    let used = doc.nodes.values().any(|n| {
                        matches!(&n.kind, NodeKind::Text(t) if t.font.as_deref() == Some(font.family.as_str()))
                    });
                    if !used {
                        self.warn(
                            format!("asset/{id:?}"),
                            "font asset is not used by any text node",
                        );
                    }
                }
            }
        }
    }

    fn validate_animations(&mut self) {
        let doc = &self.file.document;
        for (id, node) in &doc.nodes {
            self.validate_node_animations(id, node);
        }
    }

    fn validate_node_animations(&mut self, id: NodeId, node: &Node) {
        let base = format!("node/{id:?}");
        self.check_transform(&format!("{base}/transform"), &node.transform);
        self.check_animated(&format!("{base}/opacity"), &node.opacity, finite_f64);

        if node.transform.scale.base == DVec2::ZERO {
            self.warn(format!("{base}/transform/scale"), "transform scale is zero");
        }

        match &node.kind {
            NodeKind::Shape(shape) => self.validate_shape_animations(id, shape),
            NodeKind::Style(style) => self.validate_style_animations(id, style),
            NodeKind::Modifier(modifier) => self.validate_modifier_animations(id, modifier),
            NodeKind::Text(text) => {
                self.check_animated(&format!("{base}/text/size"), &text.size, finite_f64);
                self.check_animated(&format!("{base}/text/tracking"), &text.tracking, finite_f64);
                self.check_animated(&format!("{base}/text/leading"), &text.leading, finite_f64);
            }
            NodeKind::Layer(props) => {
                if !props.time_stretch.is_finite() || props.time_stretch <= 0.0 {
                    self.err(
                        format!("{base}/layer/time_stretch"),
                        "time stretch must be positive and finite",
                    );
                }
                if props.out_frame <= props.in_frame {
                    self.warn(
                        format!("{base}/layer/range"),
                        "layer out frame must be after in frame",
                    );
                }
            }
            NodeKind::Mask(mask) => {
                self.validate_shape_animations(id, &mask.shape);
                if shape_kind_is_empty(&mask.shape) {
                    self.warn(format!("{base}/mask"), "mask has no geometry");
                }
            }
            NodeKind::Image(img) => {
                self.check_animated(&format!("{base}/image/tint"), img.tint(), finite_color);
                let c = img.crop();
                if !c.x.is_finite() || !c.y.is_finite() || !c.z.is_finite() || !c.w.is_finite() {
                    self.err(format!("{base}/image/crop"), "crop is not finite");
                } else {
                    if !(0.0..=1.0).contains(&c.x)
                        || !(0.0..=1.0).contains(&c.y)
                        || c.z <= 0.0
                        || c.w <= 0.0
                        || c.z > 1.0
                        || c.w > 1.0
                    {
                        self.err(
                            format!("{base}/image/crop"),
                            "crop must be x,y in [0,1] and w,h in (0,1]",
                        );
                    }
                    if c.x + c.z > 1.0 + 1e-9 || c.y + c.w > 1.0 + 1e-9 {
                        self.err(
                            format!("{base}/image/crop"),
                            "crop rect must be inside [0,1] image bounds (x+w<=1, y+h<=1)",
                        );
                    }
                }
            }
            NodeKind::Group | NodeKind::Precomp { .. } => {}
        }
    }

    fn validate_shape_animations(&mut self, id: NodeId, shape: &ShapeKind) {
        let base = format!("node/{id:?}/shape");
        match shape {
            ShapeKind::Path(path) => {
                self.check_animated(&format!("{base}/path"), path, finite_path);
            }
            ShapeKind::Rect { pos, size, rounded } => {
                self.check_animated(&format!("{base}/pos"), pos, finite_vec2);
                self.check_animated(&format!("{base}/size"), size, finite_vec2);
                self.check_animated(&format!("{base}/rounded"), rounded, finite_f64);
            }
            ShapeKind::Ellipse { pos, size } => {
                self.check_animated(&format!("{base}/pos"), pos, finite_vec2);
                self.check_animated(&format!("{base}/size"), size, finite_vec2);
            }
            ShapeKind::Star {
                pos,
                points,
                inner_r,
                outer_r,
                roundness,
                ..
            } => {
                self.check_animated(&format!("{base}/pos"), pos, finite_vec2);
                self.check_animated(&format!("{base}/points"), points, finite_f64);
                self.check_animated(&format!("{base}/inner_r"), inner_r, finite_f64);
                self.check_animated(&format!("{base}/outer_r"), outer_r, finite_f64);
                self.check_animated(&format!("{base}/roundness"), roundness, finite_f64);
            }
            ShapeKind::Polygon {
                pos,
                points,
                outer_r,
                roundness,
            } => {
                self.check_animated(&format!("{base}/pos"), pos, finite_vec2);
                self.check_animated(&format!("{base}/points"), points, finite_f64);
                self.check_animated(&format!("{base}/outer_r"), outer_r, finite_f64);
                self.check_animated(&format!("{base}/roundness"), roundness, finite_f64);
            }
            ShapeKind::CompoundPath(compound) => {
                for (i, contour) in compound.contours.iter().enumerate() {
                    self.check_animated(&format!("{base}/contour/{i}"), contour, finite_path);
                }
            }
        }
    }

    fn validate_style_animations(&mut self, id: NodeId, style: &StyleKind) {
        let base = format!("node/{id:?}/style");
        match style {
            StyleKind::Fill { paint, .. } => {
                self.validate_paint(&format!("{base}/paint"), paint);
            }
            StyleKind::Stroke {
                paint, width, dash, ..
            } => {
                self.validate_paint(&format!("{base}/paint"), paint);
                self.check_animated(&format!("{base}/width"), width, finite_f64);
                if let Some(dash) = dash {
                    for (i, d) in dash.dashes.iter().enumerate() {
                        self.check_animated(&format!("{base}/dash/{i}"), d, finite_f64);
                    }
                    self.check_animated(&format!("{base}/dash/offset"), &dash.offset, finite_f64);
                }
            }
        }
    }

    fn validate_paint(&mut self, path: &str, paint: &StylePaint) {
        match paint {
            StylePaint::Solid { color } => self.check_animated(path, color, finite_color),
            StylePaint::Gradient(gradient) => {
                self.check_animated(&format!("{path}/start"), &gradient.start, finite_vec2);
                self.check_animated(&format!("{path}/end"), &gradient.end, finite_vec2);
                self.check_animated(&format!("{path}/stops"), &gradient.stops, finite_stops);
            }
        }
    }

    fn validate_modifier_animations(&mut self, id: NodeId, modifier: &ModifierKind) {
        let base = format!("node/{id:?}/modifier");
        match modifier {
            ModifierKind::TrimPath {
                start, end, offset, ..
            } => {
                self.check_animated(&format!("{base}/start"), start, finite_f64);
                self.check_animated(&format!("{base}/end"), end, finite_f64);
                self.check_animated(&format!("{base}/offset"), offset, finite_f64);
            }
            ModifierKind::Repeater {
                copies,
                offset,
                transform,
                start_opacity,
                end_opacity,
            } => {
                self.check_animated(&format!("{base}/copies"), copies, finite_f64);
                self.check_animated(&format!("{base}/offset"), offset, finite_f64);
                self.check_animated(&format!("{base}/start_opacity"), start_opacity, finite_f64);
                self.check_animated(&format!("{base}/end_opacity"), end_opacity, finite_f64);
                self.check_transform(&format!("{base}/transform"), transform);
            }
            ModifierKind::RoundCorners { radius } => {
                self.check_animated(&format!("{base}/radius"), radius, finite_f64);
            }
            ModifierKind::OffsetPath { amount } => {
                self.check_animated(&format!("{base}/amount"), amount, finite_f64);
            }
            ModifierKind::ZigZag {
                amplitude,
                frequency,
                ..
            } => {
                self.check_animated(&format!("{base}/amplitude"), amplitude, finite_f64);
                self.check_animated(&format!("{base}/frequency"), frequency, finite_f64);
            }
            ModifierKind::PuckerBloat { amount } => {
                self.check_animated(&format!("{base}/amount"), amount, finite_f64);
            }
        }
    }

    fn check_transform(&mut self, path: &str, transform: &AnimatedTransform) {
        self.check_animated(&format!("{path}/anchor"), &transform.anchor, finite_vec2);
        self.check_animated(
            &format!("{path}/position"),
            &transform.position,
            finite_vec2,
        );
        self.check_animated(&format!("{path}/scale"), &transform.scale, finite_vec2);
        self.check_animated(
            &format!("{path}/rotation"),
            &transform.rotation,
            finite_angle,
        );
        self.check_animated(&format!("{path}/skew"), &transform.skew, finite_f64);
        self.check_animated(
            &format!("{path}/skew_axis"),
            &transform.skew_axis,
            finite_f64,
        );
    }

    fn check_animated<T>(
        &mut self,
        path: &str,
        animated: &Animated<T>,
        check_value: impl Fn(&T) -> bool,
    ) {
        if !check_value(&animated.base) {
            self.err(format!("{path}/base"), "value is not finite");
        }
        let mut prev: Option<Frame> = None;
        for (i, key) in animated.keyframes.iter().enumerate() {
            if let Some(p) = prev
                && key.frame <= p
            {
                self.err(
                    format!("{path}/key/{i}"),
                    format!(
                        "keyframes not strictly increasing (duplicate or out of order at frame {})",
                        key.frame.0
                    ),
                );
            }
            if !check_value(&key.value) {
                self.err(format!("{path}/key/{i}"), "keyframe value is not finite");
            }
            if !key.ease_out.x.is_finite()
                || !key.ease_out.y.is_finite()
                || !key.ease_in.x.is_finite()
                || !key.ease_in.y.is_finite()
            {
                self.err(
                    format!("{path}/key/{i}/easing"),
                    "easing handle is not finite",
                );
            }
            prev = Some(key.frame);
        }
    }

    /// Style/modifier scoping mirrors group evaluation: a style paints every
    /// shape path accumulated in its group, and a modifier only affects shapes
    /// seen before it. Warn when either would be a no-op.
    fn validate_scope(&mut self) {
        let doc = &self.file.document;
        let mut visited = HashSet::new();
        for (comp_id, comp) in &doc.compositions {
            self.scope_group(
                comp.children.to_vec(),
                format!("composition/{comp_id:?}"),
                &mut visited,
            );
        }
    }

    fn scope_group(&mut self, children: Vec<NodeId>, path: String, visited: &mut HashSet<NodeId>) {
        let doc = &self.file.document;
        let mut has_shape = false;

        for (index, &id) in children.iter().enumerate() {
            let Some(node) = doc.nodes.get(id) else {
                continue;
            };
            match &node.kind {
                NodeKind::Shape(_) | NodeKind::Text(_) => has_shape = true,
                NodeKind::Modifier(_) if !has_shape => {
                    self.warn(
                        format!("{path}/children/{index}"),
                        "modifier appears before any shape in scope and will have no effect",
                    );
                }
                _ => {}
            }
        }

        if !has_shape {
            for (index, &id) in children.iter().enumerate() {
                let Some(node) = doc.nodes.get(id) else {
                    continue;
                };
                if matches!(node.kind, NodeKind::Style(_)) {
                    self.warn(
                        format!("{path}/children/{index}"),
                        "style node is not paired with any shape in scope",
                    );
                }
            }
        }

        for &id in &children {
            let Some(node) = doc.nodes.get(id) else {
                continue;
            };
            if matches!(node.kind, NodeKind::Group | NodeKind::Layer(_)) && visited.insert(id) {
                self.scope_group(
                    node.children.clone(),
                    format!("{path}/node/{id:?}"),
                    visited,
                );
            }
        }
    }

    fn validate_precomps(&mut self) {
        let doc = &self.file.document;

        for (id, node) in &doc.nodes {
            if let NodeKind::Precomp { comp, time_map } = &node.kind {
                if !doc.compositions.contains_key(*comp) {
                    self.err(
                        format!("node/{id:?}/precomp"),
                        "referenced composition does not exist",
                    );
                }
                if !time_map.stretch.is_finite() || time_map.stretch.abs() < 1e-6 {
                    self.err(
                        format!("node/{id:?}/precomp/stretch"),
                        "invalid time stretch",
                    );
                }
            }
        }

        let mut on_stack = HashSet::new();
        let mut visited = HashSet::new();
        for comp in doc.compositions.keys() {
            self.walk_precomp(comp, &mut on_stack, &mut visited);
        }
    }

    fn walk_precomp(
        &mut self,
        comp: CompId,
        on_stack: &mut HashSet<CompId>,
        visited: &mut HashSet<CompId>,
    ) {
        if on_stack.contains(&comp) {
            self.err(
                format!("precomp/{comp:?}"),
                "composition is reachable from itself through precomps (cycle)",
            );
            return;
        }
        if !visited.insert(comp) {
            return;
        }
        on_stack.insert(comp);
        if let Some(c) = self.file.document.compositions.get(comp) {
            let mut stack: Vec<NodeId> = c.children.clone();
            let mut seen_nodes = HashSet::new();
            while let Some(nid) = stack.pop() {
                if !seen_nodes.insert(nid) {
                    continue;
                }
                let Some(node) = self.file.document.nodes.get(nid) else {
                    continue;
                };
                if let NodeKind::Precomp { comp: target, .. } = &node.kind {
                    self.walk_precomp(*target, on_stack, visited);
                }
                if matches!(node.kind, NodeKind::Group | NodeKind::Layer(_)) {
                    stack.extend(node.children.iter().copied());
                }
            }
        }
        on_stack.remove(&comp);
    }

    fn validate_clips(&mut self) {
        let doc = &self.file.document;

        let mut seen = HashSet::new();
        for (i, &id) in self.file.clip_order.iter().enumerate() {
            if !self.file.clips.contains_key(id) {
                self.err(format!("clips/order/{i}"), "clip id does not exist");
            }
            if !seen.insert(id) {
                self.err(
                    format!("clips/order/{i}"),
                    "duplicate clip id in clip_order",
                );
            }
        }

        for (clip_id, clip) in &self.file.clips {
            if clip.range.1 <= clip.range.0 {
                self.err(format!("clip/{clip_id:?}/range"), "invalid clip range");
            }

            for (track_index, track) in clip.tracks.iter().enumerate() {
                let track_path = format!("clip/{clip_id:?}/track/{track_index}");
                let prop = match doc.nodes.get(track.node) {
                    Some(node) => match node.prop_ref(&track.prop) {
                        Some(prop) => prop,
                        None => {
                            self.err(
                                format!("{track_path}/prop"),
                                "track references missing or incompatible property",
                            );
                            continue;
                        }
                    },
                    None => {
                        self.err(
                            format!("{track_path}/node"),
                            "track references missing node",
                        );
                        continue;
                    }
                };

                let mut prev: Option<Frame> = None;
                for (key_index, key) in track.keys.iter().enumerate() {
                    if let Some(p) = prev
                        && key.frame <= p
                    {
                        self.err(
                            format!("{track_path}/key/{key_index}"),
                            "clip keyframes not strictly increasing (duplicate or out of order)",
                        );
                    }
                    if !key_value_matches_prop(&key.value, &prop) {
                        self.err(
                            format!("{track_path}/key/{key_index}/value"),
                            "keyframe value type does not match property",
                        );
                    }
                    prev = Some(key.frame);
                }
            }
        }
    }

    fn validate_machines(&mut self) {
        let doc = &self.file.document;

        if let Some(start) = self.file.start_machine {
            if !self.file.machines.contains_key(start) {
                self.err("start_machine", "start machine does not exist");
            }
            if !self.file.machine_order.contains(&start) {
                self.warn(
                    "start_machine",
                    "start machine exists but is detached from machine_order",
                );
            }
        }

        let mut seen = HashSet::new();
        for (i, &id) in self.file.machine_order.iter().enumerate() {
            if !self.file.machines.contains_key(id) {
                self.err(format!("machines/order/{i}"), "machine id does not exist");
            }
            if !seen.insert(id) {
                self.err(
                    format!("machines/order/{i}"),
                    "duplicate machine id in machine_order",
                );
            }
        }

        for (machine_id, machine) in &self.file.machines {
            for (layer_index, layer) in machine.layers.iter().enumerate() {
                if layer.states.is_empty() {
                    self.err(
                        format!("machine/{machine_id:?}/layer/{layer_index}"),
                        "layer has no states",
                    );
                    continue;
                }

                if layer.entry >= layer.states.len() {
                    self.err(
                        format!("machine/{machine_id:?}/layer/{layer_index}/entry"),
                        "entry state index is out of range",
                    );
                }

                for (state_index, state) in layer.states.iter().enumerate() {
                    match &state.kind {
                        StateKind::Clip { clip, speed, .. } => {
                            if !self.file.clips.contains_key(*clip) {
                                self.err(
                                    format!("machine/{machine_id:?}/layer/{layer_index}/state/{state_index}/clip"),
                                    "state references missing clip",
                                );
                            }
                            if !speed.is_finite() || *speed < 0.0 {
                                self.err(
                                    format!("machine/{machine_id:?}/layer/{layer_index}/state/{state_index}/speed"),
                                    "clip state speed must be non-negative and finite",
                                );
                            }
                        }
                        StateKind::Blend1D { input, children } => {
                            let base = format!(
                                "machine/{machine_id:?}/layer/{layer_index}/state/{state_index}/blend"
                            );
                            match machine.inputs.get(*input) {
                                Some(input_def) => {
                                    if !matches!(input_def.kind, InputKind::Number { .. }) {
                                        self.err(
                                            format!("{base}/input"),
                                            "Blend1D input must be a number input",
                                        );
                                    }
                                }
                                None => self.err(
                                    format!("{base}/input"),
                                    "Blend1D input index is out of range",
                                ),
                            }
                            if children.is_empty() {
                                self.err(format!("{base}/children"), "Blend1D has no children");
                            }
                            let mut prev: Option<f64> = None;
                            for (child_index, child) in children.iter().enumerate() {
                                if !self.file.clips.contains_key(child.clip) {
                                    self.err(
                                        format!("{base}/child/{child_index}"),
                                        "blend child references missing clip",
                                    );
                                }
                                if !child.threshold.is_finite() {
                                    self.err(
                                        format!("{base}/child/{child_index}/threshold"),
                                        "blend threshold must be finite",
                                    );
                                }
                                if let Some(p) = prev
                                    && child.threshold <= p
                                {
                                    self.warn(
                                        format!("{base}/child/{child_index}/threshold"),
                                        "blend thresholds are not strictly increasing",
                                    );
                                }
                                prev = Some(child.threshold);
                            }
                        }
                        StateKind::Empty => {}
                    }

                    self.validate_transitions(
                        machine_id,
                        machine,
                        layer_index,
                        Some(state_index),
                        &state.transitions,
                    );
                }

                self.validate_transitions(
                    machine_id,
                    machine,
                    layer_index,
                    None,
                    &layer.any_transitions,
                );
            }

            for (listener_index, listener) in machine.listeners.iter().enumerate() {
                if !doc.nodes.contains_key(listener.node) {
                    self.err(
                        format!("machine/{machine_id:?}/listener/{listener_index}/node"),
                        "listener references missing node",
                    );
                }

                let input = listener_action_input(&listener.action);
                let base = format!("machine/{machine_id:?}/listener/{listener_index}");
                match machine.inputs.get(input) {
                    Some(input_def) => {
                        if !listener_matches_input(&listener.action, input_def.kind) {
                            self.err(
                                format!("{base}/input"),
                                "listener action type does not match input type",
                            );
                        }
                    }
                    None => self.err(format!("{base}/input"), "listener references missing input"),
                }
            }
        }
    }

    #[allow(clippy::too_many_arguments)]
    fn validate_transitions(
        &mut self,
        machine_id: MachineId,
        machine: &Machine,
        layer_index: usize,
        state_index: Option<usize>,
        transitions: &[Transition],
    ) {
        let Some(layer) = machine.layers.get(layer_index) else {
            return;
        };

        for (transition_index, transition) in transitions.iter().enumerate() {
            let base = match state_index {
                Some(s) => format!(
                    "machine/{machine_id:?}/layer/{layer_index}/state/{s}/transition/{transition_index}"
                ),
                None => format!(
                    "machine/{machine_id:?}/layer/{layer_index}/any_transition/{transition_index}"
                ),
            };

            if transition.to >= layer.states.len() {
                self.err(&base, "transition target state is out of range");
            }
            if !transition.duration.is_finite() || transition.duration < 0.0 {
                self.err(&base, "transition duration must be non-negative and finite");
            }
            if let Some(exit_time) = transition.exit_time
                && (!exit_time.is_finite() || !(0.0..=1.0).contains(&exit_time))
            {
                self.err(&base, "transition exit_time must be in [0, 1]");
            }

            for (condition_index, condition) in transition.conditions.iter().enumerate() {
                let input = condition_input(condition);
                let condition_path = format!("{base}/condition/{condition_index}");
                match machine.inputs.get(input) {
                    Some(input_def) => {
                        if !condition_matches_input(condition, input_def.kind) {
                            self.err(&condition_path, "condition type does not match input type");
                        }
                    }
                    None => self.err(&condition_path, "condition references missing input"),
                }
            }
        }
    }

    fn validate_export_readiness(&mut self) {
        let doc = &self.file.document;

        // Nodes attached directly to a composition are Lottie layers; anything
        // nested inside a group/layer is a shape item and only some kinds make
        // it through that path.
        let direct: HashSet<NodeId> = doc
            .compositions
            .values()
            .flat_map(|c| c.children.iter().copied())
            .collect();

        for (id, node) in &doc.nodes {
            match &node.kind {
                NodeKind::Text(_) => {
                    self.warn(
                        format!("node/{id:?}/text"),
                        "Lottie export bakes text to vector outlines",
                    );
                }
                NodeKind::Mask(_) => {
                    self.warn(
                        format!("node/{id:?}/mask"),
                        "Lottie mask export is best-effort and may differ from Renamite clip-stack semantics",
                    );
                }
                NodeKind::Image(img) => {
                    if doc.image_asset(img.asset()).is_none() {
                        self.err(
                            format!("node/{id:?}/image"),
                            "image layer references missing image asset",
                        );
                    }
                    if !direct.contains(&id) {
                        self.warn(
                            format!("node/{id:?}/image"),
                            "nested image layer is skipped by Lottie export",
                        );
                    }
                }
                NodeKind::Precomp { .. } if !direct.contains(&id) => {
                    self.warn(
                        format!("node/{id:?}/precomp"),
                        "nested precomp is skipped by Lottie export",
                    );
                }
                _ => {}
            }
        }
    }
}

fn condition_input(condition: &Condition) -> usize {
    match condition {
        Condition::BoolIs { input, .. }
        | Condition::NumberCmp { input, .. }
        | Condition::Triggered { input } => *input,
    }
}

fn condition_matches_input(condition: &Condition, input: InputKind) -> bool {
    matches!(
        (condition, input),
        (Condition::BoolIs { .. }, InputKind::Bool { .. })
            | (Condition::NumberCmp { .. }, InputKind::Number { .. })
            | (Condition::Triggered { .. }, InputKind::Trigger)
    )
}

fn listener_action_input(action: &ListenerAction) -> usize {
    match action {
        ListenerAction::SetBool { input, .. }
        | ListenerAction::ToggleBool { input }
        | ListenerAction::SetNumber { input, .. }
        | ListenerAction::FireTrigger { input } => *input,
    }
}

fn listener_matches_input(action: &ListenerAction, input: InputKind) -> bool {
    matches!(
        (action, input),
        (ListenerAction::SetBool { .. }, InputKind::Bool { .. })
            | (ListenerAction::ToggleBool { .. }, InputKind::Bool { .. })
            | (ListenerAction::SetNumber { .. }, InputKind::Number { .. })
            | (ListenerAction::FireTrigger { .. }, InputKind::Trigger)
    )
}

fn key_value_matches_prop(value: &Value, prop: &PropRef) -> bool {
    matches!(
        (value, prop),
        (Value::F64(_), PropRef::F64(_))
            | (Value::DVec2(_), PropRef::Vec2(_))
            | (Value::Angle(_), PropRef::Angle(_))
            | (Value::Color(_), PropRef::Color(_))
            | (Value::Path(_), PropRef::Path(_))
            | (Value::Stops(_), PropRef::Stops(_))
    )
}

fn finite_f64(value: &f64) -> bool {
    value.is_finite()
}

fn finite_vec2(value: &DVec2) -> bool {
    value.is_finite()
}

fn finite_angle(value: &Angle) -> bool {
    value.0.is_finite()
}

fn finite_color(color: &Color) -> bool {
    color.r.is_finite() && color.g.is_finite() && color.b.is_finite() && color.a.is_finite()
}

fn finite_stops(stops: &GradientStops) -> bool {
    stops
        .0
        .iter()
        .all(|s| s.offset.is_finite() && finite_color(&s.color))
}

fn finite_path(path: &VectorPath) -> bool {
    path.anchors
        .iter()
        .all(|a| a.pos.is_finite() && a.tan_in.is_finite() && a.tan_out.is_finite())
}

/// Base-value heuristic for "this shape has no geometry": empty path, zero-size
/// rect/ellipse, or non-positive star/polygon radius.
fn shape_kind_is_empty(shape: &ShapeKind) -> bool {
    match shape {
        ShapeKind::Path(path) => path.base.anchors.is_empty(),
        ShapeKind::CompoundPath(compound) => compound.contours.is_empty(),
        ShapeKind::Rect { size, .. } | ShapeKind::Ellipse { size, .. } => {
            size.base.x == 0.0 && size.base.y == 0.0
        }
        ShapeKind::Star { outer_r, .. } | ShapeKind::Polygon { outer_r, .. } => outer_r.base <= 0.0,
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use renamite_animation::{EasingHandle, Interpolation, Keyframe};
    use renamite_model::{AssetId, Parent, TextAlign, TextNode};

    fn file(name: &str) -> RenFile {
        RenFile::new(Document::empty(), name)
    }

    #[test]
    fn empty_project_is_valid() {
        let report = validate(&file("empty"));
        assert_eq!(report.error_count(), 0);
        assert_eq!(report.warning_count(), 0);
    }

    #[test]
    fn missing_image_asset_is_error() {
        let mut file = file("bad");
        let fake = AssetId::from(slotmap::KeyData::from_ffi(42));
        let node = file.document.create_node(Node::new(
            "img",
            NodeKind::Image(renamite_model::ImageNode::new(fake)),
        ));
        file.document
            .attach(node, Parent::Comp(file.document.main), 0)
            .unwrap();

        let report = validate(&file);
        assert!(report.has_errors());
        assert!(
            report
                .diagnostics
                .iter()
                .any(|d| d.message.contains("image asset"))
        );
    }

    #[test]
    fn missing_font_family_is_warning() {
        let mut file = file("font");
        let node = file.document.create_node(Node::new(
            "t",
            NodeKind::Text(TextNode {
                text: String::new(),
                size: Animated::new(48.0),
                align: TextAlign::Left,
                font: Some("Missing".into()),
                tracking: Animated::new(0.0),
                leading: Animated::new(0.0),
            }),
        ));
        file.document
            .attach(node, Parent::Comp(file.document.main), 0)
            .unwrap();

        let report = validate(&file);
        assert_eq!(report.error_count(), 0);
        assert!(report.warning_count() > 0);
        assert!(
            report
                .diagnostics
                .iter()
                .any(|d| d.message.contains("font family"))
        );
    }

    #[test]
    fn duplicate_animated_keyframes_are_error() {
        use renamite_model::NodeKind;
        let mut file = file("keys");
        let mut node = Node::new("n", NodeKind::Group);
        node.opacity = Animated {
            base: 1.0,
            keyframes: vec![
                Keyframe {
                    frame: Frame(0),
                    value: 1.0,
                    interpolation: Interpolation::Linear,
                    ease_out: EasingHandle::LINEAR_OUT,
                    ease_in: EasingHandle::LINEAR_IN,
                },
                Keyframe {
                    frame: Frame(0),
                    value: 0.5,
                    interpolation: Interpolation::Linear,
                    ease_out: EasingHandle::LINEAR_OUT,
                    ease_in: EasingHandle::LINEAR_IN,
                },
            ],
        };
        let id = file.document.create_node(node);
        file.document
            .attach(id, Parent::Comp(file.document.main), 0)
            .unwrap();

        let report = validate(&file);
        assert!(report.has_errors());
        assert!(
            report
                .diagnostics
                .iter()
                .any(|d| d.message.contains("strictly increasing"))
        );
    }

    #[test]
    fn machine_bad_transition_is_error() {
        use renamite_machine::{Machine, MachineLayer, State};
        let mut file = file("machine");
        file.machines.insert(Machine {
            name: "m".into(),
            inputs: vec![],
            layers: vec![MachineLayer {
                name: "base".into(),
                entry: 0,
                any_transitions: vec![],
                states: vec![State {
                    name: "s".into(),
                    kind: StateKind::Empty,
                    transitions: vec![Transition {
                        to: 99,
                        duration: 0.0,
                        exit_time: None,
                        conditions: vec![],
                    }],
                    graph_pos: None,
                }],
            }],
            listeners: vec![],
        });

        let report = validate(&file);
        assert!(report.has_errors());
        assert!(
            report
                .diagnostics
                .iter()
                .any(|d| d.path.contains("transition"))
        );
    }

    #[test]
    fn empty_blend1d_children_are_error() {
        use renamite_machine::{Machine, MachineLayer, State};
        let mut file = file("blend");
        file.machines.insert(Machine {
            name: "m".into(),
            inputs: vec![renamite_machine::InputDef {
                name: "n".into(),
                kind: InputKind::Number { default: 0.0 },
            }],
            layers: vec![MachineLayer {
                name: "base".into(),
                entry: 0,
                any_transitions: vec![],
                states: vec![State {
                    name: "s".into(),
                    kind: StateKind::Blend1D {
                        input: 0,
                        children: vec![],
                    },
                    transitions: vec![],
                    graph_pos: None,
                }],
            }],
            listeners: vec![],
        });

        let report = validate(&file);
        assert!(report.has_errors());
        assert!(
            report
                .diagnostics
                .iter()
                .any(|d| d.message.contains("Blend1D has no children"))
        );
    }

    #[test]
    fn style_without_shape_in_scope_warns() {
        use renamite_model::{FillRule, StylePaint};
        let mut file = file("style");
        let fill = file.document.create_node(Node::new(
            "Fill",
            NodeKind::Style(StyleKind::Fill {
                paint: StylePaint::solid(Color::WHITE),
                rule: FillRule::NonZero,
            }),
        ));
        file.document
            .attach(fill, Parent::Comp(file.document.main), 0)
            .unwrap();

        let report = validate(&file);
        assert_eq!(report.error_count(), 0);
        assert!(
            report
                .diagnostics
                .iter()
                .any(|d| d.message.contains("not paired with any shape"))
        );
    }

    #[test]
    fn precomp_cycle_is_error() {
        let mut file = file("cycle");
        let comp_a = file.document.main;
        let comp_b = file
            .document
            .compositions
            .insert(renamite_model::Composition {
                name: "B".into(),
                size: (512, 512),
                rate: renamite_animation::FrameRate { num: 60, den: 1 },
                range: (Frame(0), Frame(60)),
                children: vec![],
            });
        let node_a = file.document.create_node(Node::new(
            "to B",
            NodeKind::Precomp {
                comp: comp_b,
                time_map: renamite_model::TimeMap {
                    offset: Frame(0),
                    stretch: 1.0,
                },
            },
        ));
        file.document
            .attach(node_a, Parent::Comp(comp_a), 0)
            .unwrap();
        let node_b = file.document.create_node(Node::new(
            "to A",
            NodeKind::Precomp {
                comp: comp_a,
                time_map: renamite_model::TimeMap {
                    offset: Frame(0),
                    stretch: 1.0,
                },
            },
        ));
        file.document
            .attach(node_b, Parent::Comp(comp_b), 0)
            .unwrap();

        let report = validate(&file);
        assert!(report.has_errors());
        assert!(
            report
                .diagnostics
                .iter()
                .any(|d| d.message.contains("cycle"))
        );
    }

    #[test]
    fn clip_track_missing_node_is_error() {
        use renamite_machine::Clip;
        let mut file = file("clip");
        let missing = NodeId::from(slotmap::KeyData::from_ffi(7));
        file.clips.insert(Clip {
            name: "c".into(),
            range: (Frame(0), Frame(10)),
            tracks: vec![renamite_machine::Track {
                node: missing,
                prop: renamite_model::PropPath::new("opacity"),
                keys: vec![],
            }],
            events: vec![],
        });

        let report = validate(&file);
        assert!(report.has_errors());
        assert!(
            report
                .diagnostics
                .iter()
                .any(|d| d.message.contains("missing node"))
        );
    }
}