evfmt 0.3.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
use std::ops::{Add, AddAssign, Range};
use std::slice;

use crate::presentation::Presentation;

/// Count summary for why a scanned item is non-canonical.
///
/// These axes are compositional rather than mutually exclusive. A finding may
/// simultaneously include unsanctioned selectors, modifier-related defective
/// selectors, tag-context selector cleanup, policy-redundant selectors, and
/// policy-driven presentation decisions.
///
/// The categories describe how non-canonical selector usage is repaired or
/// exposed to callers:
///
/// - unsanctioned selector usage is removed
/// - sanctioned modifier-defective selectors are removed when a base with
///   `Emoji_Modifier_Base` precedes an emoji modifier
/// - additional defective selectors are sanctioned selectors removed by the
///   formatter's narrow extension to variation-sequence emoji bases without
///   `Emoji_Modifier_Base`
/// - an intervening selector on a base without a sanctioned variation
///   sequence is removed as unsanctioned selector usage
/// - tag-context selectors are removed, replaced, or supplied by tag-specific
///   cleanup
/// - policy-redundant selectors are removed when the active policy prefers the
///   bare form
/// - ambiguous selector slots become caller-resolvable presentation decisions
///
/// Tag-context counters are separate because tag-sequence presentation has a
/// different Unicode history from ordinary policy resolution: current UTS #51
/// admits broad base-and-tag spellings, while RGI tag sequences deliberately
/// use an emoji-default base.
///
/// The scalar-length effect of a finding's default canonical replacement is
/// derived from these counters. Each current category changes the default
/// replacement by one presentation-selector scalar per count:
///
/// ```text
/// replacement.chars().count() - raw.chars().count()
///   = tag_forced_presentations + presentation_decisions
///   - unsanctioned_selectors - modifier_defective_selectors
///   - additional_defective_selectors - tag_conflicting_selectors
///   - tag_redundant_selectors - policy_redundant_selectors
/// ```
///
/// # Examples
///
/// ```rust
/// use evfmt::{Policy, scan};
/// use evfmt::analysis::analyze_scan_item;
///
/// assert!(evfmt::NonCanonicality::default().is_empty());
///
/// let policy = Policy::default();
/// let finding = scan("A\u{FE0F}")
///     .find_map(|item| analyze_scan_item(&item, &policy))
///     .unwrap();
///
/// let non_canonicality = finding.non_canonicality();
/// assert!(!non_canonicality.is_empty());
/// assert_eq!(non_canonicality.unsanctioned_selectors, 1);
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub struct NonCanonicality {
    /// Count of presentation selectors removed as unsanctioned selector usage.
    pub unsanctioned_selectors: usize,
    /// Count of sanctioned `FE0F` selectors removed in the UTS #51 defective
    /// modifier context where an `Emoji_Modifier_Base` character precedes an
    /// `Emoji_Modifier`.
    ///
    /// An intervening `FE0F` on an `Emoji_Modifier_Base` character without a
    /// sanctioned variation sequence counts as [`Self::unsanctioned_selectors`].
    pub modifier_defective_selectors: usize,
    /// Count of sanctioned `FE0F` selectors removed before an emoji modifier
    /// when the recognized variation-sequence emoji base lacks
    /// `Emoji_Modifier_Base`.
    ///
    /// Earlier UTS #51 revisions admitted an intervening `FE0F` only through a
    /// sanctioned variation sequence on an `Emoji_Modifier_Base`. `evfmt`
    /// records sanctioned selectors on other recognized emoji bases in this
    /// counter.
    pub additional_defective_selectors: usize,
    /// Count of sanctioned tag-context selectors whose requested presentation
    /// differs from the canonical base presentation in tag context.
    pub tag_conflicting_selectors: usize,
    /// Count of tag-context selector slots where canonical output supplies
    /// emoji presentation that was not already present in the source.
    pub tag_forced_presentations: usize,
    /// Count of sanctioned tag-context selectors whose requested emoji
    /// presentation is already carried by the canonical bare base in tag
    /// context.
    pub tag_redundant_selectors: usize,
    /// Count of sanctioned selectors dropped because the active policy chooses
    /// bare form as canonical.
    pub policy_redundant_selectors: usize,
    /// Count of policy presentation decisions callers may resolve.
    pub presentation_decisions: usize,
}

impl Default for NonCanonicality {
    fn default() -> Self {
        Self::EMPTY
    }
}

impl NonCanonicality {
    const EMPTY: Self = Self {
        unsanctioned_selectors: 0,
        modifier_defective_selectors: 0,
        additional_defective_selectors: 0,
        tag_conflicting_selectors: 0,
        tag_forced_presentations: 0,
        tag_redundant_selectors: 0,
        policy_redundant_selectors: 0,
        presentation_decisions: 0,
    };

    pub(super) const MODIFIER_DEFECTIVE_SELECTOR: Self = Self {
        modifier_defective_selectors: 1,
        ..Self::EMPTY
    };
    pub(super) const ADDITIONAL_DEFECTIVE_SELECTOR: Self = Self {
        additional_defective_selectors: 1,
        ..Self::EMPTY
    };
    pub(super) const TAG_CONFLICTING_SELECTOR: Self = Self {
        tag_conflicting_selectors: 1,
        ..Self::EMPTY
    };
    pub(super) const TAG_FORCED_PRESENTATION: Self = Self {
        tag_forced_presentations: 1,
        ..Self::EMPTY
    };
    pub(super) const TAG_REDUNDANT_SELECTOR: Self = Self {
        tag_redundant_selectors: 1,
        ..Self::EMPTY
    };
    pub(super) const POLICY_REDUNDANT_SELECTOR: Self = Self {
        policy_redundant_selectors: 1,
        ..Self::EMPTY
    };
    pub(super) const PRESENTATION_DECISION: Self = Self {
        presentation_decisions: 1,
        ..Self::EMPTY
    };

    pub(super) const fn unsanctioned(count: usize) -> Self {
        Self {
            unsanctioned_selectors: count,
            ..Self::EMPTY
        }
    }

    /// Return whether every non-canonicality count is zero.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        // Keep the pattern exhaustive so new accounting axes must be handled
        // here while this method remains usable in const contexts.
        matches!(
            self,
            Self {
                unsanctioned_selectors: 0,
                modifier_defective_selectors: 0,
                additional_defective_selectors: 0,
                tag_conflicting_selectors: 0,
                tag_forced_presentations: 0,
                tag_redundant_selectors: 0,
                policy_redundant_selectors: 0,
                presentation_decisions: 0,
            }
        )
    }
}

impl Add for NonCanonicality {
    type Output = Self;

    fn add(self, rhs: Self) -> Self::Output {
        Self {
            unsanctioned_selectors: self.unsanctioned_selectors + rhs.unsanctioned_selectors,
            modifier_defective_selectors: self.modifier_defective_selectors
                + rhs.modifier_defective_selectors,
            additional_defective_selectors: self.additional_defective_selectors
                + rhs.additional_defective_selectors,
            tag_conflicting_selectors: self.tag_conflicting_selectors
                + rhs.tag_conflicting_selectors,
            tag_forced_presentations: self.tag_forced_presentations + rhs.tag_forced_presentations,
            tag_redundant_selectors: self.tag_redundant_selectors + rhs.tag_redundant_selectors,
            policy_redundant_selectors: self.policy_redundant_selectors
                + rhs.policy_redundant_selectors,
            presentation_decisions: self.presentation_decisions + rhs.presentation_decisions,
        }
    }
}

impl AddAssign for NonCanonicality {
    fn add_assign(&mut self, rhs: Self) {
        *self = *self + rhs;
    }
}

/// One fixed or caller-selectable replacement assembly piece.
///
/// These elements are a private renderer representation after the semantic
/// formatter model has already resolved selector contexts and policy
/// keys. They should not be treated as the design-spec vocabulary for
/// selector classification; see `docs/designs/core/formatting.markdown`
/// for that model.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) enum ReplacementElement<D> {
    Fixed(String),
    Choice(ReplacementChoice<D>),
}

/// One internally caller-selectable replacement assembly option.
///
/// Each valid decision selects a complete replacement string for this assembly
/// piece. That string may include surrounding characters needed to keep the
/// whole finding renderable after local cleanup.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ReplacementChoice<D> {
    pub(super) default: D,
    pub(super) options: Vec<ReplacementOption<D>>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ReplacementOption<D> {
    pub(super) decision: D,
    pub(super) replacement: String,
}

impl<D: Copy> ReplacementChoice<D> {
    pub(super) const fn default_decision(&self) -> D {
        self.default
    }
}

impl<D: PartialEq> ReplacementChoice<D> {
    pub(super) fn new(default: D, options: Vec<ReplacementOption<D>>) -> Self {
        assert!(
            options.iter().any(|option| option.decision == default),
            "replacement choice default decision must be one of the options"
        );
        Self { default, options }
    }

    pub(super) fn from_replacements<const N: usize>(
        default: D,
        replacements: [(D, String); N],
    ) -> Self {
        Self::new(
            default,
            replacements
                .into_iter()
                .map(|(decision, replacement)| ReplacementOption {
                    decision,
                    replacement,
                })
                .collect(),
        )
    }

    pub(super) fn replacement(&self, decision: &D) -> Option<&str> {
        self.options
            .iter()
            .find(|option| option.decision == *decision)
            .map(|option| option.replacement.as_str())
    }

    #[allow(clippy::expect_used)] // ReplacementChoice::new validates that the default decision is one of the options.
    fn default_canonical_replacement(&self) -> &str {
        self.replacement(&self.default)
            .expect("replacement choice constructor validates its default decision")
    }
}

/// Replacement assembly and non-canonicality accounting before source location
/// is attached.
#[derive(Debug, Clone, PartialEq, Eq)]
pub(super) struct ReplacementAnalysis {
    non_canonicality: NonCanonicality,
    elements: Vec<ReplacementElement<Presentation>>,
}

impl ReplacementAnalysis {
    pub(super) fn empty() -> Self {
        Self {
            non_canonicality: NonCanonicality::default(),
            elements: Vec::new(),
        }
    }

    pub(super) fn fixed(non_canonicality: NonCanonicality, replacement: String) -> Self {
        let mut analysis = Self {
            non_canonicality,
            elements: Vec::new(),
        };
        analysis.push_fixed(replacement);
        analysis
    }

    pub(super) fn choice(
        non_canonicality: NonCanonicality,
        choice: ReplacementChoice<Presentation>,
    ) -> Self {
        Self {
            non_canonicality,
            elements: vec![ReplacementElement::Choice(choice)],
        }
    }

    /// Whether this assembled analysis would leave the scanned item canonical.
    ///
    /// This is determined only by the non-canonicality counters. Replacement
    /// elements can still be present because sequence-level analysis may need
    /// them to preserve surrounding structure when another part of the same
    /// item is non-canonical.
    pub(super) const fn is_canonical(&self) -> bool {
        self.non_canonicality.is_empty()
    }

    fn decision_count(&self) -> usize {
        self.elements
            .iter()
            .filter(|element| matches!(element, ReplacementElement::Choice(_)))
            .count()
    }

    pub(super) fn push_fixed(&mut self, text: String) {
        self.elements.push(ReplacementElement::Fixed(text));
    }
}

impl AddAssign for ReplacementAnalysis {
    fn add_assign(&mut self, rhs: Self) {
        self.non_canonicality += rhs.non_canonicality;
        self.elements.extend(rhs.elements);
    }
}

/// A single non-canonical scanned item with its valid replacement decisions.
///
/// `Finding` values are returned only for items that are non-canonical under
/// the policy passed to [`crate::analysis::analyze_scan_item`]. Their
/// [`NonCanonicality`] is guaranteed to be non-empty.
///
/// # Examples
///
/// ```rust
/// use evfmt::{Policy, Presentation, scan};
/// use evfmt::analysis::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_canonical_replacement(), "\u{00A9}\u{FE0E}");
/// assert_eq!(
///     finding.canonical_replacement_with_decisions(&[Presentation::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,
    analysis: ReplacementAnalysis,
}

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

    /// The decision vector formatting applies to this finding by default.
    ///
    /// Each presentation decision is one ambiguous selector slot in source order
    /// within the scanned item. Every current decision accepts
    /// [`Presentation::Text`] or [`Presentation::Emoji`]. Fixed cleanup
    /// contributes no presentation decision.
    ///
    /// The iterator length is equal to
    /// [`NonCanonicality::presentation_decisions`] for this finding.
    #[must_use]
    pub fn default_decisions(&self) -> impl ExactSizeIterator<Item = Presentation> + '_ {
        DefaultDecisions {
            elements: self.analysis.elements.iter(),
            remaining: self.analysis.decision_count(),
        }
    }

    /// Return the canonical replacement selected by this finding's default
    /// decision vector.
    ///
    /// Use this when accepting evfmt's default repair for the whole finding.
    /// Unlike [`Finding::canonical_replacement_with_decisions`], this method
    /// cannot fail because the decisions come from the finding itself.
    #[must_use]
    pub fn default_canonical_replacement(&self) -> String {
        let mut out = String::new();
        for element in &self.analysis.elements {
            match element {
                ReplacementElement::Fixed(text) => out.push_str(text),
                ReplacementElement::Choice(choice) => {
                    out.push_str(choice.default_canonical_replacement());
                }
            }
        }
        out
    }

    /// Return the canonical whole-item replacement for a valid decision vector.
    ///
    /// Each presentation decision is one ambiguous selector slot in source order
    /// within the scanned item. Every current decision accepts
    /// [`Presentation::Text`] or [`Presentation::Emoji`]. Fixed cleanup is
    /// included in the whole replacement but contributes no presentation decision.
    ///
    /// Returns `None` when the decision vector is invalid. Because the current
    /// API accepts both presentation decisions at every slot, the only invalid
    /// decision vectors are those with the wrong length. That `None` reports
    /// invalid caller input; it does not mean this finding is canonical.
    /// Callers that want to skip a finding can keep [`Finding::raw`].
    #[must_use]
    pub fn canonical_replacement_with_decisions(
        &self,
        decisions: &[Presentation],
    ) -> Option<String> {
        let mut decisions = decisions.iter();
        let mut out = String::new();

        for element in &self.analysis.elements {
            match element {
                ReplacementElement::Fixed(text) => out.push_str(text),
                ReplacementElement::Choice(choice) => {
                    let decision = decisions.next()?;
                    out.push_str(choice.replacement(decision)?);
                }
            }
        }

        if decisions.next().is_none() {
            Some(out)
        } else {
            None
        }
    }
}

struct DefaultDecisions<'a> {
    elements: slice::Iter<'a, ReplacementElement<Presentation>>,
    remaining: usize,
}

impl Iterator for DefaultDecisions<'_> {
    type Item = Presentation;

    fn next(&mut self) -> Option<Self::Item> {
        for element in self.elements.by_ref() {
            if let ReplacementElement::Choice(choice) = element {
                self.remaining -= 1;
                return Some(choice.default_decision());
            }
        }
        None
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        (self.remaining, Some(self.remaining))
    }
}

impl ExactSizeIterator for DefaultDecisions<'_> {
    fn len(&self) -> usize {
        self.remaining
    }
}

impl<'a> Finding<'a> {
    pub(super) fn new(item: &crate::scanner::ScanItem<'a>, analysis: ReplacementAnalysis) -> Self {
        assert!(
            !analysis.is_canonical(),
            "finding construction requires non-empty non-canonicality"
        );
        assert_eq!(
            analysis.decision_count(),
            analysis.non_canonicality.presentation_decisions,
            "finding decision count must match presentation_decisions"
        );
        Self {
            span: item.span.clone(),
            raw: item.raw,
            analysis,
        }
    }

    pub(super) fn fixed(
        item: &crate::scanner::ScanItem<'a>,
        non_canonicality: NonCanonicality,
        replacement: String,
    ) -> Self {
        Self::new(
            item,
            ReplacementAnalysis::fixed(non_canonicality, replacement),
        )
    }
}