use std::path::{Path, PathBuf};
use std::sync::{Mutex, OnceLock};
use rustc_hash::FxHashMap;
use super::FallowConfig;
struct OutsideValue {
field: &'static str,
value: PathBuf,
files: String,
}
static OUTSIDE_VALUES: OnceLock<Mutex<FxHashMap<PathBuf, Vec<OutsideValue>>>> = OnceLock::new();
fn root_key(root: &Path) -> PathBuf {
dunce::canonicalize(root).unwrap_or_else(|_| root.to_path_buf())
}
impl FallowConfig {
pub fn record_outside_values(&self, root: &Path) {
let files = self.outside_keys.file_list();
let mut values: Vec<OutsideValue> = self
.file_path_keys()
.into_iter()
.filter_map(|(value, field)| value.map(|value| (PathBuf::from(value), field)))
.chain(self.cache.dir.clone().map(|dir| (dir, "cache.dir")))
.filter(|(_, field)| self.outside_keys.covers(field))
.map(|(value, field)| OutsideValue {
field,
value,
files: files.clone(),
})
.collect();
let registry = OUTSIDE_VALUES.get_or_init(Mutex::default);
if let Ok(mut map) = registry.lock() {
if values.is_empty() {
map.remove(&root_key(root));
} else {
values.shrink_to_fit();
map.insert(root_key(root), values);
}
}
}
}
#[must_use]
pub fn outside_value_files(root: &Path, field: &str, path: &Path) -> Option<String> {
let normalized = |path: &Path| {
let joined = root.join(path);
let absolute = std::path::absolute(&joined).unwrap_or(joined);
fallow_types::path_guard::lexically_normalized(&absolute)
};
let map = OUTSIDE_VALUES.get()?.lock().ok()?;
map.get(&root_key(root))?
.iter()
.find(|entry| entry.field == field && normalized(&entry.value) == normalized(path))
.map(|entry| entry.files.clone())
}
#[must_use]
pub fn outside_value_error(root: &Path, field: &str, path: &Path) -> Option<String> {
outside_value_files(root, field, path).map(|files| {
format!(
"{field}: the file that a config file outside the project ({files}) names does not \
load. The text of a file outside the project is not shown"
)
})
}