use super::{negotiate, Messages};
use crate::error::Result;
use std::cell::RefCell;
use std::rc::Rc;
use std::sync::RwLock;
use unic_langid::LanguageIdentifier;
static LANGUAGE: RwLock<Option<LanguageIdentifier>> = RwLock::new(None);
thread_local! {
static CARRIED: RefCell<Option<(LanguageIdentifier, Rc<Messages>)>> =
const { RefCell::new(None) };
}
pub fn install(requested: Option<&str>, stored: Option<&str>) -> Result<()> {
match negotiate::negotiate(requested, stored) {
Ok(language) => {
choose(language);
Ok(())
}
Err(failure) => {
choose(host_language());
Err(failure)
}
}
}
#[must_use]
pub fn active() -> Rc<Messages> {
let wanted = language();
CARRIED.with(|cell| {
let mut slot = cell.borrow_mut();
if let Some((built_for, carried)) = slot.as_ref() {
if *built_for == wanted {
return Rc::clone(carried);
}
}
let carried = Rc::new(
Messages::load(wanted.clone()).unwrap_or_else(|_| Messages::empty(wanted.clone())),
);
*slot = Some((wanted, Rc::clone(&carried)));
carried
})
}
fn language() -> LanguageIdentifier {
if let Some(chosen) = read() {
return chosen;
}
let host = host_language();
choose(host.clone());
host
}
fn host_language() -> LanguageIdentifier {
negotiate::negotiate(None, None)
.ok()
.or_else(|| negotiate::parse(negotiate::BUILTIN))
.unwrap_or_default()
}
fn read() -> Option<LanguageIdentifier> {
LANGUAGE.read().map_or_else(
|poisoned| poisoned.into_inner().clone(),
|guard| guard.clone(),
)
}
fn choose(language: LanguageIdentifier) {
match LANGUAGE.write() {
Ok(mut guard) => *guard = Some(language),
Err(poisoned) => *poisoned.into_inner() = Some(language),
}
}
#[cfg(test)]
mod tests {
use super::*;
static ORDER: std::sync::Mutex<()> = std::sync::Mutex::new(());
fn alone<T>(body: impl FnOnce() -> T) -> T {
let guard = ORDER
.lock()
.unwrap_or_else(std::sync::PoisonError::into_inner);
let before = read();
let outcome = body();
match before {
Some(language) => choose(language),
None => clear(),
}
drop(guard);
outcome
}
fn clear() {
match LANGUAGE.write() {
Ok(mut guard) => *guard = None,
Err(poisoned) => *poisoned.into_inner() = None,
}
}
#[test]
fn the_mark_on_the_command_line_decides_what_is_in_force() {
alone(|| {
install(Some("pt-BR"), None).expect("carried by this build");
assert_eq!(active().language().to_string(), "pt-BR");
});
}
#[test]
fn the_stored_value_decides_when_no_mark_was_given() {
alone(|| {
install(None, Some("pt-BR")).expect("carried by this build");
assert_eq!(active().language().to_string(), "pt-BR");
});
}
#[test]
fn the_mark_outranks_the_stored_value() {
alone(|| {
install(Some("en"), Some("pt-BR")).expect("carried by this build");
assert_eq!(active().language().to_string(), "en");
});
}
#[test]
fn a_second_invocation_is_never_served_the_first_ones_language() {
alone(|| {
install(Some("en"), None).expect("carried");
let english = active().format("copy-table", &[("table", "orders")]);
install(Some("pt-BR"), None).expect("carried");
let portuguese = active().format("copy-table", &[("table", "orders")]);
assert_eq!(active().language().to_string(), "pt-BR");
assert_ne!(english, portuguese, "the set outlived its decision");
});
}
#[test]
fn another_thread_reads_the_same_language() {
alone(|| {
install(Some("pt-BR"), None).expect("carried");
let elsewhere = std::thread::spawn(|| active().language().to_string())
.join()
.expect("the thread finished");
assert_eq!(elsewhere, "pt-BR");
});
}
#[test]
fn a_language_this_build_lacks_is_refused_and_still_leaves_messages_in_force() {
alone(|| {
clear();
let failure = install(Some("klingon"), None).expect_err("must refuse");
assert_eq!(failure.category(), crate::error::Category::Usage);
assert!(super::super::SUPPORTED.contains(&active().language().to_string().as_str()));
});
}
#[test]
fn a_failure_raised_before_the_language_was_settled_still_reads() {
alone(|| {
clear();
let carried = active();
assert!(super::super::SUPPORTED.contains(&carried.language().to_string().as_str()));
assert!(carried.carries("copy-table"));
});
}
}