use crate::compsys::ported::_cache_invalid::_cache_invalid;
use crate::compsys::ported::_message::_message;
use crate::ported::exec_hooks::dispatch_function_call;
use crate::ported::modules::zutil::{lookupstyle, testforstyle};
use crate::ported::params::getsparam;
use std::path::Path;
pub fn _retrieve_cache(args: &[String]) -> i32 {
let cache_ident = args.first().cloned().unwrap_or_default();
let curcontext = getsparam("curcontext").unwrap_or_default();
let ctx = format!(":completion:{}:", curcontext);
if testforstyle(&ctx, "use-cache") != 0 {
return 1;
}
let cache_dir = lookupstyle(&ctx, "cache-path")
.first()
.cloned()
.unwrap_or_else(|| {
let home = getsparam("ZDOTDIR")
.filter(|s| !s.is_empty())
.or_else(|| getsparam("HOME"))
.unwrap_or_default();
format!("{}/.zcompcache", home)
});
let dir_meta = std::fs::metadata(&cache_dir);
match dir_meta {
Ok(m) if m.is_dir() => {}
Ok(_) => {
let _ = _message(&[format!(
"cache-dir ({}) isn't a directory!",
cache_dir
)]);
return 1;
}
Err(_) => return 1,
}
let cache_path = format!("{}/{}", cache_dir, cache_ident);
if Path::new(&cache_path).exists() {
if _cache_invalid(&[cache_ident]) == 0 {
return 1;
}
let _ = dispatch_function_call("source", &[cache_path]);
0
} else {
1
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn returns_one_when_use_cache_disabled() {
let _g = crate::test_util::global_state_lock();
assert_eq!(_retrieve_cache(&["my-cache".to_string()]), 1);
}
}