omena-engine-input-producers 0.1.4

Input-derived producer artifacts for Omena CSS semantic analysis
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
use std::collections::BTreeMap;

use serde::{Deserialize, Serialize};

mod expression_domain;
mod expression_semantics;
mod query_plan;
mod selector_usage;
mod semantic;
mod source_resolution;
mod source_side;
#[cfg(test)]
mod test_support;
mod type_facts;

pub use expression_domain::collect_expression_domain_flow_graphs;
pub use expression_domain::summarize_expression_domain_candidates_input;
pub use expression_domain::summarize_expression_domain_canonical_candidate_bundle_input;
pub use expression_domain::summarize_expression_domain_canonical_producer_signal_input;
pub use expression_domain::summarize_expression_domain_control_flow_analysis_input;
pub use expression_domain::summarize_expression_domain_evaluator_candidates_input;
pub use expression_domain::summarize_expression_domain_flow_analysis_input;
pub use expression_domain::summarize_expression_domain_fragments_input;
pub use expression_domain::summarize_expression_domain_plan_input;
pub use expression_semantics::summarize_expression_semantics_candidates_input;
pub use expression_semantics::summarize_expression_semantics_canonical_candidate_bundle_input;
pub use expression_semantics::summarize_expression_semantics_canonical_producer_signal_input;
pub use expression_semantics::summarize_expression_semantics_evaluator_candidates_input;
pub use expression_semantics::summarize_expression_semantics_fragments_input;
pub use expression_semantics::summarize_expression_semantics_match_fragments_input;
pub use expression_semantics::summarize_expression_semantics_query_fragments_input;
pub use query_plan::summarize_query_plan_input;
pub use selector_usage::summarize_selector_usage_candidates_input;
pub use selector_usage::summarize_selector_usage_canonical_candidate_bundle_input;
pub use selector_usage::summarize_selector_usage_canonical_producer_signal_input;
pub use selector_usage::summarize_selector_usage_evaluator_candidates_input;
pub use selector_usage::summarize_selector_usage_fragments_input;
pub use selector_usage::summarize_selector_usage_plan_input;
pub use selector_usage::summarize_selector_usage_query_fragments_input;
pub use semantic::summarize_semantic_canonical_candidate_bundle_input;
pub use semantic::summarize_semantic_canonical_producer_signal_input;
pub use semantic::summarize_semantic_evaluator_candidates_input;
pub use source_resolution::summarize_source_resolution_candidates_input;
pub use source_resolution::summarize_source_resolution_canonical_candidate_bundle_input;
pub use source_resolution::summarize_source_resolution_canonical_producer_signal_input;
pub use source_resolution::summarize_source_resolution_evaluator_candidates_input;
pub use source_resolution::summarize_source_resolution_fragments_input;
pub use source_resolution::summarize_source_resolution_match_fragments_input;
pub use source_resolution::summarize_source_resolution_plan_input;
pub use source_resolution::summarize_source_resolution_query_fragments_input;
pub use source_side::summarize_source_side_canonical_candidate_bundle_input;
pub use source_side::summarize_source_side_canonical_producer_signal_input;
pub use source_side::summarize_source_side_evaluator_candidates_input;
pub use type_facts::summarize_type_fact_input;

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct EngineInputV2 {
    pub version: String,
    pub sources: Vec<SourceAnalysisInputV2>,
    pub styles: Vec<StyleAnalysisInputV2>,
    pub type_facts: Vec<TypeFactEntryV2>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceAnalysisInputV2 {
    pub document: SourceDocumentV2,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceDocumentV2 {
    pub class_expressions: Vec<ClassExpressionInputV2>,
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct PositionV2 {
    pub line: usize,
    pub character: usize,
}

#[derive(Debug, Deserialize, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct RangeV2 {
    pub start: PositionV2,
    pub end: PositionV2,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct BemSuffixInfoV2 {
    pub raw_token_range: RangeV2,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ClassExpressionInputV2 {
    pub id: String,
    pub kind: String,
    pub scss_module_path: String,
    pub range: RangeV2,
    pub class_name: Option<String>,
    pub root_binding_decl_id: Option<String>,
    pub access_path: Option<Vec<String>>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StyleAnalysisInputV2 {
    pub file_path: String,
    pub document: StyleDocumentV2,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StyleDocumentV2 {
    pub selectors: Vec<StyleSelectorV2>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StyleSelectorV2 {
    pub name: String,
    pub view_kind: String,
    pub canonical_name: Option<String>,
    pub range: RangeV2,
    pub nested_safety: Option<String>,
    pub composes: Option<Vec<serde_json::Value>>,
    pub bem_suffix: Option<BemSuffixInfoV2>,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct TypeFactEntryV2 {
    pub file_path: String,
    pub expression_id: String,
    pub facts: StringTypeFactsV2,
}

#[derive(Debug, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct StringTypeFactsV2 {
    pub kind: String,
    pub constraint_kind: Option<String>,
    pub values: Option<Vec<String>>,
    pub prefix: Option<String>,
    pub suffix: Option<String>,
    pub min_len: Option<usize>,
    pub max_len: Option<usize>,
    pub char_must: Option<String>,
    pub char_may: Option<String>,
    pub may_include_other_chars: Option<bool>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct TypeFactInputSummaryV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub type_fact_count: usize,
    pub distinct_fact_files: usize,
    pub by_kind: BTreeMap<String, usize>,
    pub constrained_kinds: BTreeMap<String, usize>,
    pub finite_value_count: usize,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct QueryPlanSummaryV0 {
    schema_version: &'static str,
    input_version: String,
    expression_semantics_ids: Vec<String>,
    source_expression_resolution_ids: Vec<String>,
    selector_usage_ids: Vec<String>,
    total_query_count: usize,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainPlanSummaryV0 {
    schema_version: &'static str,
    input_version: String,
    planned_expression_ids: Vec<String>,
    value_domain_kinds: BTreeMap<String, usize>,
    value_constraint_kinds: BTreeMap<String, usize>,
    constraint_detail_counts: ConstraintDetailCounts,
    finite_value_count: usize,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainFragmentV0 {
    pub expression_id: String,
    pub file_path: String,
    pub value_domain_kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_suffix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_min_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_max_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_must: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_may: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_may_include_other_chars: Option<bool>,
    pub finite_value_count: usize,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainFragmentsV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub fragments: Vec<ExpressionDomainFragmentV0>,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainCandidateV0 {
    pub expression_id: String,
    pub file_path: String,
    pub value_domain_kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_suffix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_min_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_max_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_must: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_may: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_may_include_other_chars: Option<bool>,
    pub finite_value_count: usize,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub candidates: Vec<ExpressionDomainCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainCanonicalCandidateBundleV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub plan_summary: ExpressionDomainPlanSummaryV0,
    pub fragments: Vec<ExpressionDomainFragmentV0>,
    pub candidates: Vec<ExpressionDomainCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainEvaluatorCandidatePayloadV0 {
    pub expression_id: String,
    pub value_domain_kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_suffix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_min_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_max_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_must: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_may: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_may_include_other_chars: Option<bool>,
    pub finite_value_count: usize,
    pub value_domain_derivation: omena_abstract_value::ReducedClassValueDerivationV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainEvaluatorCandidateV0 {
    pub kind: &'static str,
    pub file_path: String,
    pub query_id: String,
    pub payload: ExpressionDomainEvaluatorCandidatePayloadV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainEvaluatorCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub results: Vec<ExpressionDomainEvaluatorCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainCanonicalProducerSignalV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub canonical_bundle: ExpressionDomainCanonicalCandidateBundleV0,
    pub evaluator_candidates: ExpressionDomainEvaluatorCandidatesV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainFlowAnalysisV0 {
    pub schema_version: &'static str,
    pub product: &'static str,
    pub input_version: String,
    pub analyses: Vec<ExpressionDomainFlowAnalysisEntryV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainFlowAnalysisEntryV0 {
    pub graph_id: String,
    pub file_path: String,
    pub analysis: omena_abstract_value::ClassValueFlowAnalysisV0,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ExpressionDomainFlowGraphEntryV0 {
    pub graph_id: String,
    pub file_path: String,
    pub graph: omena_abstract_value::ClassValueFlowGraphV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainControlFlowAnalysisV0 {
    pub schema_version: &'static str,
    pub product: &'static str,
    pub input_version: String,
    pub analyses: Vec<ExpressionDomainControlFlowAnalysisEntryV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionDomainControlFlowAnalysisEntryV0 {
    pub graph_id: String,
    pub file_path: String,
    pub analysis: omena_abstract_value::ClassValueControlFlowAnalysisV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsagePlanSummaryV0 {
    schema_version: &'static str,
    input_version: String,
    canonical_selector_names: Vec<String>,
    view_kind_counts: BTreeMap<String, usize>,
    nested_safety_counts: BTreeMap<String, usize>,
    composed_selector_count: usize,
    total_composes_refs: usize,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageFragmentV0 {
    pub ordinal: usize,
    pub view_kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub canonical_name: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nested_safety: Option<String>,
    pub composes_count: usize,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageFragmentsV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub fragments: Vec<SelectorUsageFragmentV0>,
}

#[derive(Debug, Clone, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageQueryFragmentV0 {
    pub query_id: String,
    pub canonical_name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub nested_safety: Option<String>,
    pub composes_count: usize,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageQueryFragmentsV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub fragments: Vec<SelectorUsageQueryFragmentV0>,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageCandidateV0 {
    pub query_id: String,
    pub canonical_name: String,
    pub file_path: String,
    pub total_references: usize,
    pub direct_reference_count: usize,
    pub editable_direct_reference_count: usize,
    pub exact_reference_count: usize,
    pub inferred_or_better_reference_count: usize,
    pub has_expanded_references: bool,
    pub has_style_dependency_references: bool,
    pub has_any_references: bool,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub candidates: Vec<SelectorUsageCandidateV0>,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageEvaluatorCandidatePayloadV0 {
    pub canonical_name: String,
    pub total_references: usize,
    pub direct_reference_count: usize,
    pub editable_direct_reference_count: usize,
    pub exact_reference_count: usize,
    pub inferred_or_better_reference_count: usize,
    pub has_expanded_references: bool,
    pub has_style_dependency_references: bool,
    pub has_any_references: bool,
    pub all_sites: Vec<SelectorUsageReferenceSiteV0>,
    pub editable_direct_sites: Vec<SelectorUsageEditableDirectSiteV0>,
}

#[derive(Debug, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageReferenceSiteV0 {
    pub file_path: String,
    pub range: RangeV2,
    pub expansion: String,
    pub reference_kind: String,
}

#[derive(Debug, Serialize, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageEditableDirectSiteV0 {
    pub file_path: String,
    pub range: RangeV2,
    pub class_name: String,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageEvaluatorCandidateV0 {
    pub kind: &'static str,
    pub file_path: String,
    pub query_id: String,
    pub payload: SelectorUsageEvaluatorCandidatePayloadV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageEvaluatorCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub results: Vec<SelectorUsageEvaluatorCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageCanonicalCandidateBundleV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub query_fragments: Vec<SelectorUsageQueryFragmentV0>,
    pub fragments: Vec<SelectorUsageFragmentV0>,
    pub candidates: Vec<SelectorUsageCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SelectorUsageCanonicalProducerSignalV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub canonical_bundle: SelectorUsageCanonicalCandidateBundleV0,
    pub evaluator_candidates: SelectorUsageEvaluatorCandidatesV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionPlanSummaryV0 {
    schema_version: &'static str,
    input_version: String,
    planned_expression_ids: Vec<String>,
    expression_kind_counts: BTreeMap<String, usize>,
    distinct_style_file_paths: Vec<String>,
    symbol_ref_with_binding_count: usize,
    style_access_count: usize,
    style_access_path_depth_sum: usize,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionQueryFragmentV0 {
    pub query_id: String,
    pub expression_id: String,
    pub expression_kind: String,
    pub style_file_path: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionQueryFragmentsV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub fragments: Vec<SourceResolutionQueryFragmentV0>,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionMatchFragmentV0 {
    pub query_id: String,
    pub expression_id: String,
    pub style_file_path: String,
    pub selector_names: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finite_values: Option<Vec<String>>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionMatchFragmentsV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub fragments: Vec<SourceResolutionMatchFragmentV0>,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionCandidateV0 {
    pub query_id: String,
    pub expression_id: String,
    pub style_file_path: String,
    pub selector_names: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finite_values: Option<Vec<String>>,
    pub selector_certainty: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_certainty: Option<String>,
    pub selector_certainty_shape_kind: String,
    pub selector_certainty_shape_label: String,
    pub value_certainty_shape_kind: String,
    pub value_certainty_shape_label: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_certainty_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_suffix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_min_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_max_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_must: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_may: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_may_include_other_chars: Option<bool>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub candidates: Vec<SourceResolutionCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionCanonicalCandidateBundleV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub query_fragments: Vec<SourceResolutionQueryFragmentV0>,
    pub fragments: Vec<SourceResolutionFragmentV0>,
    pub match_fragments: Vec<SourceResolutionMatchFragmentV0>,
    pub candidates: Vec<SourceResolutionCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionEvaluatorCandidatePayloadV0 {
    pub expression_id: String,
    pub style_file_path: String,
    pub selector_names: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finite_values: Option<Vec<String>>,
    pub selector_certainty: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_certainty: Option<String>,
    pub selector_certainty_shape_kind: String,
    pub selector_certainty_shape_label: String,
    pub value_certainty_shape_kind: String,
    pub value_certainty_shape_label: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_certainty_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_suffix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_min_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_max_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_must: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_may: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_may_include_other_chars: Option<bool>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionEvaluatorCandidateV0 {
    pub kind: &'static str,
    pub file_path: String,
    pub query_id: String,
    pub payload: SourceResolutionEvaluatorCandidatePayloadV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionEvaluatorCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub results: Vec<SourceResolutionEvaluatorCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionCanonicalProducerSignalV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub canonical_bundle: SourceResolutionCanonicalCandidateBundleV0,
    pub evaluator_candidates: SourceResolutionEvaluatorCandidatesV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceSideCanonicalCandidateBundleV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub expression_semantics: ExpressionSemanticsCanonicalCandidateBundleV0,
    pub source_resolution: SourceResolutionCanonicalCandidateBundleV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceSideEvaluatorCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub expression_semantics: ExpressionSemanticsEvaluatorCandidatesV0,
    pub source_resolution: SourceResolutionEvaluatorCandidatesV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceSideCanonicalProducerSignalV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub canonical_bundle: SourceSideCanonicalCandidateBundleV0,
    pub evaluator_candidates: SourceSideEvaluatorCandidatesV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SemanticCanonicalCandidateBundleV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub source_side: SourceSideCanonicalCandidateBundleV0,
    pub expression_domain: ExpressionDomainCanonicalCandidateBundleV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SemanticEvaluatorCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub source_side: SourceSideEvaluatorCandidatesV0,
    pub expression_domain: ExpressionDomainEvaluatorCandidatesV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SemanticCanonicalProducerSignalV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub canonical_bundle: SemanticCanonicalCandidateBundleV0,
    pub evaluator_candidates: SemanticEvaluatorCandidatesV0,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsFragmentV0 {
    query_id: String,
    expression_id: String,
    expression_kind: String,
    style_file_path: String,
    value_domain_kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_suffix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_min_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_max_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_char_must: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_char_may: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_may_include_other_chars: Option<bool>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsFragmentsV0 {
    schema_version: &'static str,
    input_version: String,
    fragments: Vec<ExpressionSemanticsFragmentV0>,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsQueryFragmentV0 {
    pub query_id: String,
    pub expression_id: String,
    pub expression_kind: String,
    pub style_file_path: String,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsQueryFragmentsV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub fragments: Vec<ExpressionSemanticsQueryFragmentV0>,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsMatchFragmentV0 {
    pub query_id: String,
    pub expression_id: String,
    pub style_file_path: String,
    pub selector_names: Vec<String>,
    pub candidate_names: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finite_values: Option<Vec<String>>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsMatchFragmentsV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub fragments: Vec<ExpressionSemanticsMatchFragmentV0>,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsCandidateV0 {
    pub query_id: String,
    pub expression_id: String,
    pub expression_kind: String,
    pub style_file_path: String,
    pub selector_names: Vec<String>,
    pub candidate_names: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finite_values: Option<Vec<String>>,
    pub value_domain_kind: String,
    pub selector_certainty: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_certainty: Option<String>,
    pub selector_certainty_shape_kind: String,
    pub selector_certainty_shape_label: String,
    pub value_certainty_shape_kind: String,
    pub value_certainty_shape_label: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_certainty_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_suffix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_min_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_max_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_must: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_may: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_may_include_other_chars: Option<bool>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub candidates: Vec<ExpressionSemanticsCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsCanonicalCandidateBundleV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub query_fragments: Vec<ExpressionSemanticsQueryFragmentV0>,
    pub fragments: Vec<ExpressionSemanticsFragmentV0>,
    pub match_fragments: Vec<ExpressionSemanticsMatchFragmentV0>,
    pub candidates: Vec<ExpressionSemanticsCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsEvaluatorCandidatePayloadV0 {
    pub expression_id: String,
    pub expression_kind: String,
    pub style_file_path: String,
    pub selector_names: Vec<String>,
    pub candidate_names: Vec<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub finite_values: Option<Vec<String>>,
    pub value_domain_kind: String,
    pub selector_certainty: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_certainty: Option<String>,
    pub selector_certainty_shape_kind: String,
    pub selector_certainty_shape_label: String,
    pub value_certainty_shape_kind: String,
    pub value_certainty_shape_label: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selector_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_certainty_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_suffix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_min_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_max_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_must: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_char_may: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub value_may_include_other_chars: Option<bool>,
    pub value_domain_derivation: omena_abstract_value::ReducedClassValueDerivationV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsEvaluatorCandidateV0 {
    pub kind: &'static str,
    pub file_path: String,
    pub query_id: String,
    pub payload: ExpressionSemanticsEvaluatorCandidatePayloadV0,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsEvaluatorCandidatesV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub results: Vec<ExpressionSemanticsEvaluatorCandidateV0>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct ExpressionSemanticsCanonicalProducerSignalV0 {
    pub schema_version: &'static str,
    pub input_version: String,
    pub canonical_bundle: ExpressionSemanticsCanonicalCandidateBundleV0,
    pub evaluator_candidates: ExpressionSemanticsEvaluatorCandidatesV0,
}

#[derive(Debug, Serialize, Clone)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionFragmentV0 {
    query_id: String,
    expression_id: String,
    style_file_path: String,
    value_certainty_shape_kind: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_certainty_constraint_kind: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_prefix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_suffix: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_min_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_max_len: Option<usize>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_char_must: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_char_may: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    value_may_include_other_chars: Option<bool>,
}

#[derive(Debug, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct SourceResolutionFragmentsV0 {
    schema_version: &'static str,
    input_version: String,
    fragments: Vec<SourceResolutionFragmentV0>,
}

#[derive(Debug, Serialize, Default, Clone)]
#[serde(rename_all = "camelCase")]
pub struct ConstraintDetailCounts {
    pub prefix_count: usize,
    pub suffix_count: usize,
    pub min_len_count: usize,
    pub min_len_sum: usize,
    pub max_len_count: usize,
    pub max_len_sum: usize,
    pub char_must_count: usize,
    pub char_must_len_sum: usize,
    pub char_may_count: usize,
    pub char_may_len_sum: usize,
    pub may_include_other_chars_count: usize,
}

fn collect_constraint_detail_counts(
    counts: &mut ConstraintDetailCounts,
    details: ConstraintDetailInput<'_>,
) {
    if details.prefix.is_some() {
        counts.prefix_count += 1;
    }
    if details.suffix.is_some() {
        counts.suffix_count += 1;
    }
    if let Some(value) = details.min_len {
        counts.min_len_count += 1;
        counts.min_len_sum += value;
    }
    if let Some(value) = details.max_len {
        counts.max_len_count += 1;
        counts.max_len_sum += value;
    }
    if let Some(value) = details.char_must {
        counts.char_must_count += 1;
        counts.char_must_len_sum += value.len();
    }
    if let Some(value) = details.char_may {
        counts.char_may_count += 1;
        counts.char_may_len_sum += value.len();
    }
    if details.may_include_other_chars == Some(true) {
        counts.may_include_other_chars_count += 1;
    }
}

pub(crate) struct ConstraintDetailInput<'a> {
    pub(crate) prefix: Option<&'a String>,
    pub(crate) suffix: Option<&'a String>,
    pub(crate) min_len: Option<usize>,
    pub(crate) max_len: Option<usize>,
    pub(crate) char_must: Option<&'a String>,
    pub(crate) char_may: Option<&'a String>,
    pub(crate) may_include_other_chars: Option<bool>,
}

pub(crate) fn map_expression_value_domain_kind(facts: &StringTypeFactsV2) -> String {
    omena_abstract_value::expression_value_domain_kind_from_facts(&abstract_value_facts(facts))
}

pub(crate) fn map_reduced_expression_value_domain_kind(facts: &StringTypeFactsV2) -> String {
    omena_abstract_value::reduced_value_domain_kind_from_facts(&abstract_value_facts(facts))
        .to_string()
}

pub(crate) fn map_reduced_expression_value_domain_derivation(
    facts: &StringTypeFactsV2,
) -> omena_abstract_value::ReducedClassValueDerivationV0 {
    omena_abstract_value::reduced_class_value_derivation_from_facts(&abstract_value_facts(facts))
}

pub(crate) fn map_value_certainty(facts: &StringTypeFactsV2) -> Option<String> {
    omena_abstract_value::value_certainty_from_facts(&abstract_value_facts(facts))
        .map(str::to_string)
}

pub(crate) fn map_value_certainty_shape_kind(facts: &StringTypeFactsV2) -> String {
    omena_abstract_value::value_certainty_shape_kind_from_facts(&abstract_value_facts(facts))
        .to_string()
}

pub(crate) fn map_value_certainty_shape_label(facts: &StringTypeFactsV2) -> String {
    omena_abstract_value::value_certainty_shape_label_from_facts(&abstract_value_facts(facts))
}

pub(crate) fn map_selector_certainty_shape_kind(
    facts: &StringTypeFactsV2,
    matched_selector_count: usize,
    selector_universe_count: usize,
) -> String {
    omena_abstract_value::selector_certainty_shape_kind_from_facts(
        &abstract_value_facts(facts),
        matched_selector_count,
        selector_universe_count,
    )
    .to_string()
}

pub(crate) fn map_selector_certainty_shape_label(
    facts: &StringTypeFactsV2,
    matched_selector_count: usize,
    selector_universe_count: usize,
) -> String {
    omena_abstract_value::selector_certainty_shape_label_from_facts(
        &abstract_value_facts(facts),
        matched_selector_count,
        selector_universe_count,
    )
}

pub(crate) fn map_selector_certainty(
    facts: &StringTypeFactsV2,
    matched_selector_count: usize,
    selector_universe_count: usize,
) -> String {
    omena_abstract_value::selector_certainty_from_facts(
        &abstract_value_facts(facts),
        matched_selector_count,
        selector_universe_count,
    )
    .to_string()
}

pub(crate) fn finite_values_for_facts(facts: &StringTypeFactsV2) -> Option<Vec<String>> {
    omena_abstract_value::finite_values_from_facts(&abstract_value_facts(facts))
}

pub(crate) fn abstract_value_facts(
    facts: &StringTypeFactsV2,
) -> omena_abstract_value::ExternalStringTypeFactsV0 {
    omena_abstract_value::ExternalStringTypeFactsV0 {
        kind: facts.kind.clone(),
        constraint_kind: facts.constraint_kind.clone(),
        values: facts.values.clone(),
        prefix: facts.prefix.clone(),
        suffix: facts.suffix.clone(),
        min_len: facts.min_len,
        max_len: facts.max_len,
        char_must: facts.char_must.clone(),
        char_may: facts.char_may.clone(),
        may_include_other_chars: facts.may_include_other_chars,
    }
}

pub(crate) fn resolve_selector_names(
    style: &StyleAnalysisInputV2,
    facts: &StringTypeFactsV2,
) -> Vec<String> {
    match facts.kind.as_str() {
        "unknown" => Vec::new(),
        "top" => canonical_selector_names(style),
        "exact" | "finiteSet" => {
            let mut names = Vec::new();
            for value in facts.values.as_ref().into_iter().flatten() {
                push_canonical_match(style, value, &mut names);
            }
            names
        }
        "constrained" => resolve_constrained_selector_names(style, facts),
        _ => Vec::new(),
    }
}

fn resolve_constrained_selector_names(
    style: &StyleAnalysisInputV2,
    facts: &StringTypeFactsV2,
) -> Vec<String> {
    let mut names = Vec::new();

    for selector in &style.document.selectors {
        if !matches_selector_constraints(selector, facts) {
            continue;
        }
        let canonical_name = canonical_name_for_selector(style, selector);
        if let Some(canonical_name) = canonical_name
            && !names.contains(&canonical_name)
        {
            names.push(canonical_name);
        }
    }

    names
}

fn matches_selector_constraints(selector: &StyleSelectorV2, facts: &StringTypeFactsV2) -> bool {
    match facts.constraint_kind.as_deref() {
        Some("prefix") => facts
            .prefix
            .as_ref()
            .is_some_and(|prefix| selector.name.starts_with(prefix)),
        Some("suffix") => facts
            .suffix
            .as_ref()
            .is_some_and(|suffix| selector.name.ends_with(suffix)),
        Some("prefixSuffix") => {
            let prefix_ok = facts
                .prefix
                .as_ref()
                .is_none_or(|prefix| selector.name.starts_with(prefix));
            let suffix_ok = facts
                .suffix
                .as_ref()
                .is_none_or(|suffix| selector.name.ends_with(suffix));
            let min_len_ok = facts
                .min_len
                .is_none_or(|min_len| selector.name.len() >= min_len);
            let max_len_ok = facts
                .max_len
                .is_none_or(|max_len| selector.name.len() <= max_len);
            prefix_ok && suffix_ok && min_len_ok && max_len_ok
        }
        Some("charInclusion") => matches_char_constraints(
            &selector.name,
            facts.char_must.as_deref().unwrap_or(""),
            facts.char_may.as_deref().unwrap_or(""),
            facts.may_include_other_chars.unwrap_or(false),
        ),
        Some("composite") => {
            let prefix_ok = facts
                .prefix
                .as_ref()
                .is_none_or(|prefix| selector.name.starts_with(prefix));
            let suffix_ok = facts
                .suffix
                .as_ref()
                .is_none_or(|suffix| selector.name.ends_with(suffix));
            let min_len_ok = facts
                .min_len
                .is_none_or(|min_len| selector.name.len() >= min_len);
            let max_len_ok = facts
                .max_len
                .is_none_or(|max_len| selector.name.len() <= max_len);
            prefix_ok
                && suffix_ok
                && min_len_ok
                && max_len_ok
                && matches_char_constraints(
                    &selector.name,
                    facts.char_must.as_deref().unwrap_or(""),
                    facts.char_may.as_deref().unwrap_or(""),
                    facts.may_include_other_chars.unwrap_or(false),
                )
        }
        _ => false,
    }
}

fn matches_char_constraints(
    value: &str,
    must_chars: &str,
    may_chars: &str,
    may_include_other_chars: bool,
) -> bool {
    let value_chars: std::collections::BTreeSet<char> = value.chars().collect();
    let must_set: std::collections::BTreeSet<char> = must_chars.chars().collect();
    let may_set: std::collections::BTreeSet<char> = may_chars.chars().collect();

    if must_set.iter().any(|char| !value_chars.contains(char)) {
        return false;
    }
    if !may_include_other_chars && value_chars.iter().any(|char| !may_set.contains(char)) {
        return false;
    }
    true
}

fn push_canonical_match(style: &StyleAnalysisInputV2, view_name: &str, names: &mut Vec<String>) {
    if let Some(canonical_name) = canonical_name_for_view_name(style, view_name)
        && !names.contains(&canonical_name)
    {
        names.push(canonical_name);
    }
}

fn canonical_selector_names(style: &StyleAnalysisInputV2) -> Vec<String> {
    let mut names = Vec::new();
    for selector in &style.document.selectors {
        if selector.view_kind == "canonical"
            && let Some(canonical_name) = selector.canonical_name.clone()
            && !names.contains(&canonical_name)
        {
            names.push(canonical_name);
        }
    }
    names
}

pub(crate) fn canonical_selector_count(style: &StyleAnalysisInputV2) -> usize {
    canonical_selector_names(style).len()
}

fn canonical_name_for_selector(
    style: &StyleAnalysisInputV2,
    selector: &StyleSelectorV2,
) -> Option<String> {
    canonical_name_for_view_name(style, &selector.name)
}

fn canonical_name_for_view_name(style: &StyleAnalysisInputV2, view_name: &str) -> Option<String> {
    let matched = style
        .document
        .selectors
        .iter()
        .find(|selector| selector.name == view_name)?;
    let canonical = style.document.selectors.iter().find(|selector| {
        selector.view_kind == "canonical" && selector.canonical_name == matched.canonical_name
    });
    canonical
        .and_then(|selector| selector.canonical_name.clone())
        .or_else(|| matched.canonical_name.clone())
        .or_else(|| Some(matched.name.clone()))
}