Start implementing output tests, don't panic on missing basedir

This commit is contained in:
southerntofu 2020-11-27 19:37:46 +01:00
parent ae043f5cd2
commit 9ae8eaf3c2
3 changed files with 73 additions and 31 deletions

View File

@ -19,11 +19,12 @@ pub struct Cli {
}
impl Cli {
// Returns a PathBuf to the basedir. If it's a relative link,
// it's not expanded here! Panics if no basedir is provided and $HOME isn't defined
pub fn basedir(&self) -> PathBuf {
if let Some(basedir) = &self.basedir {
// Returns an error when the path doesn't exist
PathBuf::from(basedir)
.canonicalize()
.expect("failed to expand relative path")
} else {
let mut home_path = PathBuf::from(env::var("HOME").expect("No $HOME in env"));
home_path.push(".forgebuild");

View File

@ -7,10 +7,20 @@ use lazy_static::lazy_static;
lazy_static! {
static ref LOGLEVEL: LogLevel = LogLevel::from_env();
static ref LANG: String = lang_from_env();
static ref LANG: Lang = lang_from_env();
static ref TRANSLATIONS: HashMap<String, String> = load_translations();
}
/// Lang configures how to deal with translations. It has three possible values:
/// None: translations return their own key name
/// Replace: wtf to do with this?
/// Some(code): where code is the language code for the JSON translation file
pub enum Lang {
None,
Replace,
Some(String)
}
pub type Context<'a> = &'a HashMap<&'a str, &'a str>;
/// Returns a PathBuf containing the translations. Does not mean
@ -36,44 +46,64 @@ fn find_translations() -> PathBuf {
}
fn load_translations() -> HashMap<String, String> {
let mut path = find_translations();
if !path.is_dir() {
panic!("Could not find translations in {:?}", path);
}
path.push(format!("{}.json", *LANG));
if !path.is_file() {
panic!("Could not find translation file: {:?}", path);
}
match fs::read_to_string(&path) {
Ok(content) => {
//let trans: HashMap<String, String> = serde_json::from_str(&content).expect("Could not load translations");
//return trans
match serde_json::from_str(&content) {
Ok(trans) => trans,
Err(e) => panic!("JSON ERROR: {}", e),
match &*LANG {
Lang::Some(lang) => {
let mut path = find_translations();
if !path.is_dir() {
panic!("Could not find translations in {:?}", path);
}
}
Err(e) => {
panic!("IO ERROR: {}", e);
path.push(format!("{}.json", lang));
if !path.is_file() {
panic!("Could not find translation file: {:?}", path);
}
match fs::read_to_string(&path) {
Ok(content) => {
//let trans: HashMap<String, String> = serde_json::from_str(&content).expect("Could not load translations");
//return trans
match serde_json::from_str(&content) {
Ok(trans) => trans,
Err(e) => panic!("JSON ERROR: {}", e),
}
}
Err(e) => {
panic!("IO ERROR: {}", e);
}
}
},
_ => {
HashMap::new()
}
}
}
fn trans(key: &str) -> String {
match TRANSLATIONS.get(key) {
Some(t) => t.to_string(),
None => {
panic!("Unknown translation string in lang {}: {}", *LANG, key);
match &*LANG {
Lang::Some(lang) => match TRANSLATIONS.get(key) {
Some(t) => t.to_string(),
None => {
panic!("Unknown translation string in lang {}: {}", lang, key);
}
},
Lang::None => {
// Return the requested key itself (for output tests)
key.to_string()
},
Lang::Replace => {
unimplemented!("Don't know what to do with this yet but we need a way to test variable substitution");
}
}
}
fn lang_from_env() -> String {
fn lang_from_env() -> Lang {
let lang =
env::var("LANG").expect("$LANG not set in environment. Your machine is misconfigured!");
lang[0..2].to_string()
match lang.as_str() {
"NONE" => Lang::None,
"REPLACE" => Lang::Replace,
_ => Lang::Some(lang[0..2].to_string())
}
}
struct LogLevel {

View File

@ -11,7 +11,18 @@ mod task;
fn main() -> Result<(), std::io::Error> {
let cmd = cli::Cli::from_args();
let base_dir: String = cmd.basedir().to_str().unwrap().into();
let mut context = HashMap::new();
let base_dir = cmd.basedir();
let base_dir = match base_dir.canonicalize() {
Ok(p) => p.to_str().unwrap().to_string(),
Err(_) => {
context.insert("i18n_basedir", base_dir.to_str().unwrap());
log::error("missing_basedir", Some(&context));
std::process::exit(1);
}
};
set_var("GITBUILDDIR", &base_dir);
@ -36,7 +47,7 @@ fn main() -> Result<(), std::io::Error> {
continue;
}
let mut context = HashMap::new();
let mut context = context.clone();
context.insert("$i18n_task", task.name.as_str());
log::debug("found_task", Some(&context));
// Maybe the task has a source we should clone?