jmap-types 0.6.0

Shared JMAP wire types (RFC 8620) for the jmap-* crate family
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
//! Localized display text (draft-atwood-jmap-updates-00 §3–§5).
//!
//! The `urn:ietf:params:jmap:i18n` capability adds two properties to
//! every data type that has localizable properties, modelled on
//! JSCalendar (RFC 8984 §4.2.8 and §4.6.1):
//!
//! - `locale` (`String|null`): the [`LanguageTag`] of the base values;
//! - `localizations` (`String[PatchObject]|null`): a [`Localizations`]
//!   map from language tag to a [`PatchObject`] over the localizable
//!   properties.
//!
//! It also adds a `language` argument (a list of [`LanguageRange`]s) to
//! `/get`, `/query` and `/queryChanges`; [`lookup`] implements the
//! RFC 4647 §3.4 Lookup that picks one tag per object, and
//! [`localized_view`] builds the single-language `/get` view.
//!
//! The `/set` side is [`parse_locale`] and [`check_localizations`],
//! which turn wire values into the validated types or an
//! `invalidProperties` [`SetError`], and [`prune_stale_localizations`],
//! which drops translations of a base value that an update changed.
//!
//! A data type declares its localizable properties with
//! [`JmapObject::LOCALIZABLE`](crate::JmapObject::LOCALIZABLE).
//! Design: `docs/design/updates.md` (decisions 27 to 31).

use crate::{PatchObject, SetError, SetErrorType};
use serde::de::{self, MapAccess, Visitor};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::{Map, Value};
use std::collections::BTreeMap;
use std::fmt;
use std::str::FromStr;

/// The capability URI (draft-atwood-jmap-updates-00 §3, §10).
pub const JMAP_I18N_URI: &str = "urn:ietf:params:jmap:i18n";

/// The Session `capabilities` value of [`JMAP_I18N_URI`]: an empty
/// object (draft-atwood-jmap-updates-00 §3).
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct I18nCapability {
    /// Catch-all for vendor / site / private extension fields.
    #[serde(flatten, default, skip_serializing_if = "Map::is_empty")]
    pub extra: Map<String, Value>,
}

/// The `accountCapabilities` value of [`JMAP_I18N_URI`]: an empty object
/// (draft-atwood-jmap-updates-00 §3).
///
/// Present on an account only when the server supports localizations
/// for it. The draft defines no account-level language preference.
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct I18nAccountCapability {
    /// Catch-all for vendor / site / private extension fields.
    #[serde(flatten, default, skip_serializing_if = "Map::is_empty")]
    pub extra: Map<String, Value>,
}

// ---------------------------------------------------------------------------
// Language tags and ranges
// ---------------------------------------------------------------------------

/// Why a string is not a well-formed [`LanguageTag`] or
/// [`LanguageRange`].
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LanguageTagError {
    /// The string is empty, or has an empty subtag (`"en--US"`, `"en-"`).
    EmptySubtag,
    /// A subtag has a character other than an ASCII letter or digit, or
    /// is longer than eight characters.
    InvalidSubtag,
    /// Every subtag is well-formed but their sequence matches no
    /// production of the grammar (RFC 5646 §2.1 or RFC 4647 §2.1).
    IllFormed,
}

impl fmt::Display for LanguageTagError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(match self {
            Self::EmptySubtag => "language tag has an empty subtag",
            Self::InvalidSubtag => "language tag subtag is not 1 to 8 ASCII letters or digits",
            Self::IllFormed => "language tag does not match the RFC 5646 grammar",
        })
    }
}

impl std::error::Error for LanguageTagError {}

/// An RFC 5646 language tag, checked to be well-formed (§2.2.9) and
/// held in the canonical case of §2.1.1.
///
/// Tags are case-insensitive (§2.1.1), so parsing folds the case:
/// `"EN-us"` and `"en-US"` give equal values, both rendered `"en-US"`.
/// Equality, ordering and hashing therefore ignore the case of the
/// input. The check is well-formedness only: subtags are not looked up
/// in the IANA registry, so `"qq-Zzzz"` is accepted.
///
/// ```rust
/// use jmap_types::i18n::LanguageTag;
///
/// let tag: LanguageTag = "sr-latn-rs".parse().unwrap();
/// assert_eq!(tag.as_str(), "sr-Latn-RS");
/// assert!("en--US".parse::<LanguageTag>().is_err());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LanguageTag(String);

/// RFC 5646 §2.1 `irregular` grandfathered tags, lowercase. They are the
/// only well-formed tags that the `langtag` production does not match;
/// the `regular` ones (`zh-min-nan`, `art-lojban`, ...) match it.
const IRREGULAR: &[&str] = &[
    "en-gb-oed",
    "i-ami",
    "i-bnn",
    "i-default",
    "i-enochian",
    "i-hak",
    "i-klingon",
    "i-lux",
    "i-mingo",
    "i-navajo",
    "i-pwn",
    "i-tao",
    "i-tay",
    "i-tsu",
    "sgn-be-fr",
    "sgn-be-nl",
    "sgn-ch-de",
];

/// Split on `-` and check each subtag is 1 to 8 ASCII alphanumerics.
fn subtags(s: &str) -> Result<Vec<&str>, LanguageTagError> {
    let parts: Vec<&str> = s.split('-').collect();
    for p in &parts {
        if p.is_empty() {
            return Err(LanguageTagError::EmptySubtag);
        }
        if p.len() > 8 || !p.bytes().all(|b| b.is_ascii_alphanumeric()) {
            return Err(LanguageTagError::InvalidSubtag);
        }
    }
    Ok(parts)
}

fn is_alpha(s: &str) -> bool {
    s.bytes().all(|b| b.is_ascii_alphabetic())
}

fn is_digit(s: &str) -> bool {
    s.bytes().all(|b| b.is_ascii_digit())
}

/// RFC 5646 §2.1 `variant = 5*8alphanum / (DIGIT 3alphanum)`.
fn is_variant(s: &str) -> bool {
    (5..=8).contains(&s.len()) || (s.len() == 4 && s.as_bytes()[0].is_ascii_digit())
}

/// `true` when `parts` (already 1*8alphanum each) match the RFC 5646
/// `langtag` production.
fn is_langtag(parts: &[&str]) -> bool {
    let mut i = 0;
    let n = parts.len();
    // language = 2*3ALPHA ["-" extlang] / 4ALPHA / 5*8ALPHA
    let lang = parts[0];
    if !is_alpha(lang) || lang.len() < 2 {
        return false;
    }
    i += 1;
    if lang.len() <= 3 {
        // extlang = 3ALPHA *2("-" 3ALPHA)
        let mut ext = 0;
        while ext < 3 && i < n && parts[i].len() == 3 && is_alpha(parts[i]) {
            ext += 1;
            i += 1;
        }
    }
    // script = 4ALPHA
    if i < n && parts[i].len() == 4 && is_alpha(parts[i]) {
        i += 1;
    }
    // region = 2ALPHA / 3DIGIT
    if i < n
        && ((parts[i].len() == 2 && is_alpha(parts[i]))
            || (parts[i].len() == 3 && is_digit(parts[i])))
    {
        i += 1;
    }
    // *("-" variant)
    while i < n && is_variant(parts[i]) {
        i += 1;
    }
    // *("-" extension); extension = singleton 1*("-" (2*8alphanum))
    while i < n && parts[i].len() == 1 && !parts[i].eq_ignore_ascii_case("x") {
        i += 1;
        let start = i;
        while i < n && parts[i].len() >= 2 {
            i += 1;
        }
        if i == start {
            return false;
        }
    }
    // ["-" privateuse]
    if i < n && parts[i].eq_ignore_ascii_case("x") {
        return is_privateuse(&parts[i..]);
    }
    i == n
}

/// RFC 5646 §2.1 `privateuse = "x" 1*("-" (1*8alphanum))`; `parts[0]`
/// is the `x`.
fn is_privateuse(parts: &[&str]) -> bool {
    parts.len() >= 2
}

/// RFC 5646 §2.1.1 canonical case: lowercase, except that a two-letter
/// subtag is uppercase and a four-letter subtag titlecase when it is not
/// the first subtag and no singleton precedes it.
fn canonical_case(parts: &[&str]) -> String {
    let mut out = String::new();
    let mut after_singleton = false;
    for (i, p) in parts.iter().enumerate() {
        if i > 0 {
            out.push('-');
        }
        if i > 0 && !after_singleton && p.len() == 2 {
            out.push_str(&p.to_ascii_uppercase());
        } else if i > 0 && !after_singleton && p.len() == 4 {
            let lower = p.to_ascii_lowercase();
            let mut chars = lower.chars();
            if let Some(c) = chars.next() {
                out.push(c.to_ascii_uppercase());
                out.push_str(chars.as_str());
            }
        } else {
            out.push_str(&p.to_ascii_lowercase());
        }
        if p.len() == 1 {
            after_singleton = true;
        }
    }
    out
}

impl LanguageTag {
    /// Parse `s` as an RFC 5646 `Language-Tag` and fold it to canonical
    /// case.
    ///
    /// # Errors
    ///
    /// [`LanguageTagError`] when `s` is not well-formed.
    pub fn parse(s: &str) -> Result<Self, LanguageTagError> {
        let parts = subtags(s)?;
        let lower = s.to_ascii_lowercase();
        let ok = if parts[0].eq_ignore_ascii_case("x") {
            is_privateuse(&parts)
        } else {
            IRREGULAR.contains(&lower.as_str()) || is_langtag(&parts)
        };
        if ok {
            Ok(Self(canonical_case(&parts)))
        } else {
            Err(LanguageTagError::IllFormed)
        }
    }

    /// The tag in canonical case.
    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl FromStr for LanguageTag {
    type Err = LanguageTagError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

impl fmt::Display for LanguageTag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl AsRef<str> for LanguageTag {
    fn as_ref(&self) -> &str {
        &self.0
    }
}

impl Serialize for LanguageTag {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for LanguageTag {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = String::deserialize(d)?;
        Self::parse(&s).map_err(de::Error::custom)
    }
}

/// An RFC 4647 §2.1 basic language range: `"*"`, or a primary subtag
/// of 1 to 8 letters followed by subtags of 1 to 8 letters or digits.
///
/// Held in the canonical case of a [`LanguageTag`] (RFC 5646 §2.1.1),
/// so that equal ranges render equally; matching ignores case either
/// way (RFC 4647 §2).
///
/// ```rust
/// use jmap_types::i18n::LanguageRange;
///
/// let r: LanguageRange = "DE-ch".parse().unwrap();
/// assert_eq!(r.as_str(), "de-CH");
/// assert!("*".parse::<LanguageRange>().unwrap().is_wildcard());
/// ```
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LanguageRange(String);

impl LanguageRange {
    /// Parse `s` as an RFC 4647 §2.1 `language-range`.
    ///
    /// # Errors
    ///
    /// [`LanguageTagError`] when `s` is not a basic language range.
    pub fn parse(s: &str) -> Result<Self, LanguageTagError> {
        if s == "*" {
            return Ok(Self(s.to_owned()));
        }
        let parts = subtags(s)?;
        if !is_alpha(parts[0]) {
            return Err(LanguageTagError::IllFormed);
        }
        Ok(Self(canonical_case(&parts)))
    }

    /// The range in canonical case.
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// `true` for the wildcard range `"*"`.
    pub fn is_wildcard(&self) -> bool {
        self.0 == "*"
    }
}

impl FromStr for LanguageRange {
    type Err = LanguageTagError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::parse(s)
    }
}

impl fmt::Display for LanguageRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl Serialize for LanguageRange {
    fn serialize<S: Serializer>(&self, s: S) -> Result<S::Ok, S::Error> {
        s.serialize_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for LanguageRange {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        let s = String::deserialize(d)?;
        Self::parse(&s).map_err(de::Error::custom)
    }
}

/// The language ranges of an `Accept-Language` field value (RFC 9110
/// §12.5.4), most preferred first: ordered by quality value, ties kept in
/// header order, with `q=0` ranges ("not acceptable") and malformed items
/// left out. The result is a priority list for [`lookup`].
///
/// ```rust
/// use jmap_types::i18n::accept_language_ranges;
///
/// let ranges = accept_language_ranges("fr-CH, fr;q=0.9, en;q=0.8, de;q=0.7, *;q=0.5");
/// let tags: Vec<&str> = ranges.iter().map(|r| r.as_str()).collect();
/// assert_eq!(tags, ["fr-CH", "fr", "en", "de", "*"]);
/// ```
pub fn accept_language_ranges(value: &str) -> Vec<LanguageRange> {
    let mut weighted: Vec<(u16, usize, LanguageRange)> = value
        .split(',')
        .enumerate()
        .filter_map(|(i, item)| {
            let mut parts = item.split(';');
            let range = LanguageRange::parse(parts.next()?.trim()).ok()?;
            // The parameter name is case-insensitive (RFC 9110 §12.4.2).
            let q = match parts.find_map(|p| {
                let p = p.trim();
                (p.len() >= 2 && p[..2].eq_ignore_ascii_case("q=")).then(|| &p[2..])
            }) {
                None => 1000,
                Some(q) => qvalue(q.trim())?,
            };
            (q > 0).then_some((q, i, range))
        })
        .collect();
    weighted.sort_by(|a, b| b.0.cmp(&a.0).then(a.1.cmp(&b.1)));
    weighted.into_iter().map(|(_, _, r)| r).collect()
}

/// An RFC 9110 §12.4.2 `qvalue` in thousandths: `0` to `1`, with at
/// most three decimal places.
fn qvalue(s: &str) -> Option<u16> {
    let (int, frac) = s.split_once('.').unwrap_or((s, ""));
    if frac.len() > 3 || !frac.bytes().all(|b| b.is_ascii_digit()) {
        return None;
    }
    let frac_val: u16 = format!("{frac:0<3}").parse().ok()?;
    match int {
        "0" => Some(frac_val),
        "1" if frac_val == 0 => Some(1000),
        _ => None,
    }
}

/// RFC 4647 §3.4 Lookup: the tag in `available` that best matches the
/// priority list `ranges`, or `None` when nothing matches and the
/// default applies.
///
/// Each range is tried in order. A range that matches no tag is
/// shortened by its last subtag, and also by a single-letter subtag left
/// at its end, until it matches or is empty; ranges are never widened,
/// so `"de"` does not match `"de-CH"`. A `"*"` that other ranges follow
/// is skipped; a `"*"` with no range after it ends the search with the
/// default. Matching ignores case.
///
/// draft-atwood-jmap-updates-00 §4.2.1 passes the `localizations` keys
/// plus the object's `locale` as `available`, and uses the object's
/// `locale` as the default.
///
/// ```rust
/// use jmap_types::i18n::{LanguageRange, LanguageTag, lookup};
///
/// let tags: Vec<LanguageTag> = ["fr", "de"].iter().map(|t| t.parse().unwrap()).collect();
/// let ranges: Vec<LanguageRange> = ["fr-CH", "de"].iter().map(|r| r.parse().unwrap()).collect();
/// assert_eq!(lookup(&ranges, &tags).map(|t| t.as_str()), Some("fr"));
/// ```
pub fn lookup<'a, I>(ranges: &[LanguageRange], available: I) -> Option<&'a LanguageTag>
where
    I: IntoIterator<Item = &'a LanguageTag>,
    I::IntoIter: Clone,
{
    let available = available.into_iter();
    for (i, range) in ranges.iter().enumerate() {
        if range.is_wildcard() {
            if i + 1 == ranges.len() {
                return None;
            }
            continue;
        }
        let mut candidate = range.as_str();
        loop {
            if let Some(tag) = available
                .clone()
                .find(|t| t.as_str().eq_ignore_ascii_case(candidate))
            {
                return Some(tag);
            }
            let Some(cut) = candidate.rfind('-') else {
                break;
            };
            candidate = &candidate[..cut];
            // Drop a singleton left at the end ("en-a-bbb" -> "en-a" -> "en").
            if let Some(cut) = candidate.rfind('-')
                && candidate.len() - cut == 2
            {
                candidate = &candidate[..cut];
            }
        }
    }
    None
}

// ---------------------------------------------------------------------------
// Localizable properties and the localizations map
// ---------------------------------------------------------------------------

/// One localizable property of a data type: its wire name and whether
/// its type allows `null` (draft-atwood-jmap-updates-00 §4.1).
///
/// Every localizable property in the draft is a `String` or a
/// `String|null`.
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LocalizableProperty {
    /// Wire name, e.g. `"name"`.
    pub name: &'static str,
    /// `true` for a `String|null` property, whose localized value may be
    /// `null` ("no value in that language").
    pub nullable: bool,
}

impl LocalizableProperty {
    /// A property named `name`, nullable when `nullable`.
    pub const fn new(name: &'static str, nullable: bool) -> Self {
        Self { name, nullable }
    }
}

/// The `localizations` property: a map from [`LanguageTag`] to a
/// [`PatchObject`] that gives the localizable properties in that
/// language (draft-atwood-jmap-updates-00 §4.1).
///
/// Deserializing parses each key as a tag in canonical case and rejects
/// two keys that differ only in case. It does not check the patches;
/// [`check_localizations`] does that against the data type's
/// [`LocalizableProperty`] list.
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct Localizations(BTreeMap<LanguageTag, PatchObject>);

impl Localizations {
    /// The patch for `tag`, if any.
    pub fn get(&self, tag: &LanguageTag) -> Option<&PatchObject> {
        self.0.get(tag)
    }

    /// The localizations, in canonical tag order.
    pub fn iter(&self) -> impl Iterator<Item = (&LanguageTag, &PatchObject)> {
        self.0.iter()
    }

    /// The language tags, in canonical order.
    pub fn tags(&self) -> impl Iterator<Item = &LanguageTag> + Clone {
        self.0.keys()
    }

    /// `true` when there is no localization.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }

    /// Number of localizations.
    pub fn len(&self) -> usize {
        self.0.len()
    }
}

impl<'de> Deserialize<'de> for Localizations {
    fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
        struct V;
        impl<'de> Visitor<'de> for V {
            type Value = Localizations;
            fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
                f.write_str("a map from language tag to PatchObject")
            }
            fn visit_map<A: MapAccess<'de>>(self, mut m: A) -> Result<Localizations, A::Error> {
                let mut out = BTreeMap::new();
                while let Some((k, v)) = m.next_entry::<String, PatchObject>()? {
                    let tag = LanguageTag::parse(&k).map_err(de::Error::custom)?;
                    if out.insert(tag, v).is_some() {
                        return Err(de::Error::custom(format!(
                            "duplicate localizations key (ignoring case): {k}"
                        )));
                    }
                }
                Ok(Localizations(out))
            }
        }
        d.deserialize_map(V)
    }
}

// ---------------------------------------------------------------------------
// /set: validation and pruning
// ---------------------------------------------------------------------------

fn invalid(property: &str, description: String) -> Box<SetError> {
    Box::new(
        SetError::new(SetErrorType::InvalidProperties)
            .with_properties([property])
            .with_description(description),
    )
}

/// Parse a `/set` `locale` value: `null` or a language tag.
///
/// # Errors
///
/// An `invalidProperties` [`SetError`] naming `locale` when `value` is
/// neither `null` nor a well-formed language tag.
pub fn parse_locale(value: &Value) -> Result<Option<LanguageTag>, Box<SetError>> {
    match value {
        Value::Null => Ok(None),
        Value::String(s) => LanguageTag::parse(s)
            .map(Some)
            .map_err(|e| invalid("locale", format!("locale {s:?}: {e}"))),
        other => Err(invalid(
            "locale",
            format!(
                "locale must be a String or null, not {}",
                crate::json_type_name(other)
            ),
        )),
    }
}

/// Check a `/set` `localizations` value against the restrictions of
/// draft-atwood-jmap-updates-00 §4.1 and return it with its keys in
/// canonical case (which servers SHOULD store).
///
/// `locale` is the object's `locale` after the same create or update.
/// `null` gives `Ok(None)`. Otherwise the value must be an object whose
/// keys are language tags, no two equal ignoring case and none equal to
/// `locale`, and whose values are objects naming only properties in
/// `properties`, each a String, or `null` when that property is
/// nullable. The defining specification's length and syntax limits on
/// localized values are the caller's to apply.
///
/// # Errors
///
/// An `invalidProperties` [`SetError`] naming `localizations`, whose
/// description says which rule failed.
pub fn check_localizations(
    locale: Option<&LanguageTag>,
    value: &Value,
    properties: &[LocalizableProperty],
) -> Result<Option<Localizations>, Box<SetError>> {
    let bad = |d: String| invalid("localizations", d);
    let obj = match value {
        Value::Null => return Ok(None),
        Value::Object(o) => o,
        other => {
            return Err(bad(format!(
                "localizations must be an object or null, not {}",
                crate::json_type_name(other)
            )));
        }
    };
    let mut out = BTreeMap::new();
    for (key, patch) in obj {
        let tag = LanguageTag::parse(key).map_err(|e| bad(format!("key {key:?}: {e}")))?;
        if locale == Some(&tag) {
            return Err(bad(format!("key {key:?} equals locale")));
        }
        let Value::Object(patch) = patch else {
            return Err(bad(format!("value for {key:?} is not a PatchObject")));
        };
        for (prop, v) in patch {
            let Some(p) = properties.iter().find(|p| p.name == prop) else {
                return Err(bad(format!(
                    "{key:?}: {prop:?} is not a localizable property"
                )));
            };
            match v {
                Value::String(_) => {}
                Value::Null if p.nullable => {}
                Value::Null => {
                    return Err(bad(format!("{key:?}: {prop:?} cannot be null")));
                }
                other => {
                    return Err(bad(format!(
                        "{key:?}: {prop:?} must be a String, not {}",
                        crate::json_type_name(other)
                    )));
                }
            }
        }
        if out
            .insert(tag, PatchObject::from_map(patch.clone()))
            .is_some()
        {
            return Err(bad(format!(
                "key {key:?} duplicates another key ignoring case"
            )));
        }
    }
    Ok(Some(Localizations(out)))
}

/// What a `/set` update `patch` sets inside `localizations`
/// (draft-atwood-jmap-updates-00 §4.1).
enum Touched {
    /// `localizations` as a whole.
    All,
    /// Per language: `None` for the whole PatchObject of that language,
    /// `Some(props)` for those properties inside it.
    Some(BTreeMap<LanguageTag, Option<Vec<String>>>),
}

fn touched(patch: &PatchObject) -> Touched {
    let mut out: BTreeMap<LanguageTag, Option<Vec<String>>> = BTreeMap::new();
    for key in patch.as_map().keys() {
        let mut segs = key.splitn(3, '/');
        if segs.next() != Some("localizations") {
            continue;
        }
        let Some(tag) = segs.next() else {
            return Touched::All;
        };
        let Ok(tag) = LanguageTag::parse(tag) else {
            continue;
        };
        match segs.next() {
            None => {
                out.insert(tag, None);
            }
            Some(prop) => {
                // JSON Pointer unescaping (RFC 6901 §4) of the property name.
                let prop = prop.replace("~1", "/").replace("~0", "~");
                let entry = out.entry(tag).or_insert_with(|| Some(Vec::new()));
                if let Some(props) = entry {
                    props.push(prop);
                }
            }
        }
    }
    Touched::Some(out)
}

/// Drop translations made stale by a `/set` update
/// (draft-atwood-jmap-updates-00 §4.1).
///
/// `old` and `new` are the serialized object before and after `patch`
/// was applied. For each property of `properties` whose base value
/// differs between them, and each language whose PatchObject the patch
/// did not set (not `localizations` as a whole, not
/// `localizations/<tag>`, and not `localizations/<tag>/<property>`),
/// the property is removed from that language's PatchObject in `new`,
/// and a PatchObject left empty is removed. A `localizations` left
/// empty becomes `null`.
///
/// Returns `true` when anything was removed: the caller then includes
/// `localizations` in the `updated` entry it returns to clients that use
/// the capability. Malformed stored keys are left as they are.
///
/// ```rust
/// use jmap_types::PatchObject;
/// use jmap_types::i18n::{LocalizableProperty, prune_stale_localizations};
/// use serde_json::json;
///
/// const PROPS: &[LocalizableProperty] = &[LocalizableProperty::new("name", false)];
/// let old = json!({"name": "Team", "localizations": {"fr": {"name": "Équipe"}}});
/// let mut new = json!({"name": "Staff", "localizations": {"fr": {"name": "Équipe"}}});
/// let patch: PatchObject = serde_json::from_value(json!({"name": "Staff"})).unwrap();
/// let removed = prune_stale_localizations(
///     old.as_object().unwrap(), new.as_object_mut().unwrap(), &patch, PROPS);
/// assert!(removed);
/// assert_eq!(new["localizations"], json!(null));
/// ```
pub fn prune_stale_localizations(
    old: &Map<String, Value>,
    new: &mut Map<String, Value>,
    patch: &PatchObject,
    properties: &[LocalizableProperty],
) -> bool {
    let changed: Vec<&str> = properties
        .iter()
        .map(|p| p.name)
        .filter(|n| old.get(*n) != new.get(*n))
        .collect();
    if changed.is_empty() {
        return false;
    }
    let touched = match touched(patch) {
        Touched::All => return false,
        Touched::Some(t) => t,
    };
    let Some(Value::Object(locs)) = new.get_mut("localizations") else {
        return false;
    };
    let mut removed = false;
    locs.retain(|key, entry| {
        let Ok(tag) = LanguageTag::parse(key) else {
            return true;
        };
        let set_here = touched.get(&tag);
        if matches!(set_here, Some(None)) {
            return true;
        }
        let Value::Object(p) = entry else {
            return true;
        };
        for name in &changed {
            let protected =
                matches!(set_here, Some(Some(props)) if props.iter().any(|q| q == name));
            if !protected && p.remove(*name).is_some() {
                removed = true;
            }
        }
        !p.is_empty()
    });
    if locs.is_empty() {
        new.insert("localizations".to_owned(), Value::Null);
    }
    removed
}

// ---------------------------------------------------------------------------
// /get: the single-language view
// ---------------------------------------------------------------------------

/// The tag that §4.2.1 chooses for `obj` under `ranges`, and the
/// PatchObject stored for it when it is a key of `localizations`.
fn choose(
    obj: &Map<String, Value>,
    ranges: &[LanguageRange],
) -> (Option<LanguageTag>, Option<Map<String, Value>>) {
    let locale = obj
        .get("locale")
        .and_then(Value::as_str)
        .and_then(|s| LanguageTag::parse(s).ok());
    let mut localizations: BTreeMap<LanguageTag, Map<String, Value>> =
        match obj.get("localizations") {
            Some(Value::Object(m)) => m
                .iter()
                .filter_map(|(k, v)| Some((LanguageTag::parse(k).ok()?, v.as_object()?.clone())))
                .collect(),
            _ => BTreeMap::new(),
        };
    let available: Vec<&LanguageTag> = localizations.keys().chain(locale.as_ref()).collect();
    let chosen = lookup(ranges, available.iter().copied())
        .or(locale.as_ref())
        .cloned();
    let patch = chosen.as_ref().and_then(|t| localizations.remove(t));
    (chosen, patch)
}

/// The value that the `/get` view under `ranges` ([`localized_view`])
/// gives `property` of the serialized object `obj`, for sorting and
/// filtering a `/query` with `language` (draft-atwood-jmap-updates-00
/// §4.2.2). `None` when the property is absent; `Some(&Value::Null)` when
/// the chosen language has no value for it.
///
/// ```rust
/// use jmap_types::i18n::{LanguageRange, localized_value};
/// use serde_json::json;
///
/// let obj = json!({"name": "Inbox", "locale": "en",
///                  "localizations": {"de": {"name": "Posteingang"}}});
/// let de: Vec<LanguageRange> = vec!["de-AT".parse().unwrap()];
/// assert_eq!(localized_value(obj.as_object().unwrap(), "name", &de), Some(json!("Posteingang")));
/// ```
pub fn localized_value(
    obj: &Map<String, Value>,
    property: &str,
    ranges: &[LanguageRange],
) -> Option<Value> {
    match choose(obj, ranges).1.and_then(|mut p| p.remove(property)) {
        Some(v) => Some(v),
        None => obj.get(property).cloned(),
    }
}

/// Turn the serialized object `obj` into its view in one language
/// (draft-atwood-jmap-updates-00 §4.2.1).
///
/// The tag is chosen by [`lookup`] over the keys of `localizations` and
/// the object's `locale`, falling back to `locale` (which may be
/// `null`). When the chosen tag is a key of `localizations`, its
/// PatchObject is applied to the properties in `properties`; a `null`
/// value there sets the property to `null`. `locale` is set to the
/// chosen tag, and `localizations` to `null`, since the view holds one
/// language only (decision 30).
///
/// Call it before filtering the object to the requested `properties`.
/// Stored values that do not parse (a malformed `locale` or key) are
/// treated as absent.
///
/// ```rust
/// use jmap_types::i18n::{LanguageRange, LocalizableProperty, localized_view};
/// use serde_json::json;
///
/// const PROPS: &[LocalizableProperty] = &[LocalizableProperty::new("name", false)];
/// let mut obj = json!({
///     "name": "Team contacts", "locale": "en",
///     "localizations": {"fr": {"name": "Contacts du groupe"}, "de": {"name": "Team-Kontakte"}},
/// });
/// let ranges: Vec<LanguageRange> = ["fr-CH", "de"].iter().map(|r| r.parse().unwrap()).collect();
/// localized_view(obj.as_object_mut().unwrap(), PROPS, &ranges);
/// assert_eq!(obj, json!({"name": "Contacts du groupe", "locale": "fr", "localizations": null}));
/// ```
pub fn localized_view(
    obj: &mut Map<String, Value>,
    properties: &[LocalizableProperty],
    ranges: &[LanguageRange],
) {
    let (chosen, patch) = choose(obj, ranges);
    if let Some(patch) = patch {
        for p in properties {
            if let Some(v) = patch.get(p.name) {
                obj.insert(p.name.to_owned(), v.clone());
            }
        }
    }
    obj.insert(
        "locale".to_owned(),
        chosen.map_or(Value::Null, |t| Value::String(t.0)),
    );
    obj.insert("localizations".to_owned(), Value::Null);
}

#[cfg(test)]
mod tests {
    use super::*;
    use serde_json::json;

    fn tag(s: &str) -> LanguageTag {
        LanguageTag::parse(s).unwrap()
    }

    /// RFC 9110 §12.5.4 and §12.4.2: quality ordering, `q=0` excluded,
    /// malformed qvalues and ranges dropped.
    #[test]
    fn accept_language_quality_order() {
        let tags = |v: &str| -> Vec<String> {
            accept_language_ranges(v)
                .iter()
                .map(|r| r.as_str().to_owned())
                .collect()
        };
        assert_eq!(tags("da, en-gb;q=0.8, en;q=0.7"), ["da", "en-GB", "en"]);
        assert_eq!(tags("en;q=0.5, de"), ["de", "en"]);
        assert_eq!(tags("de;q=0, en"), ["en"]);
        assert_eq!(tags("en;q=1.5, de;q=0.1234, fr;q=0.123"), ["fr"]);
        assert_eq!(tags("a b, ;q=1, de;Q=0.5, en;q=0.6"), ["en", "de"]);
        assert!(tags("").is_empty());
    }

    #[test]
    fn canonical_case_follows_rfc5646_2_1_1() {
        // The examples of RFC 5646 §2.1.1.
        assert_eq!(tag("mn-cyrl-mn").as_str(), "mn-Cyrl-MN");
        assert_eq!(tag("MN-cYRL-mn").as_str(), "mn-Cyrl-MN");
        assert_eq!(tag("en-ca-x-ca").as_str(), "en-CA-x-ca");
        assert_eq!(tag("SGN-BE-FR").as_str(), "sgn-BE-FR");
        assert_eq!(tag("az-latn-x-latn").as_str(), "az-Latn-x-latn");
        assert_eq!(tag("I-KLINGON").as_str(), "i-klingon");
    }

    #[test]
    fn case_variants_are_equal() {
        assert_eq!(tag("EN-us"), tag("en-US"));
    }

    #[test]
    fn range_rejects_digit_primary_and_accepts_wildcard() {
        assert_eq!(
            LanguageRange::parse("1de"),
            Err(LanguageTagError::IllFormed)
        );
        assert!(LanguageRange::parse("*").unwrap().is_wildcard());
        assert_eq!(
            LanguageRange::parse("de--ch"),
            Err(LanguageTagError::EmptySubtag)
        );
    }

    #[test]
    fn localizations_rejects_case_duplicate_keys_on_deserialize() {
        let r: Result<Localizations, _> = serde_json::from_value(json!({"de-CH": {}, "de-ch": {}}));
        assert!(r.is_err());
    }

    #[test]
    fn touched_reads_each_path_form() {
        let p: PatchObject = serde_json::from_value(json!({
            "localizations/FR": {"name": "x"},
            "localizations/de/description": null,
            "name": "y",
        }))
        .unwrap();
        let Touched::Some(t) = touched(&p) else {
            panic!("whole-map set reported for per-language paths");
        };
        assert_eq!(t.get(&tag("fr")), Some(&None));
        assert_eq!(
            t.get(&tag("de")),
            Some(&Some(vec!["description".to_owned()]))
        );
    }
}