use std::path::{Path, PathBuf};
use crate::config::{Config, is_glob};
use crate::error::WasmCheckError;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedFile {
key: String,
path: PathBuf,
}
impl ResolvedFile {
#[must_use]
pub fn key(&self) -> &str {
&self.key
}
#[must_use]
pub fn path(&self) -> &Path {
&self.path
}
}
pub fn find_wasm_files(dir: impl AsRef<Path>) -> Result<Vec<PathBuf>, WasmCheckError> {
let mut entries: Vec<PathBuf> = std::fs::read_dir(dir.as_ref())?
.filter_map(Result::ok)
.map(|entry| entry.path())
.filter(|path| path.extension().is_some_and(|ext| ext == "wasm"))
.collect();
entries.sort();
Ok(entries)
}
pub fn resolve_files(
cli_file: Option<&str>,
config: Option<&Config>,
config_dir: &Path,
) -> Result<Vec<ResolvedFile>, WasmCheckError> {
let base = normalized_dir(config_dir);
if let Some(file) = cli_file {
let path = clean(Path::new(file));
if !path.is_file() {
return Err(WasmCheckError::FileNotFound(path));
}
return Ok(vec![ResolvedFile {
key: key_for(&path, &base),
path,
}]);
}
if let Some(config) = config.filter(|c| !c.files().is_empty()) {
let mut resolved = Vec::new();
for entry in config.files() {
if is_glob(entry) {
let pattern = glob_pattern(&base, entry);
let matches = glob::glob(&pattern)
.map_err(|e| WasmCheckError::Io(std::io::Error::other(e.to_string())))?;
for path in matches.filter_map(Result::ok).filter(|p| p.is_file()) {
resolved.push(ResolvedFile {
key: entry.clone(),
path: clean(&path),
});
}
} else {
let path = clean(&base.join(entry));
if !path.is_file() {
return Err(WasmCheckError::FileNotFound(path));
}
resolved.push(ResolvedFile {
key: entry.clone(),
path,
});
}
}
resolved.sort_by(|a, b| a.path.cmp(&b.path));
resolved.dedup();
if resolved.is_empty() {
return Err(WasmCheckError::NoWasmFound {
dir: display_dir(&base),
});
}
return Ok(resolved);
}
let dir = if base.as_os_str().is_empty() {
PathBuf::from(".")
} else {
base.clone()
};
let found = find_wasm_files(&dir)?;
match found.as_slice() {
[] => Err(WasmCheckError::NoWasmFound {
dir: display_dir(&base),
}),
[only] => Ok(vec![ResolvedFile {
key: file_name(only),
path: clean(only),
}]),
many => Err(WasmCheckError::MultipleWasmFound {
found: many.iter().map(|p| p.display().to_string()).collect(),
}),
}
}
fn normalized_dir(dir: &Path) -> PathBuf {
if dir.as_os_str().is_empty() || dir == Path::new(".") {
PathBuf::new()
} else {
dir.to_path_buf()
}
}
fn display_dir(base: &Path) -> PathBuf {
if base.as_os_str().is_empty() {
PathBuf::from(".")
} else {
base.to_path_buf()
}
}
fn glob_pattern(base: &Path, entry: &str) -> String {
if base.as_os_str().is_empty() {
entry.to_string()
} else {
format!("{}/{}", base.to_string_lossy().replace('\\', "/"), entry)
}
}
fn key_for(path: &Path, base: &Path) -> String {
if base.as_os_str().is_empty() {
return path.display().to_string();
}
match path.strip_prefix(base) {
Ok(relative) if !relative.as_os_str().is_empty() => relative.display().to_string(),
_ => path.display().to_string(),
}
}
fn file_name(path: &Path) -> String {
path.file_name()
.map(|name| name.to_string_lossy().to_string())
.unwrap_or_else(|| path.display().to_string())
}
fn clean(path: &Path) -> PathBuf {
path.strip_prefix(".")
.map(Path::to_path_buf)
.unwrap_or_else(|_| path.to_path_buf())
}