use crate::ported::exec::{dispatch_function_call, execute_script_zsh_pipeline};
use crate::ported::modules::zutil::{lookupstyle, testforstyle};
use crate::ported::params::getsparam;
use crate::ported::utils::quotestring;
use crate::ported::zsh_h::QT_SINGLE;
fn call_cache_policy(policy: &str, cache_path: &str) -> i32 {
if let Some(rc) = dispatch_function_call(policy, &[cache_path.to_string()]) {
return rc;
}
let src = format!(
"{}{} {}",
"\n".repeat(18),
quotestring(policy, QT_SINGLE),
quotestring(cache_path, QT_SINGLE)
);
execute_script_zsh_pipeline(&src).unwrap_or(1)
}
pub fn _cache_invalid(args: &[String]) -> i32 {
let _fn_scope = crate::compsys::ported::shared::FnScope::enter("_cache_invalid");
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 cache_path = format!("{}/{}", cache_dir, cache_ident);
let policy = lookupstyle(&ctx, "cache-policy")
.first()
.cloned()
.unwrap_or_default();
if !policy.is_empty() && call_cache_policy(&policy, &cache_path) == 0 {
return 0;
}
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!(_cache_invalid(&["my-cache".to_string()]), 1);
}
}