use crate::{lang::Lang, path::DictionaryType};
const BASE_URL: &str = "https://huggingface.co/datasets/daxida/wty-release/resolve/main/latest";
fn source_str(dict_ty: DictionaryType, source: &Lang) -> &str {
match dict_ty {
DictionaryType::Main
| DictionaryType::Ipa
| DictionaryType::Glossary
| DictionaryType::GlossaryExtended => source.as_ref(),
DictionaryType::IpaMerged => "all",
}
}
fn download_url(
dict_ty: DictionaryType,
dict_name_expanded: &str,
source: Lang,
target: Lang,
) -> String {
let source_str = source_str(dict_ty, &source);
format!("{BASE_URL}/dict/{source_str}/{target}/{dict_name_expanded}.zip?download=true")
}
fn index_url(dict_name_expanded: &str) -> String {
format!("{BASE_URL}/index/{dict_name_expanded}-index.json?download=true")
}
pub fn get_index(
dict_ty: DictionaryType,
dict_name_expanded: &str,
source: Lang,
target: Lang,
) -> String {
let current_date = chrono::Utc::now().format("%Y.%m.%d"); let index_url = index_url(dict_name_expanded);
let download_url = download_url(dict_ty, dict_name_expanded, source, target);
let source_str = source_str(dict_ty, &source);
format!(
r#"{{
"title": "{dict_name_expanded}",
"format": 3,
"revision": "{current_date}",
"sequenced": true,
"author": "wty contributors",
"url": "https://github.com/yomidevs/wiktionary-to-yomitan",
"description": "Dictionaries for various language pairs generated from Wiktionary data, via Kaikki and wty.",
"attribution": "https://kaikki.org/",
"sourceLanguage": "{source_str}",
"targetLanguage": "{target}",
"isUpdatable": true,
"indexUrl": "{index_url}",
"downloadUrl": "{download_url}"
}}"#
)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn url_download_ipa() {
assert_eq!(
download_url(DictionaryType::Ipa, "wty-afb-en-ipa", Lang::Afb, Lang::En),
"https://huggingface.co/datasets/daxida/wty-release/resolve/main/latest/dict/afb/en/wty-afb-en-ipa.zip?download=true"
);
}
#[test]
fn url_download_ipa_merged() {
assert_eq!(
download_url(DictionaryType::IpaMerged, "wty-en-ipa", Lang::Afb, Lang::En),
"https://huggingface.co/datasets/daxida/wty-release/resolve/main/latest/dict/all/en/wty-en-ipa.zip?download=true"
);
}
#[test]
fn url_index() {
assert_eq!(
index_url("wty-afb-en-ipa"),
"https://huggingface.co/datasets/daxida/wty-release/resolve/main/latest/index/wty-afb-en-ipa-index.json?download=true"
);
}
}