diff options
author | Luca Matei Pintilie <luca@lucamatei.com> | 2023-09-17 22:13:44 +0200 |
---|---|---|
committer | Thomas Voss <mail@thomasvoss.com> | 2023-09-18 12:33:39 +0200 |
commit | e5b6e24a621694aa51f8f9301886bd2ccc24be05 (patch) | |
tree | 48504b7af992c543bc1437c32cad132f300cb2c3 /src | |
parent | 4ddadd7b64627269c3d9b92b66633969402b7e03 (diff) |
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 <luca@lucamatei.com>
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 16 |
1 files 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::<PathBuf>(), + }; 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()), ] |