const MAX_DEPTH: usize = 40;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Part {
Text(String),
Simple(String),
Pound,
Complex(Complex),
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Kind {
Plural,
SelectOrdinal,
Select,
}
impl Kind {
pub fn word(self) -> &'static str {
match self {
Kind::Plural => "plural",
Kind::SelectOrdinal => "selectordinal",
Kind::Select => "select",
}
}
pub fn plural_kind(self) -> crate::PluralKind {
match self {
Kind::Plural => crate::PluralKind::Cardinal,
Kind::SelectOrdinal => crate::PluralKind::Ordinal,
Kind::Select => crate::PluralKind::Select,
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Complex {
pub head: String,
pub kind: Kind,
pub branches: Vec<Branch>,
}
impl Complex {
pub fn arg(&self) -> String {
arg_name(&self.head)
}
pub fn other(&self) -> Option<&Branch> {
self.branches.iter().find(|b| b.selector == "other")
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Branch {
pub selector: String,
pub mid: String,
pub msg: Vec<Part>,
pub trail: String,
}
#[derive(Debug)]
pub struct IcuError;
type R<T> = Result<T, IcuError>;
fn is_ws(c: char) -> bool {
crate::py_isspace_char(c)
}
fn ch(s: &str, i: usize) -> Option<char> {
s.get(i..).and_then(|r| r.chars().next())
}
fn skip_ws(s: &str, mut i: usize) -> usize {
while let Some(c) = ch(s, i) {
if !is_ws(c) {
break;
}
i += c.len_utf8();
}
i
}
pub fn parse(s: &str) -> R<Vec<Part>> {
if !s.bytes().any(|b| matches!(b, b'{' | b'}' | b'\'')) {
return Ok(if s.is_empty() { vec![] } else { vec![Part::Text(s.to_string())] });
}
let (parts, i) = message(s, 0, 0, false, false)?;
if i != s.len() {
return Err(IcuError);
}
Ok(parts)
}
fn message(s: &str, mut i: usize, depth: usize, in_plural: bool, nested: bool) -> R<(Vec<Part>, usize)> {
if depth > MAX_DEPTH {
return Err(IcuError);
}
let b = s.as_bytes();
let n = b.len();
let mut parts = Vec::new();
let mut run = i;
while i < n {
match b[i] {
b'{' => {
flush(s, &mut parts, run, i);
let (p, next) = argument(s, i, depth)?;
parts.push(p);
i = next;
run = i;
}
b'}' => {
if !nested {
return Err(IcuError);
}
break;
}
b'#' if in_plural => {
flush(s, &mut parts, run, i);
parts.push(Part::Pound);
i += 1;
run = i;
}
b'\'' => {
let nxt = b.get(i + 1).copied();
if nxt == Some(b'\'') {
i += 2;
} else if matches!(nxt, Some(b'{' | b'}' | b'|')) || (nxt == Some(b'#') && in_plural) {
let mut j = i + 1;
while j < n {
if b[j] == b'\'' {
if b.get(j + 1) == Some(&b'\'') {
j += 2;
continue;
}
break;
}
j += 1;
}
i = j + 1;
} else {
i += 1;
}
}
_ => i += 1,
}
}
let end = i.min(n);
flush(s, &mut parts, run, end);
Ok((parts, i))
}
fn flush(s: &str, parts: &mut Vec<Part>, from: usize, to: usize) {
if to > from {
parts.push(Part::Text(s[from..to].to_string()));
}
}
fn argument(s: &str, start: usize, depth: usize) -> R<(Part, usize)> {
let mut j = skip_ws(s, start + 1);
let name_start = j;
while let Some(c) = ch(s, j) {
if is_ws(c) || matches!(c, ',' | '{' | '}' | '#' | '\'' | '|') {
break;
}
j += c.len_utf8();
}
if j == name_start {
return Err(IcuError);
}
j = skip_ws(s, j);
let b = s.as_bytes();
match b.get(j) {
Some(b'}') => return Ok((Part::Simple(s[start..=j].to_string()), j + 1)),
Some(b',') => {}
_ => return Err(IcuError),
}
j = skip_ws(s, j + 1);
let ty_start = j;
while b.get(j).is_some_and(u8::is_ascii_alphabetic) {
j += 1;
}
if j == ty_start {
return Err(IcuError);
}
let kind_word = &s[ty_start..j];
j = skip_ws(s, j);
match b.get(j) {
Some(b'}') => return Ok((Part::Simple(s[start..=j].to_string()), j + 1)),
Some(b',') => {}
_ => return Err(IcuError),
}
let kind = match kind_word {
"plural" => Kind::Plural,
"selectordinal" => Kind::SelectOrdinal,
"select" => Kind::Select,
_ => {
let mut level = 1usize;
let mut k = j + 1;
while k < b.len() && level > 0 {
match b[k] {
b'{' => level += 1,
b'}' => level -= 1,
b'\'' => match s[k + 1..].find('\'') {
Some(p) => k = k + 1 + p,
None => k = b.len(),
},
_ => {}
}
k += 1;
}
if level > 0 {
return Err(IcuError);
}
return Ok((Part::Simple(s[start..k].to_string()), k));
}
};
j = skip_ws(s, j + 1);
if kind != Kind::Select {
j = offset(s, j).unwrap_or(j);
}
let head = s[start..j].to_string();
let mut branches = Vec::new();
loop {
if j >= b.len() {
return Err(IcuError);
}
if b[j] == b'}' {
break;
}
let sel_end = selector_end(s, j).ok_or(IcuError)?;
let selector = s[j..sel_end].to_string();
let k = skip_ws(s, sel_end);
if b.get(k) != Some(&b'{') {
return Err(IcuError);
}
let (msg, k2) = message(s, k + 1, depth + 1, kind != Kind::Select, true)?;
if b.get(k2) != Some(&b'}') {
return Err(IcuError);
}
let t = skip_ws(s, k2 + 1);
branches.push(Branch { selector, mid: s[sel_end..k].to_string(), msg, trail: s[k2 + 1..t].to_string() });
j = t;
}
if branches.is_empty() {
return Err(IcuError);
}
Ok((Part::Complex(Complex { head, kind, branches }), j + 1))
}
fn offset(s: &str, j: usize) -> Option<usize> {
let rest = s.get(j..)?.strip_prefix("offset")?;
let mut k = skip_ws(s, s.len() - rest.len());
if s.as_bytes().get(k) != Some(&b':') {
return None;
}
k = skip_ws(s, k + 1);
let d0 = k;
while s.as_bytes().get(k).is_some_and(u8::is_ascii_digit) {
k += 1;
}
if k == d0 {
return None;
}
Some(skip_ws(s, k))
}
fn selector_end(s: &str, j: usize) -> Option<usize> {
let b = s.as_bytes();
if b.get(j) == Some(&b'=') {
let mut k = j + 1;
if b.get(k) == Some(&b'-') {
k += 1;
}
let d0 = k;
while b.get(k).is_some_and(u8::is_ascii_digit) {
k += 1;
}
if k > d0 {
if b.get(k) == Some(&b'.') && b.get(k + 1).is_some_and(u8::is_ascii_digit) {
k += 1;
while b.get(k).is_some_and(u8::is_ascii_digit) {
k += 1;
}
}
return Some(k);
}
}
let mut k = j;
while let Some(c) = ch(s, k) {
if is_ws(c) || c == '{' || c == '}' {
break;
}
k += c.len_utf8();
}
(k > j).then_some(k)
}
pub fn translatable(parts: &[Part], locos: bool) -> bool {
parts.iter().any(|p| match p {
Part::Text(t) | Part::Simple(t) => has_words(t, locos),
_ => false,
})
}
pub fn has_words(t: &str, locos: bool) -> bool {
const OPEN: &str = "\u{27E6}x";
const CLOSE: char = '\u{27E7}';
if !locos || !t.contains(OPEN) {
return crate::py_has_alnum(t);
}
let mut rest = t;
let mut kept = String::new();
while let Some(at) = rest.find(OPEN) {
let after = &rest[at + OPEN.len()..];
let d = after.chars().take_while(|c| crate::py_isdecimal_char(*c)).map(char::len_utf8).sum::<usize>();
if d > 0 && after[d..].starts_with(CLOSE) {
kept.push_str(&rest[..at]);
rest = &after[d + CLOSE.len_utf8()..];
} else {
let skip = at + '\u{27E6}'.len_utf8();
kept.push_str(&rest[..skip]);
rest = &rest[skip..];
}
}
kept.push_str(rest);
crate::py_has_alnum(&kept)
}
pub fn arg_name(head: &str) -> String {
let inner = head.trim_start_matches('{').trim_start();
inner.split([',', '}']).next().unwrap_or("").trim().to_string()
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BranchSlot {
pub group: usize,
pub parent: Option<usize>,
pub kind: Kind,
pub arg: String,
pub category: String,
pub added: bool,
}
#[derive(Debug, Clone)]
pub struct Level<'a> {
pub parts: &'a [Part],
pub path: String,
pub slot: Option<BranchSlot>,
}
pub fn missing<'c>(cx: &Complex, cats: &[&'c str]) -> Vec<&'c str> {
if cx.kind == Kind::Select || cx.other().is_none() {
return vec![];
}
cats.iter().copied().filter(|c| !cx.branches.iter().any(|b| b.selector == *c)).collect()
}
pub fn levels<'a>(parts: &'a [Part], cats: &dyn Fn(Kind) -> Option<Vec<&'static str>>) -> Vec<Level<'a>> {
let mut out = Vec::new();
let mut groups = 0usize;
walk(parts, String::new(), None, false, cats, &mut groups, &mut out);
out
}
fn walk<'a>(
parts: &'a [Part],
path: String,
slot: Option<BranchSlot>,
added: bool,
cats: &dyn Fn(Kind) -> Option<Vec<&'static str>>,
groups: &mut usize,
out: &mut Vec<Level<'a>>,
) {
let parent = slot.as_ref().map(|s| s.group);
out.push(Level { parts, path: path.clone(), slot });
for p in parts {
let Part::Complex(cx) = p else { continue };
let group = *groups;
*groups += 1;
let arg = cx.arg();
let here = if path.is_empty() { String::new() } else { format!("{path} / ") };
let branch = |selector: &str, added: bool| BranchSlot { group, parent, kind: cx.kind, arg: arg.clone(), category: selector.to_string(), added };
for b in &cx.branches {
walk(&b.msg, format!("{here}{arg}: {} {}", cx.kind.word(), b.selector), Some(branch(&b.selector, added)), added, cats, groups, out);
}
if cx.kind == Kind::Select {
continue;
}
let (Some(want), Some(other)) = (cats(cx.kind), cx.other()) else { continue };
for c in missing(cx, &want) {
walk(&other.msg, format!("{here}{arg}: {} {c}", cx.kind.word()), Some(branch(c, true)), true, cats, groups, out);
}
}
}
pub const ADDED_HIDDEN: &str = "icu_added";
pub fn added_matches(unit: &crate::Unit, added_path: Option<&str>) -> bool {
unit.hidden(ADDED_HIDDEN) == added_path
}
pub fn key_added_mark(category: &str, cats: &[&str]) -> String {
format!("{category} of {}", cats.join(" "))
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum KeyAdded<'u> {
No,
Fits(&'u str),
Foreign,
}
pub fn key_added<'u>(unit: &'u crate::Unit, cats: Option<&[&str]>) -> KeyAdded<'u> {
let Some((cat, list)) = unit.hidden(ADDED_HIDDEN).and_then(|m| m.split_once(" of ")) else { return KeyAdded::No };
match cats {
Some(cats) if list.split(' ').eq(cats.iter().copied()) && cats.contains(&cat) => KeyAdded::Fits(cat),
_ => KeyAdded::Foreign,
}
}
pub fn has_any_target(unit: &crate::Unit) -> bool {
unit.segments.iter().any(|s| s.has_target())
}
pub fn foreign_added_error(unit: &crate::Unit) -> crate::Error {
crate::Error::TargetChanged { unit: unit.id.clone() }
}
pub fn first_foreign_added<'u>(units: &'u [crate::Unit], fits: &std::collections::HashSet<&str>) -> Option<&'u crate::Unit> {
units.iter().find(|u| u.hidden(ADDED_HIDDEN).is_some_and(|m| !fits.contains(m)))
}
pub fn key_added_copy(other: &crate::Unit, id: impl Into<String>, category: &str, cats: &[&str], slot: Option<crate::PluralSlot>) -> crate::Unit {
let mut u = other.clone();
let id = id.into();
let old = std::mem::take(&mut u.id);
for s in u.segments.iter_mut().filter(|s| s.id == old) {
s.id = id.clone();
}
u.id = id;
u.set_hidden(ADDED_HIDDEN, key_added_mark(category, cats));
u.set_plural(slot.map(|s| s.with_added(true)));
u
}
pub fn place_added(units: &mut Vec<crate::Unit>, parts: &mut [crate::SkelPart]) {
use std::collections::HashMap;
let is_added = |u: &crate::Unit| u.plural().is_some_and(|p| p.is_added());
if !units.iter().any(is_added) {
return;
}
let mut last: HashMap<&str, usize> = HashMap::new();
for (i, u) in units.iter().enumerate() {
let Some(p) = u.plural().filter(|_| !is_added(u)) else { continue };
last.insert(p.group(), i);
if let Some(parent) = p.parent() {
last.insert(parent, i);
}
}
let mut after: HashMap<usize, Vec<usize>> = HashMap::new();
let mut moved = vec![false; units.len()];
for (i, u) in units.iter().enumerate() {
if !is_added(u) {
continue;
}
if let Some(&at) = u.plural().and_then(|p| last.get(p.group())) {
after.entry(at).or_default().push(i);
moved[i] = true;
}
}
let mut order: Vec<usize> = Vec::with_capacity(units.len());
for (i, &m) in moved.iter().enumerate() {
if !m {
order.push(i);
}
if let Some(list) = after.get(&i) {
order.extend(list.iter().copied());
}
}
if order.iter().enumerate().all(|(k, &i)| k == i) {
return;
}
apply_order(units, parts, &order);
}
fn apply_order(units: &mut Vec<crate::Unit>, parts: &mut [crate::SkelPart], order: &[usize]) {
let mut new_at = vec![usize::MAX; units.len()];
for (k, &i) in order.iter().enumerate() {
new_at[i] = k;
}
for p in parts.iter_mut() {
if let crate::SkelPart::Unit(i) = p {
if let Some(&n) = new_at.get(*i).filter(|n| **n != usize::MAX) {
*i = n;
}
}
}
let mut old: Vec<Option<crate::Unit>> = std::mem::take(units).into_iter().map(Some).collect();
units.extend(order.iter().map(|&i| old[i].take().expect("each unit once")));
}
#[doc(hidden)]
pub fn reorder_added(doc: &crate::Document, keep: bool) -> crate::Document {
let mut d = doc.clone();
let added = |u: &crate::Unit| u.plural().is_some_and(|p| p.is_added());
let mut order: Vec<usize> = (0..d.units.len()).filter(|&i| !added(&d.units[i])).collect();
if keep {
order.extend((0..d.units.len()).filter(|&i| added(&d.units[i])));
}
let mut none: Vec<crate::SkelPart> = Vec::new();
let parts = match &mut d.skeleton {
crate::Skeleton::Text { parts, .. } => parts,
_ => &mut none,
};
apply_order(&mut d.units, parts, &order);
d
}
pub fn suffix_plural_slots(units: &mut [crate::Unit], key: &dyn Fn(&crate::Unit) -> Option<String>) {
const WORDS: [&str; 6] = ["Zero", "One", "Two", "Few", "Many", "Other"];
let mut groups: std::collections::HashMap<String, Vec<(usize, &'static str)>> = std::collections::HashMap::new();
for (i, u) in units.iter().enumerate() {
let Some(k) = key(u) else { continue };
if !k.bytes().all(|b| b.is_ascii_alphanumeric() || b == b'_') {
continue;
}
if let Some((stem, w)) = WORDS.iter().find_map(|w| k.strip_suffix(w).filter(|s| !s.is_empty()).map(|s| (s.to_string(), *w))) {
groups.entry(stem).or_default().push((i, w));
}
}
for (stem, forms) in groups {
let mut words: Vec<&str> = forms.iter().map(|f| f.1).collect();
words.sort_unstable();
words.dedup();
if words.len() < 2 || !words.contains(&"Other") {
continue;
}
for (i, w) in forms {
if units[i].plural.is_none() {
units[i].set_plural(key_slot(stem.as_str(), false, &w.to_ascii_lowercase(), false));
}
}
}
}
pub fn key_slot(group: impl Into<Box<str>>, ordinal: bool, category: &str, added: bool) -> crate::PluralSlot {
let kind = if ordinal { crate::PluralKind::Ordinal } else { crate::PluralKind::Cardinal };
crate::PluralSlot::new(group, kind).with_category(category).with_added(added)
}
pub fn render_branches(cx: &Complex, rendered: Vec<String>, added: &[(&str, String)], cats: Option<&[&str]>) -> String {
struct B {
selector: String,
mid: String,
text: String,
trail: String,
}
let mut list: Vec<B> = cx
.branches
.iter()
.zip(rendered)
.map(|(b, text)| B { selector: b.selector.clone(), mid: b.mid.clone(), text, trail: b.trail.clone() })
.collect();
let cats = if cx.kind == Kind::Select { None } else { cats };
if let Some(cats) = cats {
if let Some(o) = list.iter().find(|b| b.selector == "other") {
let (other_mid, other_text) = (o.mid.clone(), o.text.clone());
let mut kept: Vec<B> = list.into_iter().filter(|b| b.selector.starts_with('=') || cats.contains(&b.selector.as_str())).collect();
let missing: Vec<&str> = cats.iter().copied().filter(|c| !kept.iter().any(|b| b.selector == *c)).collect();
if !missing.is_empty() {
let idx = kept.iter().position(|b| b.selector == "other").unwrap_or(kept.len());
let sep = if idx > 0 && !kept[idx - 1].trail.is_empty() { kept[idx - 1].trail.clone() } else { " ".to_string() };
let new = missing.iter().map(|c| {
let text = added.iter().find(|(a, _)| a == c).map_or_else(|| other_text.clone(), |(_, t)| t.clone());
B { selector: (*c).to_string(), mid: other_mid.clone(), text, trail: sep.clone() }
});
let tail = kept.split_off(idx);
kept.extend(new);
kept.extend(tail);
}
list = kept;
}
}
let mut out = cx.head.clone();
for b in &list {
out.push_str(&b.selector);
out.push_str(&b.mid);
out.push('{');
out.push_str(&b.text);
out.push('}');
out.push_str(&b.trail);
}
out.push('}');
out
}
pub fn plural_slot(message: &str, s: &BranchSlot) -> crate::PluralSlot {
let mut p = crate::PluralSlot::new(format!("{message}#{}", s.group), s.kind.plural_kind()).with_category(s.category.as_str()).with_added(s.added);
if !s.arg.is_empty() {
p = p.with_arg(s.arg.as_str());
}
if let Some(g) = s.parent {
p = p.with_parent(format!("{message}#{g}"));
}
p
}
#[cfg(test)]
mod tests {
use super::*;
fn render(parts: &[Part]) -> String {
let mut out = String::new();
for p in parts {
match p {
Part::Text(t) | Part::Simple(t) => out.push_str(t),
Part::Pound => out.push('#'),
Part::Complex(cx) => {
out.push_str(&cx.head);
for b in &cx.branches {
out.push_str(&format!("{}{}{{{}}}{}", b.selector, b.mid, render(&b.msg), b.trail));
}
out.push('}');
}
}
}
out
}
fn none(_: Kind) -> Option<Vec<&'static str>> {
None
}
#[test]
fn every_byte_is_kept() {
for s in [
"plain",
"Hello {name}!",
"{n, plural, =0{none} one{# x} other{# xs}}",
"{ count , plural , offset:1 one { # item } other { # items } }",
"{g, select, a{A} other{B}} tail",
"It''s '{quoted}' text",
"{d, date, ::yMMMd} and {n, number, '{'}",
"{a, plural, one{{b, select, x{X} other{Y}}} other{Z}}",
"{g, select, female{{n, plural, one{# her} other{# hers}}} other{{n, plural, one{# their} other{# theirs}}}}",
"'#' {n, plural, one{'#' # '{x}'} other{''#''}}",
] {
let parts = parse(s).unwrap_or_else(|_| panic!("{s}"));
assert_eq!(render(&parts), s);
}
}
#[test]
fn nested_select_inside_plural() {
let parts = parse("{n, plural, one{{g, select, f{she has #} other{they have #}}} other{# items}}").unwrap();
let Part::Complex(cx) = &parts[0] else { panic!() };
assert_eq!((cx.kind, cx.arg(), cx.branches.len()), (Kind::Plural, "n".to_string(), 2));
let Part::Complex(inner) = &cx.branches[0].msg[0] else { panic!() };
assert_eq!(inner.kind, Kind::Select);
assert_eq!(inner.branches[0].msg, [Part::Text("she has #".into())]);
let lv = levels(&parts, &none);
let paths: Vec<&str> = lv.iter().map(|l| l.path.as_str()).collect();
assert_eq!(paths, ["", "n: plural one", "n: plural one / g: select f", "n: plural one / g: select other", "n: plural other"]);
let s = lv[2].slot.as_ref().unwrap();
assert_eq!((s.group, s.parent, s.kind, s.arg.as_str(), s.category.as_str(), s.added), (1, Some(0), Kind::Select, "g", "f", false));
assert_eq!(lv[4].slot.as_ref().unwrap().group, 0);
assert!(lv[0].slot.is_none());
}
#[test]
fn pound_exact_offsets_and_quotes() {
assert_eq!(parse("# {g, select, a{#} other{#}}").unwrap().len(), 2);
let parts = parse("{n, plural, one{#} other{##}}").unwrap();
let Part::Complex(cx) = &parts[0] else { panic!() };
assert_eq!(cx.branches[1].msg, [Part::Pound, Part::Pound]);
let parts = parse("{n, plural, =0{none} =-1{minus} =1.5{half} other{#}}").unwrap();
let Part::Complex(cx) = &parts[0] else { panic!() };
let sel: Vec<&str> = cx.branches.iter().map(|b| b.selector.as_str()).collect();
assert_eq!(sel, ["=0", "=-1", "=1.5", "other"]);
let parts = parse("{ n , plural , offset : 2 one{x} other{y}}").unwrap();
let Part::Complex(cx) = &parts[0] else { panic!() };
assert_eq!(cx.head, "{ n , plural , offset : 2 ");
assert_eq!(cx.arg(), "n");
assert!(parse("{g, select, offset:1 other{x}}").is_err());
assert_eq!(parse("It''s '{x}' ok").unwrap(), [Part::Text("It''s '{x}' ok".into())]);
assert_eq!(parse("don't {x}").unwrap(), [Part::Text("don't ".into()), Part::Simple("{x}".into())]);
let parts = parse("{n, plural, other{'#' is #}}").unwrap();
let Part::Complex(cx) = &parts[0] else { panic!() };
assert_eq!(cx.branches[0].msg, [Part::Text("'#' is ".into()), Part::Pound]);
assert!(matches!(parse("{n, number, ::compact-short}").unwrap()[0], Part::Simple(_)));
assert!(matches!(parse("{n, plural}").unwrap()[0], Part::Simple(_)));
}
#[test]
fn rejects_what_localization_os_rejects() {
let deep = format!("{}x{}", "{a, select, other{".repeat(60), "}".repeat(60));
for bad in ["{", "}", "{n, plural, one{x}", "{n, plural}x{", "{n, plural, }", "{n, plural, one x}", "{, plural, other{x}}", "'{open", "{n, number, {x}", deep.as_str()] {
assert!(parse(bad).is_err(), "{bad}");
}
}
#[test]
fn the_locos_flag_counts_literal_tokens_as_no_words() {
assert!(has_words("\u{27E6}x1\u{27E7}", false));
assert!(!has_words("\u{27E6}x1\u{27E7}", true));
assert!(has_words("\u{27E6}x1\u{27E7} files", true));
assert!(has_words("\u{27E6}xa\u{27E7}", true));
assert!(!translatable(&[Part::Pound, Part::Text(" ".into())], false));
}
#[test]
fn missing_categories_become_added_copies_of_other() {
let parts = parse("{n, plural, =0{none} one{# file} other{# files}} {g, select, a{A} other{B}}").unwrap();
let pl = |k: Kind| (k == Kind::Plural).then(|| vec!["one", "few", "many", "other"]);
let lv = levels(&parts, &pl);
let got: Vec<(&str, bool)> = lv.iter().map(|l| (l.path.as_str(), l.slot.as_ref().is_some_and(|s| s.added))).collect();
assert_eq!(
got,
[
("", false),
("n: plural =0", false),
("n: plural one", false),
("n: plural other", false),
("n: plural few", true),
("n: plural many", true),
("g: select a", false),
("g: select other", false),
]
);
assert_eq!(lv[4].parts, lv[3].parts);
let parts = parse("{n, plural, one{x}}").unwrap();
assert_eq!(levels(&parts, &pl).len(), 2);
}
#[test]
fn render_fills_missing_categories_from_other_or_the_added_text() {
let parts = parse("{n, plural, one{# file} other{# files}}").unwrap();
let Part::Complex(cx) = &parts[0] else { panic!() };
let r = || vec!["# plik".to_string(), "# plików".to_string()];
let cats = ["one", "few", "many", "other"];
assert_eq!(render_branches(cx, r(), &[], Some(&cats)), "{n, plural, one{# plik} few{# plików} many{# plików} other{# plików}}");
assert_eq!(render_branches(cx, r(), &[("few", "# pliki".into())], Some(&cats)), "{n, plural, one{# plik} few{# pliki} many{# plików} other{# plików}}");
assert_eq!(render_branches(cx, r(), &[("few", "x".into())], None), "{n, plural, one{# plik} other{# plików}}");
}
#[test]
fn key_style_added_forms_fit_only_the_same_categories() {
let other = crate::Unit::new("u3", vec![crate::Inline::text("# files")]);
let slot = key_slot("files", false, "other", false);
let u = key_added_copy(&other, "u3.few", "few", &["one", "few", "many", "other"], Some(slot.clone().with_category("few")));
assert_eq!((u.id.as_str(), u.segments[0].id.as_str()), ("u3.few", "u3.few"));
assert_eq!(u.hidden(ADDED_HIDDEN), Some("few of one few many other"));
assert!(u.plural().is_some_and(|p| p.is_added() && p.category() == Some("few") && p.group() == "files"));
assert_eq!(key_added(&u, Some(&["one", "few", "many", "other"])), KeyAdded::Fits("few"));
assert_eq!(key_added(&u, Some(&["one", "two", "few", "other"])), KeyAdded::Foreign);
assert_eq!(key_added(&u, None), KeyAdded::Foreign);
assert_eq!(key_added(&other, Some(&["one", "other"])), KeyAdded::No);
let mut icu = other.clone();
icu.set_hidden(ADDED_HIDDEN, "count: plural few");
assert_eq!(key_added(&icu, Some(&["one", "few", "many", "other"])), KeyAdded::No);
assert!(!has_any_target(&u));
}
#[test]
fn added_forms_move_after_their_group_and_the_skeleton_follows() {
let unit = |id: &str, group: Option<&str>, added: bool| {
let mut u = crate::Unit::new(id, vec![crate::Inline::text(id)]);
if let Some(g) = group {
u.set_plural(key_slot(g, false, "other", added));
}
u
};
let mut units = vec![unit("a", Some("g1"), false), unit("b", Some("g1"), false), unit("c", None, false), unit("d", Some("g2"), false), unit("b.few", Some("g1"), true), unit("b.many", Some("g1"), true), unit("d.few", Some("g2"), true), unit("x.few", Some("gone"), true)];
let mut parts: Vec<crate::SkelPart> = (0..4).map(crate::SkelPart::Unit).collect();
place_added(&mut units, &mut parts);
let ids: Vec<&str> = units.iter().map(|u| u.id.as_str()).collect();
assert_eq!(ids, ["a", "b", "b.few", "b.many", "c", "d", "d.few", "x.few"]);
let at: Vec<usize> = parts.iter().map(|p| if let crate::SkelPart::Unit(i) = p { *i } else { usize::MAX }).collect();
assert_eq!(at, [0, 1, 4, 5]);
let doc = crate::Document::new("x", units, crate::Skeleton::text(parts, "utf-8", crate::ParserInfo::new("x", "0", 1)));
let back = reorder_added(&doc, true);
assert_eq!(back.units.iter().map(|u| u.id.as_str()).collect::<Vec<_>>(), ["a", "b", "c", "d", "b.few", "b.many", "d.few", "x.few"]);
let none = reorder_added(&doc, false);
assert_eq!(none.units.len(), 4);
let crate::Skeleton::Text { parts, .. } = &none.skeleton else { panic!() };
assert!(parts.iter().enumerate().all(|(k, p)| matches!(p, crate::SkelPart::Unit(i) if *i == k)));
}
#[test]
fn suffix_keys_group_by_stem() {
let mut units: Vec<crate::Unit> = ["itemCountOne", "itemCountOther", "phone", "lonelyOther", "aOne", "aMany"].iter().map(|k| {
let mut u = crate::Unit::new(*k, vec![]);
u.context.key = Some((*k).to_string());
u
}).collect();
suffix_plural_slots(&mut units, &|u| u.context.key.clone());
let got: Vec<Option<(&str, &str)>> = units.iter().map(|u| u.plural().map(|p| (p.group(), p.category().unwrap()))).collect();
assert_eq!(got, [Some(("itemCount", "one")), Some(("itemCount", "other")), None, None, None, None]);
}
#[test]
fn slots_name_groups_by_message() {
let s = BranchSlot { group: 1, parent: Some(0), kind: Kind::Select, arg: "g".into(), category: "f".into(), added: false };
let p = plural_slot("u3", &s);
assert_eq!((p.group(), p.parent(), p.category(), p.arg(), p.kind(), p.is_added()), ("u3#1", Some("u3#0"), Some("f"), Some("g"), crate::PluralKind::Select, false));
}
}