evfmt 0.2.0

Emoji Variation Formatter
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
//! Policy-aware findings for scanned emoji variation structures.
//!
//! This module implements the policy and fixed-rule analysis steps from the
//! conceptual formatting algorithm. It produces findings with valid replacement
//! decision slots and a render plan, so callers can choose a replacement
//! without re-reading policy for the same item.
//! Interactive callers normally use this module directly: [`analyze_scan_item`]
//! computes reasonableness, applies [`Policy`] only where policy is relevant,
//! and stores the valid replacement plan in each [`Finding`].
//!
//! - [`crate::scanner`] decides structural item boundaries
//! - [`analyze_scan_item`] turns policy-neutral reasonableness into findings and replacement slots
//!
//! Use this module when callers need to inspect or override repairs
//! item-by-item; otherwise [`crate::format_text`] is the shorter path.
//!
//! # Examples
//!
//! ```rust
//! use evfmt::{Policy, scan};
//! use evfmt::findings::analyze_scan_item;
//!
//! let policy = Policy::default();
//! let input = "A\u{FE0F}\u{00A9}";
//!
//! let repaired = scan(input)
//!     .map(|item| {
//!         analyze_scan_item(&item, &policy).map_or_else(
//!             || item.raw.to_owned(),
//!             |finding| finding.default_replacement().to_owned(),
//!         )
//!     })
//!     .collect::<String>();
//!
//! assert_eq!(repaired, "A\u{00A9}\u{FE0F}");
//! ```

use std::ops::Range;

use crate::policy::{Policy, SingletonRule};
use crate::scanner::{
    EmojiLike, EmojiModification, EmojiSequence, EmojiStem, Presentation, ScanItem, ScanKind,
    ZwjJoinedEmoji, ZwjLink,
};
use crate::unicode;

mod fixed;

use fixed::FixedEmojiLike;

#[cfg(test)]
mod tests;

// --- Public violation / decision types ---

/// Reason a scanned item is non-canonical.
///
/// Unsanctioned presentation selectors are tracked as a separate axis because
/// they can be the whole problem, or they can appear alongside a primary
/// sequence/policy violation.
///
/// # Examples
///
/// ```rust
/// use evfmt::{Policy, scan};
/// use evfmt::findings::{Violation, analyze_scan_item};
///
/// let policy = Policy::default();
/// let finding = scan("A\u{FE0F}")
///     .find_map(|item| analyze_scan_item(&item, &policy))
///     .unwrap();
///
/// assert_eq!(finding.violation(), Violation::UnsanctionedSelectorsOnly);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Violation {
    /// The finding only contains unsanctioned presentation selectors.
    ///
    /// This can happen either as an isolated selector run or inside an
    /// otherwise canonical emoji-shaped item.
    UnsanctionedSelectorsOnly,
    /// The finding has a primary violation, and may also contain unsanctioned
    /// presentation selectors.
    Primary(PrimaryViolation),
}

impl Violation {
    const fn primary(kind: PrimaryViolationKind, has_unsanctioned_selectors: bool) -> Self {
        Self::Primary(PrimaryViolation::new(kind, has_unsanctioned_selectors))
    }

    const fn with_unsanctioned_selectors(self) -> Self {
        match self {
            Self::UnsanctionedSelectorsOnly => Self::UnsanctionedSelectorsOnly,
            Self::Primary(primary) => Self::Primary(PrimaryViolation {
                has_unsanctioned_selectors: true,
                ..primary
            }),
        }
    }
}

/// A primary sequence or policy violation, plus any attached selector cleanup.
///
/// # Examples
///
/// ```rust
/// use evfmt::{Policy, scan};
/// use evfmt::findings::{PrimaryViolationKind, Violation, analyze_scan_item};
///
/// let policy = Policy::default();
/// let finding = scan("\u{00A9}")
///     .find_map(|item| analyze_scan_item(&item, &policy))
///     .unwrap();
///
/// let Violation::Primary(primary) = finding.violation() else {
///     panic!("bare copyright should need policy resolution");
/// };
/// assert_eq!(primary.kind, PrimaryViolationKind::BareNeedsResolution);
/// assert!(!primary.has_unsanctioned_selectors);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct PrimaryViolation {
    /// The primary reason this item is non-canonical.
    pub kind: PrimaryViolationKind,
    /// Whether the same item also contains unsanctioned presentation selectors.
    pub has_unsanctioned_selectors: bool,
}

impl PrimaryViolation {
    const fn new(kind: PrimaryViolationKind, has_unsanctioned_selectors: bool) -> Self {
        Self {
            kind,
            has_unsanctioned_selectors,
        }
    }
}

/// Primary sequence or policy category for a non-canonical scanned item.
///
/// # Examples
///
/// ```rust
/// use evfmt::{Policy, scan};
/// use evfmt::findings::{PrimaryViolationKind, Violation, analyze_scan_item};
///
/// let policy = Policy::default();
/// let finding = scan("#\u{FE0E}")
///     .find_map(|item| analyze_scan_item(&item, &policy))
///     .unwrap();
///
/// assert!(matches!(
///     finding.violation(),
///     Violation::Primary(primary)
///         if primary.kind == PrimaryViolationKind::RedundantSelector
/// ));
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum PrimaryViolationKind {
    /// Wrong or missing presentation selectors in a keycap or ZWJ-related sequence.
    NotFullyQualifiedSequence,
    /// Sanctioned presentation selector that matches the bare side on a bare-preferred
    /// character. The requested presentation is valid, but the formatting policy
    /// prefers bare form.
    RedundantSelector,
    /// Bare variation-sequence character that the formatting policy wants to resolve
    /// with a presentation selector. The bare form is valid, but the policy prefers an
    /// explicit presentation selector.
    BareNeedsResolution,
}

/// One selector-bearing presentation choice in a replacement decision vector.
///
/// A complete replacement decision is a slice of these choices, one per
/// [`DecisionSlot`] reported by a finding. Fixed repairs have no presentation
/// slots, so their complete decision vector is empty. In particular, choosing
/// bare form is represented as a fixed repair rather than as a public decision
/// slot.
///
/// # Examples
///
/// ```rust
/// use evfmt::{Policy, scan};
/// use evfmt::findings::{ReplacementDecision, analyze_scan_item};
///
/// let policy = Policy::default();
/// let finding = scan("\u{00A9}")
///     .find_map(|item| analyze_scan_item(&item, &policy))
///     .unwrap();
///
/// assert_eq!(
///     finding.replacement(&[ReplacementDecision::Text]).unwrap(),
///     "\u{00A9}\u{FE0E}"
/// );
/// assert_eq!(
///     finding.replacement(&[ReplacementDecision::Emoji]).unwrap(),
///     "\u{00A9}\u{FE0F}"
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ReplacementDecision {
    /// Resolve this slot as text presentation.
    Text,
    /// Resolve this slot as emoji presentation.
    Emoji,
}

impl ReplacementDecision {
    const fn from_presentation(presentation: Presentation) -> Self {
        match presentation {
            Presentation::Text => Self::Text,
            Presentation::Emoji => Self::Emoji,
        }
    }
}

/// One presentation slot in a finding's replacement decision vector.
///
/// # Examples
///
/// ```rust
/// use evfmt::{Policy, scan};
/// use evfmt::findings::{ReplacementDecision, analyze_scan_item};
///
/// let policy = Policy::default();
/// let finding = scan("\u{00A9}")
///     .find_map(|item| analyze_scan_item(&item, &policy))
///     .unwrap();
///
/// let slot = &finding.decision_slots()[0];
/// assert_eq!(slot.choices(), &[ReplacementDecision::Text, ReplacementDecision::Emoji]);
/// assert_eq!(slot.default_decision(), ReplacementDecision::Emoji);
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct DecisionSlot {
    choices: Vec<ReplacementDecision>,
    default: ReplacementDecision,
}

impl DecisionSlot {
    /// Valid choices for this slot.
    #[must_use]
    pub fn choices(&self) -> &[ReplacementDecision] {
        &self.choices
    }

    /// The choice batch formatting applies to this slot by default.
    #[must_use]
    pub const fn default_decision(&self) -> ReplacementDecision {
        self.default
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct SlotReplacement {
    decision: ReplacementDecision,
    replacement: String,
}

#[derive(Debug, Clone, PartialEq, Eq)]
struct ReplacementSlotPlan {
    public: DecisionSlot,
    replacements: Vec<SlotReplacement>,
}

impl ReplacementSlotPlan {
    fn replacement(&self, decision: ReplacementDecision) -> Option<&str> {
        self.replacements
            .iter()
            .find(|replacement| replacement.decision == decision)
            .map(|replacement| replacement.replacement.as_str())
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
enum ReplacementPiece {
    Literal(String),
    Slot(usize),
}

/// Valid replacement decision vector and render plan for a finding.
#[derive(Debug, Clone, PartialEq, Eq)]
struct ReplacementPlan {
    /// Why the item is non-canonical.
    violation: Violation,
    /// Public presentation slots in this finding's decision vector.
    decision_slots: Vec<DecisionSlot>,
    /// Presentation slots callers must decide to select a non-default
    /// replacement. Empty means the finding is a fixed repair.
    slots: Vec<ReplacementSlotPlan>,
    /// Render plan for this finding's replacement. Slot references index into
    /// `slots`; literal pieces already include fixed cleanup.
    pieces: Vec<ReplacementPiece>,
    /// The replacement batch formatting applies by default.
    default_replacement: String,
}

impl ReplacementPlan {
    /// Why the analyzed item is non-canonical.
    #[must_use]
    const fn violation(&self) -> Violation {
        self.violation
    }

    /// Presentation slots in this finding's replacement decision vector.
    #[must_use]
    fn decision_slots(&self) -> &[DecisionSlot] {
        &self.decision_slots
    }

    /// The replacement decision batch formatting applies by default.
    #[must_use]
    fn default_decision(&self) -> Vec<ReplacementDecision> {
        self.slots
            .iter()
            .map(|slot| slot.public.default_decision())
            .collect()
    }

    /// The replacement batch formatting applies by default.
    #[must_use]
    fn default_replacement(&self) -> &str {
        &self.default_replacement
    }

    fn render_replacement(&self, decisions: &[ReplacementDecision]) -> Option<String> {
        if decisions.len() != self.slots.len() {
            return None;
        }

        let mut out = String::new();
        for piece in &self.pieces {
            match piece {
                ReplacementPiece::Literal(text) => out.push_str(text),
                ReplacementPiece::Slot(slot_index) => {
                    let decision = decisions.get(*slot_index).copied()?;
                    out.push_str(self.slots.get(*slot_index)?.replacement(decision)?);
                }
            }
        }
        Some(out)
    }
}

/// A single non-canonical scanned item with its valid replacement decisions and replacements.
///
/// # Examples
///
/// ```rust
/// use evfmt::{Policy, scan};
/// use evfmt::findings::{ReplacementDecision, analyze_scan_item};
///
/// let policy = Policy::default();
/// let finding = scan("\u{00A9}")
///     .find_map(|item| analyze_scan_item(&item, &policy))
///     .unwrap();
///
/// assert_eq!(finding.raw, "\u{00A9}");
/// assert_eq!(finding.default_replacement(), "\u{00A9}\u{FE0F}");
/// assert_eq!(
///     finding.replacement(&[ReplacementDecision::Text]).unwrap(),
///     "\u{00A9}\u{FE0E}"
/// );
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Finding<'a> {
    /// Byte range in the original input.
    pub span: Range<usize>,
    /// Original raw source slice for the item.
    pub raw: &'a str,
    /// How valid replacement decisions map to replacement text for this finding.
    replacement_plan: ReplacementPlan,
}

impl Finding<'_> {
    /// Why the analyzed item is non-canonical.
    #[must_use]
    pub const fn violation(&self) -> Violation {
        self.replacement_plan.violation()
    }

    /// Presentation slots in this finding's replacement decision vector.
    ///
    /// A fixed repair has no presentation slots. Call [`Finding::replacement`]
    /// with an empty decision slice to apply that repair.
    #[must_use]
    pub fn decision_slots(&self) -> &[DecisionSlot] {
        self.replacement_plan.decision_slots()
    }

    /// The replacement decision batch formatting applies by default.
    #[must_use]
    pub fn default_decision(&self) -> Vec<ReplacementDecision> {
        self.replacement_plan.default_decision()
    }

    /// The replacement text for the decision batch formatting applies by default.
    ///
    /// This is the infallible borrowed form of:
    ///
    /// ```rust
    /// use evfmt::Policy;
    /// use evfmt::findings::analyze_scan_item;
    /// use evfmt::scanner::scan;
    ///
    /// let policy = Policy::default();
    /// let finding = scan("A\u{FE0F}")
    ///     .find_map(|item| analyze_scan_item(&item, &policy))
    ///     .unwrap();
    ///
    /// finding.replacement(&finding.default_decision()).unwrap();
    /// ```
    ///
    /// The default decision vector is always valid for this finding.
    #[must_use]
    pub fn default_replacement(&self) -> &str {
        self.replacement_plan.default_replacement()
    }

    /// Return the replacement text for a valid replacement decision vector.
    ///
    /// The decision slice must contain exactly one choice for each
    /// [`DecisionSlot`] returned by [`Finding::decision_slots`]. Fixed repairs
    /// have no slots and therefore use an empty decision slice.
    ///
    /// Returns `None` when the decision vector has the wrong length or contains
    /// a choice that is not valid for its slot.
    /// Callers that want to skip a finding can keep [`Finding::raw`].
    #[must_use]
    pub fn replacement(&self, decision: &[ReplacementDecision]) -> Option<String> {
        self.replacement_plan.render_replacement(decision)
    }
}

// --- Public API ---

/// Analyze a scanned item under the current formatter policy.
///
/// # Examples
///
/// ```rust
/// use evfmt::{Policy, scan};
/// use evfmt::findings::{Violation, analyze_scan_item};
///
/// let policy = Policy::default();
///
/// assert!(scan("plain text")
///     .all(|item| analyze_scan_item(&item, &policy).is_none()));
///
/// let selector_item = scan("\u{FE0F}").next().unwrap();
/// let finding = analyze_scan_item(&selector_item, &policy).unwrap();
/// assert_eq!(finding.violation(), Violation::UnsanctionedSelectorsOnly);
/// assert_eq!(finding.replacement(&[]).unwrap(), "");
/// ```
#[must_use]
pub fn analyze_scan_item<'a>(item: &ScanItem<'a>, policy: &Policy) -> Option<Finding<'a>> {
    match &item.kind {
        ScanKind::Passthrough => None,
        ScanKind::UnsanctionedPresentationSelectors(_) => Some(unambiguous_finding(
            item,
            Violation::UnsanctionedSelectorsOnly,
            String::new(),
        )),
        ScanKind::EmojiSequence(sequence) => match sequence {
            // The scanner preserves malformed ZWJ-like shapes such as leading,
            // consecutive, or trailing ZWJ links. Finding analysis keeps that
            // non-selector structure intact: these paths only remove or
            // normalize presentation selectors. The scanner's typed sequence
            // shape determines which presentation rules apply:
            //
            // - `LinksOnly`: only link-attached selectors can be cleaned
            // - single-component `EmojiHeaded`: use the ordinary
            //   singleton/flag rules for that component, and preserve any
            //   trailing ZWJ links in the same scanned item
            // - joined `EmojiHeaded`: preserve ZWJ links but resolve each
            //   component with the same component-local policy/fixed cleanup
            //   it would use outside the surrounding ZWJ links
            //
            // This is the findings-side implementation of the ZWJ-related
            // sequence contract in
            // `docs/designs/features/sequence-handling.markdown`.
            EmojiSequence::LinksOnly(links) => analyze_links_only_zwj_sequence(item, links),
            EmojiSequence::EmojiHeaded {
                first,
                joined,
                trailing_links,
            } if joined.is_empty() => analyze_single_emoji(item, first, trailing_links, policy),
            EmojiSequence::EmojiHeaded {
                first,
                joined,
                trailing_links,
            } => analyze_multi_emoji_zwj_sequence(item, first, joined, trailing_links, policy),
        },
    }
}

// --- Case: single emoji, either standalone or wrapped in trailing ZWJ links ---

fn analyze_single_emoji<'a>(
    item: &ScanItem<'a>,
    emoji: &EmojiLike,
    trailing_links: &[ZwjLink],
    policy: &Policy,
) -> Option<Finding<'a>> {
    match &emoji.stem {
        EmojiStem::SingletonBase {
            base,
            presentation_selectors_after_base,
        } => {
            let outcome = singleton_analysis_outcome(
                *base,
                presentation_selectors_after_base,
                &emoji.modifiers,
                policy,
            );
            single_emoji_singleton_finding(
                item,
                *base,
                presentation_selectors_after_base,
                &emoji.modifiers,
                trailing_links,
                outcome,
            )
        }
        EmojiStem::Flag {
            first_ri,
            presentation_selectors_after_first_ri,
            second_ri,
            presentation_selectors_after_second_ri,
        } => analyze_single_flag(
            item,
            *first_ri,
            presentation_selectors_after_first_ri,
            *second_ri,
            presentation_selectors_after_second_ri,
            &emoji.modifiers,
            trailing_links,
        ),
    }
}

/// Analyze a regional-indicator flag stem, with or without modifications.
///
/// Flag stems have no base-presentation slot. Regional indicators never carry
/// a sanctioned presentation sequence, so every presentation selector attached
/// to either RI is unsanctioned and removed under rule 1.
///
/// If greedy scanner grouping attaches modifiers, keycaps, or tags after a flag
/// stem, their own trailing presentation selectors are also rule-1 cleanup:
/// preserve the modification content and strip the unsanctioned selectors.
fn analyze_single_flag<'a>(
    item: &ScanItem<'a>,
    first_ri: char,
    presentation_selectors_after_first_ri: &[Presentation],
    second_ri: char,
    presentation_selectors_after_second_ri: &[Presentation],
    modifications: &[EmojiModification],
    trailing_links: &[ZwjLink],
) -> Option<Finding<'a>> {
    let has_ri_presentation_selectors = !presentation_selectors_after_first_ri.is_empty()
        || !presentation_selectors_after_second_ri.is_empty();
    let has_modification_presentation_selectors =
        has_trailing_modification_presentation_selectors(modifications);
    let has_link_presentation_selectors = trailing_links.iter().any(zwj_link_has_selectors);

    if !has_ri_presentation_selectors
        && !has_modification_presentation_selectors
        && !has_link_presentation_selectors
    {
        return None;
    }

    Some(unambiguous_finding(
        item,
        Violation::UnsanctionedSelectorsOnly,
        render_flag_with_links(first_ri, second_ri, modifications, trailing_links),
    ))
}

fn analyze_links_only_zwj_sequence<'a>(
    item: &ScanItem<'a>,
    links: &[ZwjLink],
) -> Option<Finding<'a>> {
    let has_link_presentation_selectors = links.iter().any(zwj_link_has_selectors);
    if !has_link_presentation_selectors {
        return None;
    }

    Some(unambiguous_finding(
        item,
        Violation::UnsanctionedSelectorsOnly,
        render_zwj_links_only_sequence(links),
    ))
}

fn single_emoji_singleton_finding<'a>(
    item: &ScanItem<'a>,
    base: char,
    presentation_selectors_after_base: &[Presentation],
    modifications: &[EmojiModification],
    trailing_links: &[ZwjLink],
    outcome: SingletonAnalysisOutcome,
) -> Option<Finding<'a>> {
    let has_link_presentation_selectors = trailing_links.iter().any(zwj_link_has_selectors);

    match outcome {
        SingletonAnalysisOutcome::Canonical if !has_link_presentation_selectors => None,
        SingletonAnalysisOutcome::Canonical => {
            debug_assert!(
                presentation_selectors_after_base.len() <= 1,
                "canonical singleton base cannot carry multiple presentation selectors"
            );
            Some(unambiguous_finding(
                item,
                Violation::UnsanctionedSelectorsOnly,
                render_singleton_with_links(
                    base,
                    presentation_selectors_after_base.first().copied(),
                    modifications,
                    trailing_links,
                ),
            ))
        }
        SingletonAnalysisOutcome::Repair {
            canonical_presentation,
            violation,
        } => Some(unambiguous_finding(
            item,
            // Link-attached selectors are orthogonal cleanup. They should not
            // hide a primary singleton/keycap violation in the wrapped emoji.
            if has_link_presentation_selectors {
                violation.with_unsanctioned_selectors()
            } else {
                violation
            },
            render_singleton_with_links(
                base,
                canonical_presentation,
                modifications,
                trailing_links,
            ),
        )),
        SingletonAnalysisOutcome::ResolvePresentation { default } if trailing_links.is_empty() => {
            Some(resolve_presentation_finding(
                item,
                base,
                modifications,
                default,
            ))
        }
        SingletonAnalysisOutcome::ResolvePresentation { default } => {
            Some(resolve_zwj_wrapper_presentation_finding(
                item,
                base,
                modifications,
                trailing_links,
                default,
                has_link_presentation_selectors,
            ))
        }
    }
}

fn analyze_multi_emoji_zwj_sequence<'a>(
    item: &ScanItem<'a>,
    first: &EmojiLike,
    joined: &[ZwjJoinedEmoji],
    trailing_links: &[ZwjLink],
    policy: &Policy,
) -> Option<Finding<'a>> {
    let mut builder = ReplacementPlanBuilder::new();
    let mut has_unsanctioned_selectors = false;
    let mut has_noncanonical_component = false;
    let mut has_resolution_slot = false;

    let first_outcome = zwj_component_outcome(first, policy);
    has_unsanctioned_selectors |= first_outcome.has_unsanctioned_selectors;
    has_noncanonical_component |= first_outcome.is_noncanonical;
    has_resolution_slot |= first_outcome.has_resolution_slot;
    builder.extend(first_outcome.pieces, first_outcome.slots);

    for joined in joined {
        if zwj_link_has_selectors(&joined.link) {
            has_unsanctioned_selectors = true;
        }
        builder.push_literal(unicode::ZWJ.to_string());

        let joined_outcome = zwj_component_outcome(&joined.emoji, policy);
        has_unsanctioned_selectors |= joined_outcome.has_unsanctioned_selectors;
        has_noncanonical_component |= joined_outcome.is_noncanonical;
        has_resolution_slot |= joined_outcome.has_resolution_slot;
        builder.extend(joined_outcome.pieces, joined_outcome.slots);
    }

    if trailing_links.iter().any(zwj_link_has_selectors) {
        has_unsanctioned_selectors = true;
    }
    for _ in trailing_links {
        builder.push_literal(unicode::ZWJ.to_string());
    }

    if !has_noncanonical_component && !has_unsanctioned_selectors {
        return None;
    }

    let violation = if has_resolution_slot {
        Violation::primary(
            PrimaryViolationKind::BareNeedsResolution,
            has_unsanctioned_selectors,
        )
    } else if has_noncanonical_component {
        Violation::primary(
            PrimaryViolationKind::NotFullyQualifiedSequence,
            has_unsanctioned_selectors,
        )
    } else {
        Violation::UnsanctionedSelectorsOnly
    };

    Some(finding_from_builder(item, violation, builder))
}

// --- ZWJ sequence analysis helpers ---

fn zwj_link_has_selectors(link: &ZwjLink) -> bool {
    !link.presentation_selectors_after_link.is_empty()
}

struct ZwjComponentOutcome {
    pieces: Vec<ComponentReplacementPiece>,
    slots: Vec<ReplacementSlotPlan>,
    is_noncanonical: bool,
    has_unsanctioned_selectors: bool,
    has_resolution_slot: bool,
}

enum ComponentReplacementPiece {
    Literal(String),
    Slot(usize),
}

fn zwj_component_outcome(emoji: &EmojiLike, policy: &Policy) -> ZwjComponentOutcome {
    match &emoji.stem {
        EmojiStem::SingletonBase {
            base,
            presentation_selectors_after_base,
        } => zwj_singleton_component_outcome(
            *base,
            presentation_selectors_after_base,
            &emoji.modifiers,
            policy,
        ),
        EmojiStem::Flag {
            first_ri,
            presentation_selectors_after_first_ri,
            second_ri,
            presentation_selectors_after_second_ri,
        } => {
            let has_unsanctioned_selectors = !presentation_selectors_after_first_ri.is_empty()
                || !presentation_selectors_after_second_ri.is_empty()
                || has_trailing_modification_presentation_selectors(&emoji.modifiers);
            ZwjComponentOutcome {
                pieces: vec![ComponentReplacementPiece::Literal(
                    FixedEmojiLike::flag(*first_ri, *second_ri, &emoji.modifiers)
                        .render_to_string(),
                )],
                slots: vec![],
                is_noncanonical: has_unsanctioned_selectors,
                has_unsanctioned_selectors,
                has_resolution_slot: false,
            }
        }
    }
}

fn zwj_singleton_component_outcome(
    base: char,
    presentation_selectors_after_base: &[Presentation],
    modifications: &[EmojiModification],
    policy: &Policy,
) -> ZwjComponentOutcome {
    match singleton_analysis_outcome(
        base,
        presentation_selectors_after_base,
        modifications,
        policy,
    ) {
        SingletonAnalysisOutcome::Canonical => {
            let presentation = presentation_selectors_after_base.first().copied();
            ZwjComponentOutcome {
                pieces: vec![ComponentReplacementPiece::Literal(render_singleton(
                    base,
                    presentation,
                    modifications,
                ))],
                slots: vec![],
                is_noncanonical: false,
                has_unsanctioned_selectors: false,
                has_resolution_slot: false,
            }
        }
        SingletonAnalysisOutcome::Repair {
            canonical_presentation,
            violation,
        } => ZwjComponentOutcome {
            pieces: vec![ComponentReplacementPiece::Literal(render_singleton(
                base,
                canonical_presentation,
                modifications,
            ))],
            slots: vec![],
            is_noncanonical: true,
            has_unsanctioned_selectors: matches!(violation, Violation::UnsanctionedSelectorsOnly)
                || matches!(
                    violation,
                    Violation::Primary(PrimaryViolation {
                        has_unsanctioned_selectors: true,
                        ..
                    })
                ),
            has_resolution_slot: false,
        },
        SingletonAnalysisOutcome::ResolvePresentation { default } => {
            let slot = presentation_slot(
                ReplacementDecision::from_presentation(default),
                [
                    (
                        ReplacementDecision::Text,
                        render_singleton(base, Some(Presentation::Text), modifications),
                    ),
                    (
                        ReplacementDecision::Emoji,
                        render_singleton(base, Some(Presentation::Emoji), modifications),
                    ),
                ],
            );
            ZwjComponentOutcome {
                pieces: vec![ComponentReplacementPiece::Slot(0)],
                slots: vec![slot],
                is_noncanonical: true,
                has_unsanctioned_selectors: false,
                has_resolution_slot: true,
            }
        }
    }
}

fn render_zwj_links_only_sequence(links: &[ZwjLink]) -> String {
    let mut out = String::new();
    render_zwj_links(&mut out, links);
    out
}

fn render_zwj_links(out: &mut String, links: &[ZwjLink]) {
    for _ in links {
        out.push(unicode::ZWJ);
    }
}

// --- Singleton analysis planning ---

#[derive(Clone, Copy)]
enum SingletonAnalysisOutcome {
    /// No finding is needed.
    Canonical,
    /// A deterministic repair is available.
    ///
    /// `canonical_presentation` is only the selector state for the base slot;
    /// rendering also preserves every modification after stripping their
    /// trailing presentation selectors.
    Repair {
        canonical_presentation: Option<Presentation>,
        violation: Violation,
    },
    /// A bare standalone slot remains policy-ambiguous and needs an explicit
    /// text/emoji choice from the caller.
    ResolvePresentation { default: Presentation },
}

/// Decide how a singleton-base emoji-like unit should be analyzed.
///
/// The fixed-cleanup precedence lives in
/// `docs/designs/features/sequence-handling.markdown`. Rule 5 is the only
/// path where policy may be consulted.
fn singleton_analysis_outcome(
    base: char,
    presentation_selectors_after_base: &[Presentation],
    modifications: &[EmojiModification],
    policy: &Policy,
) -> SingletonAnalysisOutcome {
    match modifications.first() {
        // Precedence 2: emoji modifiers force a bare base. Legacy FE0F before an
        // emoji modifier is removed.
        Some(EmojiModification::EmojiModifier { .. }) => {
            singleton_fixed_cleanup_outcome(presentation_selectors_after_base, modifications, None)
        }
        // Precedence 3: tag context forces emoji presentation for
        // non-emoji-default bases, subject to precedence 1's
        // sanctioned-presentation guard below.
        Some(EmojiModification::TagModifier(_)) => singleton_fixed_cleanup_outcome(
            presentation_selectors_after_base,
            modifications,
            if unicode::is_emoji_default(base) {
                None
            } else {
                sanctioned_presentation(base, Presentation::Emoji)
            },
        ),
        // Precedence 5: no fixed-cleanup context remains, so policy chooses the
        // canonical presentation. The policy analysis still rejects selector
        // choices unsupported by the base's variation-sequence data.
        first_modification => standalone_singleton_analysis_outcome(
            base,
            presentation_selectors_after_base,
            unicode::has_variation_sequence(base),
            matches!(
                first_modification,
                Some(EmojiModification::EnclosingKeycap { .. })
            ),
            policy,
        ),
    }
}

fn sanctioned_presentation(base: char, presentation: Presentation) -> Option<Presentation> {
    unicode::has_variation_sequence(base).then_some(presentation)
}

fn singleton_fixed_cleanup_outcome(
    presentation_selectors_after_base: &[Presentation],
    modifications: &[EmojiModification],
    canonical_presentation: Option<Presentation>,
) -> SingletonAnalysisOutcome {
    // Fixed cleanup is canonical only when both the base slot is already in the
    // selected presentation state and every modification is free of trailing
    // presentation selectors.
    if singleton_base_presentation_is_canonical(
        presentation_selectors_after_base,
        canonical_presentation,
    ) && !has_trailing_modification_presentation_selectors(modifications)
    {
        return SingletonAnalysisOutcome::Canonical;
    }

    SingletonAnalysisOutcome::Repair {
        canonical_presentation,
        violation: Violation::UnsanctionedSelectorsOnly,
    }
}

/// Whether the base presentation selectors already match the selected
/// canonical base presentation.
///
/// This intentionally ignores trailing selectors after modifications; those
/// are checked separately. Keeping the two checks separate avoids using a
/// rendered string as a proxy for canonicality.
fn singleton_base_presentation_is_canonical(
    presentation_selectors_after_base: &[Presentation],
    presentation: Option<Presentation>,
) -> bool {
    presentation_selectors_after_base == presentation.as_slice()
}

fn standalone_singleton_analysis_outcome(
    base: char,
    presentation_selectors_after_base: &[Presentation],
    has_sanctioned_presentation: bool,
    is_keycap: bool,
    policy: &Policy,
) -> SingletonAnalysisOutcome {
    // This function is entered only after fixed-cleanup rules have been
    // exhausted. It therefore handles a true standalone base slot: no
    // modifier, tag, or missing-variation keycap context can force a
    // presentation before policy.
    if !has_sanctioned_presentation {
        // Base has no sanctioned variation sequence data. Any attached
        // presentation selector is unsanctioned and gets removed; absence of
        // presentation selectors means there is nothing to repair.
        return if presentation_selectors_after_base.is_empty() {
            SingletonAnalysisOutcome::Canonical
        } else {
            SingletonAnalysisOutcome::Repair {
                canonical_presentation: None,
                violation: Violation::UnsanctionedSelectorsOnly,
            }
        };
    }

    match (
        policy.singleton_rule(base, is_keycap),
        presentation_selectors_after_base,
    ) {
        // More than one presentation selector where the first matches the
        // bare-side of the rule: the primary violation is the redundant first
        // selector, and the extras are unsanctioned selector cleanup.
        (SingletonRule::TextToBare, &[Presentation::Text, _, ..])
        | (SingletonRule::EmojiToBare, &[Presentation::Emoji, _, ..]) => {
            SingletonAnalysisOutcome::Repair {
                canonical_presentation: None,
                violation: Violation::primary(PrimaryViolationKind::RedundantSelector, true),
            }
        }
        // More than one presentation selector but the first is meaningful
        // under this rule: the first selector is canonical, so the only
        // violation is the unsanctioned selector cleanup after it.
        (_, &[current_presentation, _, ..]) => SingletonAnalysisOutcome::Repair {
            canonical_presentation: Some(current_presentation),
            violation: Violation::UnsanctionedSelectorsOnly,
        },
        // Bare stem under a rule that resolves bare to a concrete presentation:
        // let the caller decide between text and emoji.
        (SingletonRule::BareToEmoji, &[]) => SingletonAnalysisOutcome::ResolvePresentation {
            default: Presentation::Emoji,
        },
        (SingletonRule::BareToText, &[]) => SingletonAnalysisOutcome::ResolvePresentation {
            default: Presentation::Text,
        },
        // Exactly one presentation selector that matches the bare-side of the
        // rule: the presentation selector is redundant, drop it.
        (SingletonRule::TextToBare, &[Presentation::Text])
        | (SingletonRule::EmojiToBare, &[Presentation::Emoji]) => {
            SingletonAnalysisOutcome::Repair {
                canonical_presentation: None,
                violation: Violation::primary(PrimaryViolationKind::RedundantSelector, false),
            }
        }
        // All remaining single-presentation-selector and no-presentation-selector
        // cases are already canonical under the active rule.
        (SingletonRule::TextToBare, &[Presentation::Emoji] | &[])
        | (SingletonRule::EmojiToBare, &[Presentation::Text] | &[])
        | (SingletonRule::BareToText | SingletonRule::BareToEmoji, &[_]) => {
            SingletonAnalysisOutcome::Canonical
        }
    }
}

// --- Shared selector predicates ---

fn modification_has_trailing_presentation_selectors(m: &EmojiModification) -> bool {
    match m {
        EmojiModification::EmojiModifier {
            presentation_selectors_after_modifier,
            ..
        } => !presentation_selectors_after_modifier.is_empty(),
        EmojiModification::EnclosingKeycap {
            presentation_selectors_after_keycap,
        } => !presentation_selectors_after_keycap.is_empty(),
        EmojiModification::TagModifier(runs) => runs
            .iter()
            .any(|r| !r.presentation_selectors_after_tag.is_empty()),
    }
}

fn has_trailing_modification_presentation_selectors(modifications: &[EmojiModification]) -> bool {
    modifications
        .iter()
        .any(modification_has_trailing_presentation_selectors)
}

// --- Finding builders ---

fn unambiguous_finding<'a>(
    item: &ScanItem<'a>,
    violation: Violation,
    fix_replacement: String,
) -> Finding<'a> {
    let mut builder = ReplacementPlanBuilder::new();
    builder.push_literal(fix_replacement);
    finding_from_builder(item, violation, builder)
}

fn resolve_presentation_finding<'a>(
    item: &ScanItem<'a>,
    base: char,
    modifications: &[EmojiModification],
    default: Presentation,
) -> Finding<'a> {
    let mut builder = ReplacementPlanBuilder::new();
    builder.push_slot(presentation_slot(
        ReplacementDecision::from_presentation(default),
        [
            (
                ReplacementDecision::Text,
                render_singleton(base, Some(Presentation::Text), modifications),
            ),
            (
                ReplacementDecision::Emoji,
                render_singleton(base, Some(Presentation::Emoji), modifications),
            ),
        ],
    ));
    finding_from_builder(
        item,
        Violation::primary(PrimaryViolationKind::BareNeedsResolution, false),
        builder,
    )
}

fn resolve_zwj_wrapper_presentation_finding<'a>(
    item: &ScanItem<'a>,
    base: char,
    modifications: &[EmojiModification],
    trailing_links: &[ZwjLink],
    default: Presentation,
    has_unsanctioned_selectors: bool,
) -> Finding<'a> {
    debug_assert!(
        !trailing_links.is_empty(),
        "ZWJ wrapper presentation resolution requires at least one trailing link"
    );
    let mut builder = ReplacementPlanBuilder::new();
    builder.push_slot(presentation_slot(
        ReplacementDecision::from_presentation(default),
        [
            (
                ReplacementDecision::Text,
                render_singleton_with_links(
                    base,
                    Some(Presentation::Text),
                    modifications,
                    trailing_links,
                ),
            ),
            (
                ReplacementDecision::Emoji,
                render_singleton_with_links(
                    base,
                    Some(Presentation::Emoji),
                    modifications,
                    trailing_links,
                ),
            ),
        ],
    ));
    finding_from_builder(
        item,
        Violation::primary(
            PrimaryViolationKind::BareNeedsResolution,
            has_unsanctioned_selectors,
        ),
        builder,
    )
}

fn presentation_slot<const N: usize>(
    default: ReplacementDecision,
    replacements: [(ReplacementDecision, String); N],
) -> ReplacementSlotPlan {
    let replacements: Vec<_> = replacements
        .into_iter()
        .map(|(decision, replacement)| SlotReplacement {
            decision,
            replacement,
        })
        .collect();
    let choices = replacements
        .iter()
        .map(|replacement| replacement.decision)
        .collect();
    ReplacementSlotPlan {
        public: DecisionSlot { choices, default },
        replacements,
    }
}

struct ReplacementPlanBuilder {
    slots: Vec<ReplacementSlotPlan>,
    pieces: Vec<ReplacementPiece>,
}

impl ReplacementPlanBuilder {
    const fn new() -> Self {
        Self {
            slots: Vec::new(),
            pieces: Vec::new(),
        }
    }

    fn push_literal(&mut self, literal: String) {
        self.pieces.push(ReplacementPiece::Literal(literal));
    }

    fn push_slot(&mut self, slot: ReplacementSlotPlan) {
        let slot_index = self.slots.len();
        self.slots.push(slot);
        self.pieces.push(ReplacementPiece::Slot(slot_index));
    }

    fn extend(&mut self, pieces: Vec<ComponentReplacementPiece>, slots: Vec<ReplacementSlotPlan>) {
        let slot_offset = self.slots.len();
        self.slots.extend(slots);
        self.pieces
            .extend(pieces.into_iter().map(|piece| match piece {
                ComponentReplacementPiece::Literal(literal) => ReplacementPiece::Literal(literal),
                ComponentReplacementPiece::Slot(slot_index) => {
                    ReplacementPiece::Slot(slot_offset + slot_index)
                }
            }));
    }

    fn build(self, violation: Violation) -> ReplacementPlan {
        let default_replacement = render_default_replacement(&self.slots, &self.pieces);
        let plan = ReplacementPlan {
            violation,
            decision_slots: self.slots.iter().map(|slot| slot.public.clone()).collect(),
            slots: self.slots,
            pieces: self.pieces,
            default_replacement,
        };
        debug_assert_eq!(
            plan.render_replacement(&plan.default_decision()),
            Some(plan.default_replacement.clone())
        );
        plan
    }
}

fn render_default_replacement(
    slots: &[ReplacementSlotPlan],
    pieces: &[ReplacementPiece],
) -> String {
    let mut out = String::new();
    for piece in pieces {
        match piece {
            ReplacementPiece::Literal(text) => out.push_str(text),
            ReplacementPiece::Slot(slot_index) => {
                if let Some(slot) = slots.get(*slot_index)
                    && let Some(replacement) = slot.replacement(slot.public.default_decision())
                {
                    out.push_str(replacement);
                }
            }
        }
    }
    out
}

fn finding_from_builder<'a>(
    item: &ScanItem<'a>,
    violation: Violation,
    builder: ReplacementPlanBuilder,
) -> Finding<'a> {
    Finding {
        span: item.span.clone(),
        raw: item.raw,
        replacement_plan: builder.build(violation),
    }
}

// --- Fixed emoji-like rendering ---

fn render_singleton(
    base: char,
    presentation: Option<Presentation>,
    modifications: &[EmojiModification],
) -> String {
    FixedEmojiLike::singleton_base(base, presentation, modifications).render_to_string()
}

fn render_singleton_with_links(
    base: char,
    presentation: Option<Presentation>,
    modifications: &[EmojiModification],
    trailing_links: &[ZwjLink],
) -> String {
    let mut out =
        FixedEmojiLike::singleton_base(base, presentation, modifications).render_to_string();
    render_zwj_links(&mut out, trailing_links);
    out
}

fn render_flag_with_links(
    first_ri: char,
    second_ri: char,
    modifications: &[EmojiModification],
    trailing_links: &[ZwjLink],
) -> String {
    let mut out = FixedEmojiLike::flag(first_ri, second_ri, modifications).render_to_string();
    render_zwj_links(&mut out, trailing_links);
    out
}