From e5b6e24a621694aa51f8f9301886bd2ccc24be05 Mon Sep 17 00:00:00 2001 From: Luca Matei Pintilie Date: Sun, 17 Sep 2023 22:13:44 +0200 Subject: Add default cache path if XDG_CACHE_HOME does not exist If the XDG_CACHE_HOME environmental variable does not exist then mmv will default to "$HOME/.cache"[1] If HOME does not exist then it will panic like before [1]: https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html Signed-off-by: Luca Matei Pintilie --- src/main.rs | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/src/main.rs b/src/main.rs index 9885a82..883e2df 100644 --- a/src/main.rs +++ b/src/main.rs @@ -176,12 +176,20 @@ fn work() -> Result<(), io::Error> { let ts = require!(SystemTime::now().duration_since(UNIX_EPOCH)) .as_nanos() .to_string(); - let cache_base = env::var("XDG_CACHE_HOME").unwrap_or_else(|_| { - err!("XDG_CACHE_HOME variable must be set"); - }); + let cache_base = match env::var("XDG_CACHE_HOME") { + Ok(s) => PathBuf::from(s), + _ => [ + &env::var("HOME").unwrap_or_else(|_| { + err!("XDG_CACHE_HOME or HOME variable must be set"); + }), + ".cache", + ] + .iter() + .collect::(), + }; let mmv_name = option_env!("MMV_NAME").unwrap_or(MMV_DEFAULT_NAME); cache_dir = [ - Path::new(cache_base.as_str()), + cache_base.as_path(), Path::new(mmv_name), Path::new(ts.as_str()), ] -- cgit v1.2.3