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;
pub const JMAP_I18N_URI: &str = "urn:ietf:params:jmap:i18n";
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct I18nCapability {
#[serde(flatten, default, skip_serializing_if = "Map::is_empty")]
pub extra: Map<String, Value>,
}
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct I18nAccountCapability {
#[serde(flatten, default, skip_serializing_if = "Map::is_empty")]
pub extra: Map<String, Value>,
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LanguageTagError {
EmptySubtag,
InvalidSubtag,
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 {}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LanguageTag(String);
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",
];
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())
}
fn is_variant(s: &str) -> bool {
(5..=8).contains(&s.len()) || (s.len() == 4 && s.as_bytes()[0].is_ascii_digit())
}
fn is_langtag(parts: &[&str]) -> bool {
let mut i = 0;
let n = parts.len();
let lang = parts[0];
if !is_alpha(lang) || lang.len() < 2 {
return false;
}
i += 1;
if lang.len() <= 3 {
let mut ext = 0;
while ext < 3 && i < n && parts[i].len() == 3 && is_alpha(parts[i]) {
ext += 1;
i += 1;
}
}
if i < n && parts[i].len() == 4 && is_alpha(parts[i]) {
i += 1;
}
if i < n
&& ((parts[i].len() == 2 && is_alpha(parts[i]))
|| (parts[i].len() == 3 && is_digit(parts[i])))
{
i += 1;
}
while i < n && is_variant(parts[i]) {
i += 1;
}
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;
}
}
if i < n && parts[i].eq_ignore_ascii_case("x") {
return is_privateuse(&parts[i..]);
}
i == n
}
fn is_privateuse(parts: &[&str]) -> bool {
parts.len() >= 2
}
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 {
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)
}
}
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)
}
}
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct LanguageRange(String);
impl LanguageRange {
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)))
}
pub fn as_str(&self) -> &str {
&self.0
}
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)
}
}
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()?;
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()
}
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,
}
}
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];
if let Some(cut) = candidate.rfind('-')
&& candidate.len() - cut == 2
{
candidate = &candidate[..cut];
}
}
}
None
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct LocalizableProperty {
pub name: &'static str,
pub nullable: bool,
}
impl LocalizableProperty {
pub const fn new(name: &'static str, nullable: bool) -> Self {
Self { name, nullable }
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize)]
#[serde(transparent)]
pub struct Localizations(BTreeMap<LanguageTag, PatchObject>);
impl Localizations {
pub fn get(&self, tag: &LanguageTag) -> Option<&PatchObject> {
self.0.get(tag)
}
pub fn iter(&self) -> impl Iterator<Item = (&LanguageTag, &PatchObject)> {
self.0.iter()
}
pub fn tags(&self) -> impl Iterator<Item = &LanguageTag> + Clone {
self.0.keys()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
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)
}
}
fn invalid(property: &str, description: String) -> Box<SetError> {
Box::new(
SetError::new(SetErrorType::InvalidProperties)
.with_properties([property])
.with_description(description),
)
}
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)
),
)),
}
}
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)))
}
enum Touched {
All,
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) => {
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)
}
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
}
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)
}
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(),
}
}
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()
}
#[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() {
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()]))
);
}
}