#![allow(dead_code, reason = "each test uses the part it needs")]
use mf2::{
ArgValue, CatalogFile, Corpus, Dir, Function, MsgId, Registry, Tr, TrArgs, functions, tr,
tr_args1,
};
use mf2_catalog::Manifest;
use mf2_catalog::writer::{self, Options};
static FUNCTIONS: [(&str, &dyn Function); 1] = [("integer", &functions::INTEGER)];
pub(crate) static REGISTRY: Registry = Registry::new(&FUNCTIONS);
pub(crate) const MESSAGES: [(&str, &[&str]); 3] = [("m0", &[]), ("m1", &["name"]), ("m2", &["n"])];
pub(crate) const LANGUAGES: [(&str, Dir, [&str; 3]); 2] = [
(
"en",
Dir::Ltr,
["Welcome", "Hello, {$name}!", "{$n :integer} files"],
),
(
"fr",
Dir::Ltr,
["Bienvenue", "Bonjour, {$name} !", "{$n :integer} fichiers"],
),
];
pub(crate) fn welcome() -> Tr {
tr(MsgId::from_raw(0))
}
pub(crate) fn hello(name: &'static str) -> TrArgs {
tr_args1(MsgId::from_raw(1), ArgValue::str_static(name))
}
pub(crate) fn files(n: i64) -> TrArgs {
tr_args1(MsgId::from_raw(2), ArgValue::from(n))
}
pub(crate) fn corpus(embed: bool) -> (&'static Corpus, Vec<(&'static str, Vec<u8>)>) {
corpus_of(&LANGUAGES, embed)
}
pub(crate) fn corpus_of(
languages: &[(&'static str, Dir, [&str; 3])],
embed: bool,
) -> (&'static Corpus, Vec<(&'static str, Vec<u8>)>) {
let manifest = Manifest {
ids: MESSAGES.iter().map(|(id, _)| (*id).to_owned()).collect(),
slots: MESSAGES
.iter()
.map(|(_, slots)| slots.iter().map(|s| (*s).to_owned()).collect())
.collect(),
markup: vec![Vec::new(); MESSAGES.len()],
functions: vec!["integer".to_owned()],
};
let mut locales = Vec::new();
let mut files = Vec::new();
let mut raw = Vec::new();
for &(tag, dir, sources) in languages {
let parsed: Vec<_> = sources
.iter()
.map(|source| {
mf2_syntax::parse_model(source)
.message
.expect("the test message parses")
})
.collect();
let messages: Vec<_> = parsed.iter().map(Some).collect();
let bytes = writer::catalog(&manifest, &messages, &Options::new(tag, dir))
.expect("the test catalog writes");
let bytes: &'static [u8] = Box::leak(bytes.into_boxed_slice());
let name: &'static str =
Box::leak(format!("{tag}.{}.mf2b", mf2_catalog::content_hash(bytes)).into_boxed_str());
locales.push((tag, dir));
files.push(CatalogFile::new(tag, name, embed.then_some(bytes)));
raw.push((name, bytes.to_vec()));
}
let corpus = Corpus::new(
languages[0].0,
manifest.hash(),
Box::leak(locales.into_boxed_slice()),
®ISTRY,
Box::leak(files.into_boxed_slice()),
)
.with_language_matching(mf2::LanguageMatching::cldr());
(Box::leak(Box::new(corpus)), raw)
}
pub(crate) struct TempDir(pub(crate) std::path::PathBuf);
impl TempDir {
pub(crate) fn with(label: &str, files: &[(&str, &[u8])]) -> TempDir {
let root = std::env::temp_dir().join(format!("mf2-{label}-{}", std::process::id()));
let _ = std::fs::remove_dir_all(&root);
std::fs::create_dir_all(&root).expect("the temporary directory is created");
for (name, bytes) in files {
std::fs::write(root.join(name), bytes).expect("the catalog is written");
}
TempDir(root)
}
}
impl Drop for TempDir {
fn drop(&mut self) {
let _ = std::fs::remove_dir_all(&self.0);
}
}