use crate::ShunError;
pub const DEFAULT_REPO: &str = "celestia-island/sysl";
pub const DEFAULT_BRANCH: &str = "main";
pub fn sysl_dir(locale: &str) -> Option<&'static str> {
match locale {
"zh-Hans" | "zh" | "zh-CN" => Some("zhs"),
"zh-Hant" | "zh-TW" | "zh-HK" => Some("zht"),
"ja" => Some("ja"),
"ko" => Some("ko"),
"fr" => Some("fr"),
"ru" => Some("ru"),
"es" => Some("es"),
"de" => Some("de"),
"pt" => Some("pt"),
"ar" => Some("ar"),
_ => None,
}
}
fn raw_url(repo: &str, branch: &str, path: &str) -> String {
format!("https://raw.githubusercontent.com/{repo}/{branch}/{path}")
}
fn fetch_text(url: &str) -> Result<String, ShunError> {
ureq::get(url)
.call()
.map_err(|e| ShunError::Config(format!("license fetch `{url}`: {e}")))?
.into_string()
.map_err(|e| ShunError::Config(format!("license read `{url}`: {e}")))
}
pub fn fetch_locale(repo: &str, branch: &str, locale: &str) -> Result<String, ShunError> {
let path = match locale {
"en" => "LICENSE.txt".to_string(),
other => {
let dir = sysl_dir(other).ok_or_else(|| {
ShunError::Config(format!("no SySL translation for locale `{other}`"))
})?;
format!("i18n/{dir}/LICENSE.txt")
}
};
fetch_text(&raw_url(repo, branch, &path))
}