diff --git a/src/cli.rs b/src/cli.rs index 406152d..aa7c0ea 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -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"); diff --git a/src/log.rs b/src/log.rs index f2423a4..d2fb656 100644 --- a/src/log.rs +++ b/src/log.rs @@ -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 = 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 { - 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 = 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 = 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 { diff --git a/src/main.rs b/src/main.rs index 6c5ea40..b9659a6 100644 --- a/src/main.rs +++ b/src/main.rs @@ -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?