ferrocat-icu 0.13.0

Compact, Rust-native ICU MessageFormat parsing primitives for ferrocat.
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
use std::collections::{BTreeMap, BTreeSet};

use crate::ast::{IcuMessage, IcuNode, IcuOption, IcuPluralKind};

/// Severity level attached to ICU authoring diagnostics.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IcuDiagnosticSeverity {
    /// Informational message that does not indicate a problem.
    Info,
    /// Non-fatal condition that may require author or translator attention.
    Warning,
    /// Structural incompatibility that can break runtime formatting.
    Error,
}

/// Broad role of an ICU argument in a parsed message.
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
pub enum IcuArgumentKind {
    /// Simple substitution such as `{name}`.
    Argument,
    /// Number formatter such as `{count, number}`.
    Number,
    /// Date formatter such as `{created, date}`.
    Date,
    /// Time formatter such as `{created, time}`.
    Time,
    /// List formatter such as `{items, list}`.
    List,
    /// Duration formatter such as `{elapsed, duration}`.
    Duration,
    /// Relative-time "ago" formatter.
    Ago,
    /// Name formatter.
    Name,
    /// Select expression.
    Select,
    /// Cardinal plural expression.
    Plural,
    /// Ordinal plural expression.
    SelectOrdinal,
}

/// Classification of an optional formatter style segment.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IcuStyleKind {
    /// Formatter has no style segment.
    None,
    /// Formatter uses a known named style.
    Predefined,
    /// Formatter uses an ICU skeleton, identified by the `::` prefix.
    Skeleton,
    /// Formatter uses an opaque pattern-style segment.
    Pattern,
}

/// One data argument reference discovered in an ICU message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IcuArgument {
    /// Argument name referenced by the message.
    pub name: String,
    /// Structural role of the argument.
    pub kind: IcuArgumentKind,
}

/// One formatter reference discovered in an ICU message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IcuFormatter {
    /// Argument name passed to the formatter.
    pub name: String,
    /// Formatter kind.
    pub kind: IcuArgumentKind,
    /// Raw optional style segment.
    pub style: Option<String>,
    /// Classification of the raw style segment.
    pub style_kind: IcuStyleKind,
}

/// Consumer-defined support decision for an ICU formatter.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum IcuFormatterSupport {
    /// The formatter kind and style are supported by the consumer.
    Supported,
    /// The formatter kind is not supported by the consumer.
    UnsupportedKind {
        /// Severity to attach to the emitted diagnostic.
        severity: IcuDiagnosticSeverity,
    },
    /// The formatter style is not supported by the consumer.
    UnsupportedStyle {
        /// Severity to attach to the emitted diagnostic.
        severity: IcuDiagnosticSeverity,
    },
}

/// One select expression discovered in an ICU message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IcuSelectSummary {
    /// Argument name used for selection.
    pub name: String,
    /// Selector labels in source order.
    pub selectors: Vec<String>,
}

/// One cardinal or ordinal plural expression discovered in an ICU message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IcuPluralSummary {
    /// Argument name used for plural selection.
    pub name: String,
    /// Whether the plural expression is cardinal or ordinal.
    pub kind: IcuPluralKind,
    /// Parsed plural offset.
    pub offset: u32,
    /// Selector labels in source order.
    pub selectors: Vec<String>,
}

/// One rich-text tag discovered in an ICU message.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IcuTagSummary {
    /// Tag name without angle brackets.
    pub name: String,
}

/// Structural summary of a parsed ICU message.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct IcuAnalysis {
    /// Data arguments referenced by the message, excluding rich-text tags.
    pub arguments: Vec<IcuArgument>,
    /// Formatter arguments referenced by the message.
    pub formatters: Vec<IcuFormatter>,
    /// Cardinal and ordinal plural expressions.
    pub plurals: Vec<IcuPluralSummary>,
    /// Select expressions.
    pub selects: Vec<IcuSelectSummary>,
    /// Rich-text tags.
    pub tags: Vec<IcuTagSummary>,
}

/// Options controlling ICU source/translation compatibility checks.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IcuCompatibilityOptions {
    /// Whether translation-only data arguments should be reported.
    pub report_extra_arguments: bool,
    /// Whether translation-only rich-text tags should be reported.
    pub report_extra_tags: bool,
    /// Whether translation-only select selectors should be reported.
    pub report_extra_selectors: bool,
    /// Whether opaque number/date/time pattern styles should be reported.
    pub report_pattern_styles: bool,
}

impl Default for IcuCompatibilityOptions {
    fn default() -> Self {
        Self {
            report_extra_arguments: true,
            report_extra_tags: true,
            report_extra_selectors: true,
            report_pattern_styles: true,
        }
    }
}

/// One diagnostic emitted by an ICU compatibility check.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct IcuDiagnostic {
    /// Severity for the diagnostic.
    pub severity: IcuDiagnosticSeverity,
    /// Stable machine-readable diagnostic code.
    pub code: String,
    /// Human-readable explanation of the condition.
    pub message: String,
    /// Argument, selector, or tag name associated with the diagnostic.
    pub name: Option<String>,
}

impl IcuDiagnostic {
    fn new(
        severity: IcuDiagnosticSeverity,
        code: &'static str,
        message: impl Into<String>,
        name: impl Into<Option<String>>,
    ) -> Self {
        Self {
            severity,
            code: code.to_owned(),
            message: message.into(),
            name: name.into(),
        }
    }
}

/// Report returned by [`compare_icu_messages`].
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct IcuCompatibilityReport {
    /// Diagnostics found while comparing source and translation.
    pub diagnostics: Vec<IcuDiagnostic>,
}

impl IcuCompatibilityReport {
    /// Returns `true` when the report contains at least one error diagnostic.
    #[must_use]
    pub fn has_errors(&self) -> bool {
        self.diagnostics
            .iter()
            .any(|diagnostic| diagnostic.severity == IcuDiagnosticSeverity::Error)
    }
}

/// Produces a structural summary of a parsed ICU message.
#[must_use]
pub fn analyze_icu(message: &IcuMessage) -> IcuAnalysis {
    let mut analysis = IcuAnalysis::default();
    visit_nodes(&message.nodes, &mut analysis);
    analysis
}

/// Extracts data argument names in first-seen order, excluding rich-text tags.
#[must_use]
pub fn extract_argument_names(message: &IcuMessage) -> Vec<String> {
    unique_names(analyze_icu(message).arguments.iter().map(|arg| &arg.name))
}

/// Extracts rich-text tag names in first-seen order.
#[must_use]
pub fn extract_tag_names(message: &IcuMessage) -> Vec<String> {
    unique_names(analyze_icu(message).tags.iter().map(|tag| &tag.name))
}

/// Validates ICU formatter usage against a consumer-provided support policy.
///
/// The support callback receives each formatter discovered by [`analyze_icu`]
/// and decides whether the formatter is supported, has an unsupported kind, or
/// has an unsupported style. This keeps runtime-specific support policy outside
/// of `ferrocat-icu` while standardizing emitted diagnostics.
#[must_use]
pub fn validate_icu_formatter_support(
    message: &IcuMessage,
    mut support: impl FnMut(&IcuFormatter) -> IcuFormatterSupport,
) -> IcuCompatibilityReport {
    let analysis = analyze_icu(message);
    let mut report = IcuCompatibilityReport::default();

    for formatter in &analysis.formatters {
        match support(formatter) {
            IcuFormatterSupport::Supported => {}
            IcuFormatterSupport::UnsupportedKind { severity } => {
                report.diagnostics.push(IcuDiagnostic::new(
                    severity,
                    "icu.unsupported_formatter_kind",
                    format!(
                        "ICU formatter `{}` uses unsupported formatter kind {:?}.",
                        formatter.name, formatter.kind
                    ),
                    Some(formatter.name.clone()),
                ));
            }
            IcuFormatterSupport::UnsupportedStyle { severity } => {
                report.diagnostics.push(IcuDiagnostic::new(
                    severity,
                    "icu.unsupported_formatter_style",
                    format!(
                        "ICU formatter `{}` uses unsupported {:?} formatter style `{}`.",
                        formatter.name,
                        formatter.kind,
                        formatter_style_label(formatter.style.as_deref())
                    ),
                    Some(formatter.name.clone()),
                ));
            }
        }
    }

    report
}

/// Compares source and translation ICU messages for authoring compatibility.
#[must_use]
pub fn compare_icu_messages(
    source: &IcuMessage,
    translation: &IcuMessage,
    options: &IcuCompatibilityOptions,
) -> IcuCompatibilityReport {
    let source = analyze_icu(source);
    let translation = analyze_icu(translation);
    let mut report = IcuCompatibilityReport::default();

    compare_arguments(&source, &translation, options, &mut report);
    compare_formatter_styles(&source, &translation, &mut report);
    compare_tags(&source, &translation, options, &mut report);
    compare_selects(&source, &translation, options, &mut report);
    compare_plurals(&source, &translation, &mut report);
    if options.report_pattern_styles {
        report_pattern_styles(&source, "source", &mut report);
        report_pattern_styles(&translation, "translation", &mut report);
    }

    report
}

fn visit_nodes(nodes: &[IcuNode], analysis: &mut IcuAnalysis) {
    for node in nodes {
        match node {
            IcuNode::Literal(_) | IcuNode::Pound => {}
            IcuNode::Argument { name } => {
                push_argument(analysis, name, IcuArgumentKind::Argument);
            }
            IcuNode::Number { name, style } => {
                push_formatter(analysis, name, IcuArgumentKind::Number, style);
            }
            IcuNode::Date { name, style } => {
                push_formatter(analysis, name, IcuArgumentKind::Date, style);
            }
            IcuNode::Time { name, style } => {
                push_formatter(analysis, name, IcuArgumentKind::Time, style);
            }
            IcuNode::List { name, style } => {
                push_formatter(analysis, name, IcuArgumentKind::List, style);
            }
            IcuNode::Duration { name, style } => {
                push_formatter(analysis, name, IcuArgumentKind::Duration, style);
            }
            IcuNode::Ago { name, style } => {
                push_formatter(analysis, name, IcuArgumentKind::Ago, style);
            }
            IcuNode::Name { name, style } => {
                push_formatter(analysis, name, IcuArgumentKind::Name, style);
            }
            IcuNode::Select { name, options } => {
                push_argument(analysis, name, IcuArgumentKind::Select);
                analysis.selects.push(IcuSelectSummary {
                    name: name.clone(),
                    selectors: selectors(options),
                });
                visit_options(options, analysis);
            }
            IcuNode::Plural {
                name,
                kind,
                offset,
                options,
            } => {
                let argument_kind = match kind {
                    IcuPluralKind::Cardinal => IcuArgumentKind::Plural,
                    IcuPluralKind::Ordinal => IcuArgumentKind::SelectOrdinal,
                };
                push_argument(analysis, name, argument_kind);
                analysis.plurals.push(IcuPluralSummary {
                    name: name.clone(),
                    kind: kind.clone(),
                    offset: *offset,
                    selectors: selectors(options),
                });
                visit_options(options, analysis);
            }
            IcuNode::Tag { name, children } => {
                analysis.tags.push(IcuTagSummary { name: name.clone() });
                visit_nodes(children, analysis);
            }
        }
    }
}

fn visit_options(options: &[IcuOption], analysis: &mut IcuAnalysis) {
    for option in options {
        visit_nodes(&option.value, analysis);
    }
}

fn push_argument(analysis: &mut IcuAnalysis, name: &str, kind: IcuArgumentKind) {
    analysis.arguments.push(IcuArgument {
        name: name.to_owned(),
        kind,
    });
}

fn push_formatter(
    analysis: &mut IcuAnalysis,
    name: &str,
    kind: IcuArgumentKind,
    style: &Option<String>,
) {
    push_argument(analysis, name, kind);
    analysis.formatters.push(IcuFormatter {
        name: name.to_owned(),
        kind,
        style: style.clone(),
        style_kind: classify_style(kind, style.as_deref()),
    });
}

fn selectors(options: &[IcuOption]) -> Vec<String> {
    options
        .iter()
        .map(|option| option.selector.clone())
        .collect()
}

fn classify_style(kind: IcuArgumentKind, style: Option<&str>) -> IcuStyleKind {
    let Some(style) = style.map(str::trim).filter(|style| !style.is_empty()) else {
        return IcuStyleKind::None;
    };
    if style.starts_with("::") {
        return IcuStyleKind::Skeleton;
    }
    if is_predefined_style(kind, style) {
        return IcuStyleKind::Predefined;
    }
    IcuStyleKind::Pattern
}

fn is_predefined_style(kind: IcuArgumentKind, style: &str) -> bool {
    match kind {
        IcuArgumentKind::Number => matches!(style, "integer" | "currency" | "percent"),
        IcuArgumentKind::Date | IcuArgumentKind::Time => {
            matches!(style, "short" | "medium" | "long" | "full")
        }
        IcuArgumentKind::List => matches!(style, "conjunction" | "disjunction" | "unit"),
        _ => false,
    }
}

fn unique_names<'a>(names: impl IntoIterator<Item = &'a String>) -> Vec<String> {
    let mut seen = BTreeSet::new();
    let mut out = Vec::new();
    for name in names {
        if seen.insert(name.clone()) {
            out.push(name.clone());
        }
    }
    out
}

fn argument_map(analysis: &IcuAnalysis) -> BTreeMap<String, IcuArgumentKind> {
    analysis
        .arguments
        .iter()
        .map(|argument| (argument.name.clone(), argument.kind))
        .collect()
}

fn formatter_map(analysis: &IcuAnalysis) -> BTreeMap<(String, IcuArgumentKind), Option<String>> {
    analysis
        .formatters
        .iter()
        .map(|formatter| {
            (
                (formatter.name.clone(), formatter.kind),
                formatter.style.clone().map(|style| style.trim().to_owned()),
            )
        })
        .collect()
}

fn selector_set(selectors: &[String]) -> BTreeSet<String> {
    selectors.iter().cloned().collect()
}

fn formatter_style_label(style: Option<&str>) -> &str {
    style
        .map(str::trim)
        .filter(|style| !style.is_empty())
        .unwrap_or("<none>")
}

fn compare_arguments(
    source: &IcuAnalysis,
    translation: &IcuAnalysis,
    options: &IcuCompatibilityOptions,
    report: &mut IcuCompatibilityReport,
) {
    let source_arguments = argument_map(source);
    let translation_arguments = argument_map(translation);

    for (name, source_kind) in &source_arguments {
        let Some(translation_kind) = translation_arguments.get(name) else {
            report.diagnostics.push(IcuDiagnostic::new(
                IcuDiagnosticSeverity::Error,
                "icu.missing_argument",
                format!("Translation is missing ICU argument `{name}`."),
                Some(name.clone()),
            ));
            continue;
        };
        if source_kind != translation_kind {
            report.diagnostics.push(IcuDiagnostic::new(
                IcuDiagnosticSeverity::Error,
                "icu.argument_kind_changed",
                format!(
                    "Translation changes ICU argument `{name}` from {source_kind:?} to {translation_kind:?}."
                ),
                Some(name.clone()),
            ));
        }
    }

    if options.report_extra_arguments {
        for name in translation_arguments.keys() {
            if !source_arguments.contains_key(name) {
                report.diagnostics.push(IcuDiagnostic::new(
                    IcuDiagnosticSeverity::Error,
                    "icu.extra_argument",
                    format!(
                        "Translation adds ICU argument `{name}` that is not present in source."
                    ),
                    Some(name.clone()),
                ));
            }
        }
    }
}

fn compare_formatter_styles(
    source: &IcuAnalysis,
    translation: &IcuAnalysis,
    report: &mut IcuCompatibilityReport,
) {
    let source_formatters = formatter_map(source);
    let translation_formatters = formatter_map(translation);

    for ((name, kind), source_style) in source_formatters {
        let Some(translation_style) = translation_formatters.get(&(name.clone(), kind)) else {
            continue;
        };
        if source_style != *translation_style {
            report.diagnostics.push(IcuDiagnostic::new(
                IcuDiagnosticSeverity::Error,
                "icu.formatter_style_changed",
                format!(
                    "Translation changes ICU formatter style for `{name}` from {source_style:?} to {translation_style:?}."
                ),
                Some(name),
            ));
        }
    }
}

fn tag_counts(analysis: &IcuAnalysis) -> BTreeMap<String, usize> {
    let mut counts = BTreeMap::new();
    for tag in &analysis.tags {
        *counts.entry(tag.name.clone()).or_insert(0) += 1;
    }
    counts
}

fn compare_tags(
    source: &IcuAnalysis,
    translation: &IcuAnalysis,
    options: &IcuCompatibilityOptions,
    report: &mut IcuCompatibilityReport,
) {
    let source_tags = tag_counts(source);
    let translation_tags = tag_counts(translation);
    for (name, source_count) in &source_tags {
        let translation_count = translation_tags.get(name).copied().unwrap_or_default();
        if translation_count < *source_count {
            report.diagnostics.push(IcuDiagnostic::new(
                IcuDiagnosticSeverity::Error,
                "icu.missing_tag",
                format!("Translation is missing ICU tag `{name}`."),
                Some(name.clone()),
            ));
        }
    }
    if options.report_extra_tags {
        for (name, translation_count) in &translation_tags {
            let source_count = source_tags.get(name).copied().unwrap_or_default();
            if *translation_count > source_count {
                report.diagnostics.push(IcuDiagnostic::new(
                    IcuDiagnosticSeverity::Error,
                    "icu.extra_tag",
                    format!("Translation adds ICU tag `{name}` that is not present in source."),
                    Some(name.clone()),
                ));
            }
        }
    }
}

fn select_map(analysis: &IcuAnalysis) -> BTreeMap<String, BTreeSet<String>> {
    let mut out = BTreeMap::<String, BTreeSet<String>>::new();
    for select in &analysis.selects {
        out.entry(select.name.clone())
            .or_default()
            .extend(selector_set(&select.selectors));
    }
    out
}

fn compare_selects(
    source: &IcuAnalysis,
    translation: &IcuAnalysis,
    options: &IcuCompatibilityOptions,
    report: &mut IcuCompatibilityReport,
) {
    let source_selects = select_map(source);
    let translation_selects = select_map(translation);
    for (name, source_selectors) in &source_selects {
        let Some(translation_selectors) = translation_selects.get(name) else {
            continue;
        };
        for selector in source_selectors {
            if !translation_selectors.contains(selector) {
                report.diagnostics.push(IcuDiagnostic::new(
                    IcuDiagnosticSeverity::Error,
                    "icu.missing_select_selector",
                    format!(
                        "Translation is missing ICU select selector `{selector}` for `{name}`."
                    ),
                    Some(format!("{name}:{selector}")),
                ));
            }
        }
        if options.report_extra_selectors {
            for selector in translation_selectors {
                if !source_selectors.contains(selector) {
                    report.diagnostics.push(IcuDiagnostic::new(
                        IcuDiagnosticSeverity::Warning,
                        "icu.extra_select_selector",
                        format!("Translation adds ICU select selector `{selector}` for `{name}`."),
                        Some(format!("{name}:{selector}")),
                    ));
                }
            }
        }
    }
}

fn plural_key(plural: &IcuPluralSummary) -> (String, IcuPluralKind) {
    (plural.name.clone(), plural.kind.clone())
}

fn plural_map(analysis: &IcuAnalysis) -> BTreeMap<(String, IcuPluralKind), IcuPluralSummary> {
    analysis
        .plurals
        .iter()
        .map(|plural| (plural_key(plural), plural.clone()))
        .collect()
}

fn compare_plurals(
    source: &IcuAnalysis,
    translation: &IcuAnalysis,
    report: &mut IcuCompatibilityReport,
) {
    let source_plurals = plural_map(source);
    let translation_plurals = plural_map(translation);

    for (key, source_plural) in source_plurals {
        let Some(translation_plural) = translation_plurals.get(&key) else {
            continue;
        };
        if source_plural.offset != translation_plural.offset {
            report.diagnostics.push(IcuDiagnostic::new(
                IcuDiagnosticSeverity::Error,
                "icu.plural_offset_changed",
                format!(
                    "Translation changes ICU plural offset for `{}` from {} to {}.",
                    source_plural.name, source_plural.offset, translation_plural.offset
                ),
                Some(source_plural.name.clone()),
            ));
        }

        let translation_selectors = selector_set(&translation_plural.selectors);
        for selector in &source_plural.selectors {
            if !translation_selectors.contains(selector) {
                report.diagnostics.push(IcuDiagnostic::new(
                    IcuDiagnosticSeverity::Error,
                    "icu.missing_plural_selector",
                    format!(
                        "Translation is missing ICU plural selector `{selector}` for `{}`.",
                        source_plural.name
                    ),
                    Some(format!("{}:{selector}", source_plural.name)),
                ));
            }
        }
    }
}

fn report_pattern_styles(analysis: &IcuAnalysis, label: &str, report: &mut IcuCompatibilityReport) {
    for formatter in &analysis.formatters {
        if !matches!(
            formatter.kind,
            IcuArgumentKind::Number | IcuArgumentKind::Date | IcuArgumentKind::Time
        ) || formatter.style_kind != IcuStyleKind::Pattern
        {
            continue;
        }
        report.diagnostics.push(IcuDiagnostic::new(
            IcuDiagnosticSeverity::Warning,
            "icu.pattern_style_discouraged",
            format!(
                "{label} ICU formatter `{}` uses an opaque pattern style; prefer a predefined style or `::` skeleton.",
                formatter.name
            ),
            Some(formatter.name.clone()),
        ));
    }
}

#[cfg(test)]
mod tests {
    use crate::{
        IcuArgumentKind, IcuCompatibilityOptions, IcuDiagnosticSeverity, IcuFormatterSupport,
        IcuStyleKind, analyze_icu, compare_icu_messages, extract_argument_names, extract_tag_names,
        parse_icu, validate_icu_formatter_support,
    };

    #[test]
    fn analysis_separates_arguments_formatters_plurals_selects_and_tags() {
        let message = parse_icu(
            "<link>{gender, select, other {{count, plural, one {{when, date, short}} other {{count, number, ::compact-short}}}}}</link>",
        )
        .expect("parse");
        let analysis = analyze_icu(&message);

        assert_eq!(
            extract_argument_names(&message),
            vec!["gender", "count", "when"]
        );
        assert_eq!(extract_tag_names(&message), vec!["link"]);
        assert_eq!(analysis.tags.len(), 1);
        assert_eq!(analysis.selects.len(), 1);
        assert_eq!(analysis.plurals.len(), 1);
        assert_eq!(analysis.formatters.len(), 2);
        assert!(
            analysis
                .arguments
                .iter()
                .any(|argument| argument.kind == IcuArgumentKind::Select)
        );
    }

    #[test]
    fn analysis_classifies_formatter_styles() {
        let message = parse_icu(
            "{n, number, integer} {total, number, ::currency/USD} {created, date, yyyy-MM-dd} {items, list, disjunction}",
        )
        .expect("parse");
        let analysis = analyze_icu(&message);
        let kinds = analysis
            .formatters
            .iter()
            .map(|formatter| formatter.style_kind)
            .collect::<Vec<_>>();

        assert_eq!(
            kinds,
            vec![
                IcuStyleKind::Predefined,
                IcuStyleKind::Skeleton,
                IcuStyleKind::Pattern,
                IcuStyleKind::Predefined
            ]
        );
    }

    #[test]
    fn compatibility_reports_argument_formatter_tag_and_selector_mismatches() {
        let source = parse_icu(
            "<link>{gender, select, male {{count, number, integer} for {user}} female {{count, number, integer}} other {{count, number, integer}}}</link>",
        )
        .expect("parse source");
        let translation = parse_icu(
            "<b>{gender, select, male {{count}} other {{total, number, integer}} extra {{count}}}</b>",
        )
        .expect("parse translation");

        let report =
            compare_icu_messages(&source, &translation, &IcuCompatibilityOptions::default());
        let codes = report
            .diagnostics
            .iter()
            .map(|diagnostic| diagnostic.code.as_str())
            .collect::<Vec<_>>();

        assert!(report.has_errors());
        assert!(codes.contains(&"icu.missing_argument"));
        assert!(codes.contains(&"icu.extra_argument"));
        assert!(codes.contains(&"icu.argument_kind_changed"));
        assert!(codes.contains(&"icu.missing_tag"));
        assert!(codes.contains(&"icu.extra_tag"));
        assert!(codes.contains(&"icu.missing_select_selector"));
        assert!(codes.contains(&"icu.extra_select_selector"));
    }

    #[test]
    fn compatibility_allows_extra_plural_categories_but_reports_offset_changes() {
        let source =
            parse_icu("{count, plural, offset:1 one {# file} other {# files}}").expect("source");
        let translation =
            parse_icu("{count, plural, offset:2 one {# Datei} few {# Dateien} other {# Dateien}}")
                .expect("translation");

        let report =
            compare_icu_messages(&source, &translation, &IcuCompatibilityOptions::default());

        assert!(
            report
                .diagnostics
                .iter()
                .any(|diagnostic| diagnostic.code == "icu.plural_offset_changed")
        );
        assert!(
            !report
                .diagnostics
                .iter()
                .any(|diagnostic| diagnostic.code == "icu.extra_plural_selector")
        );
    }

    #[test]
    fn compatibility_reports_discouraged_pattern_styles_as_warnings() {
        let source = parse_icu("{created, date, yyyy-MM-dd}").expect("source");
        let translation = parse_icu("{created, date, yyyy-MM-dd}").expect("translation");

        let report =
            compare_icu_messages(&source, &translation, &IcuCompatibilityOptions::default());

        assert!(report.diagnostics.iter().any(|diagnostic| {
            diagnostic.code == "icu.pattern_style_discouraged"
                && diagnostic.severity == IcuDiagnosticSeverity::Warning
        }));
    }

    #[test]
    fn formatter_support_validation_allows_supported_formatters() {
        let message = parse_icu("{total, number, percent} {created, date, short}").expect("parse");

        let report = validate_icu_formatter_support(&message, |_| IcuFormatterSupport::Supported);

        assert!(report.diagnostics.is_empty());
    }

    #[test]
    fn formatter_support_validation_reports_unsupported_kinds() {
        let message = parse_icu("{items, list, conjunction}").expect("parse");

        let report = validate_icu_formatter_support(&message, |formatter| {
            if formatter.kind == IcuArgumentKind::List {
                IcuFormatterSupport::UnsupportedKind {
                    severity: IcuDiagnosticSeverity::Error,
                }
            } else {
                IcuFormatterSupport::Supported
            }
        });

        assert_eq!(report.diagnostics.len(), 1);
        assert_eq!(report.diagnostics[0].code, "icu.unsupported_formatter_kind");
        assert_eq!(report.diagnostics[0].severity, IcuDiagnosticSeverity::Error);
        assert_eq!(report.diagnostics[0].name.as_deref(), Some("items"));
    }

    #[test]
    fn formatter_support_validation_reports_unsupported_styles() {
        let message = parse_icu("{total, number, ::compact-short}").expect("parse");

        let report = validate_icu_formatter_support(&message, |formatter| {
            if formatter.style.as_deref() == Some("::compact-short") {
                IcuFormatterSupport::UnsupportedStyle {
                    severity: IcuDiagnosticSeverity::Warning,
                }
            } else {
                IcuFormatterSupport::Supported
            }
        });

        assert_eq!(report.diagnostics.len(), 1);
        assert_eq!(
            report.diagnostics[0].code,
            "icu.unsupported_formatter_style"
        );
        assert_eq!(
            report.diagnostics[0].severity,
            IcuDiagnosticSeverity::Warning
        );
        assert_eq!(report.diagnostics[0].name.as_deref(), Some("total"));
    }
}