use std::{ffi::OsString, num::NonZeroUsize};
use anyhow::{anyhow, ensure};
use indexmap::IndexSet;
use super::StdlibConfig;
use crate::localization::{self, keys};
impl StdlibConfig {
pub fn with_which_cache_capacity(mut self, capacity: usize) -> anyhow::Result<Self> {
let non_zero_capacity = NonZeroUsize::new(capacity).ok_or_else(|| {
anyhow!(
"{}",
localization::message(keys::STDLIB_WHICH_CACHE_CAPACITY_POSITIVE)
)
})?;
self.which_cache_capacity = non_zero_capacity;
Ok(self)
}
pub fn with_workspace_skip_dirs<I, S>(mut self, dirs: I) -> anyhow::Result<Self>
where
I: IntoIterator<Item = S>,
S: AsRef<str>,
{
let mut validated = IndexSet::new();
for dir in dirs {
let candidate = dir.as_ref().trim();
ensure!(
!candidate.is_empty(),
"{}",
localization::message(keys::STDLIB_SKIP_DIR_EMPTY)
);
ensure!(
!matches!(candidate, "." | ".."),
"{}",
localization::message(keys::STDLIB_SKIP_DIR_NAVIGATION)
);
ensure!(
!candidate.contains(['/', '\\']),
"{}",
localization::message(keys::STDLIB_SKIP_DIR_SEPARATOR)
);
validated.insert(candidate.to_owned());
}
self.workspace_skip_dirs = validated.into_iter().collect();
Ok(self)
}
#[must_use]
pub fn with_path_override(mut self, path: impl Into<OsString>) -> Self {
self.path_override = Some(path.into());
self
}
pub(crate) const fn path_override(&self) -> Option<&OsString> {
self.path_override.as_ref()
}
#[must_use]
pub fn with_pathext_override(mut self, pathext: impl Into<OsString>) -> Self {
self.pathext_override = Some(pathext.into());
self
}
pub(crate) const fn pathext_override(&self) -> Option<&OsString> {
self.pathext_override.as_ref()
}
#[must_use]
pub fn workspace_skip_dirs(&self) -> &[String] {
&self.workspace_skip_dirs
}
pub(crate) const fn which_cache_capacity(&self) -> NonZeroUsize {
self.which_cache_capacity
}
}