fn collect_assemblyscript_files(project_path: &Path) -> Result<Vec<PathBuf>> {
let mut files = Vec::new();
for entry in WalkDir::new(project_path)
.into_iter()
.filter_map(std::result::Result::ok)
.filter(|e| e.file_type().is_file())
{
process_assemblyscript_entry(entry.path(), &mut files);
}
Ok(files)
}
fn process_assemblyscript_entry(path: &Path, files: &mut Vec<PathBuf>) {
let ext = match path.extension().and_then(|s| s.to_str()) {
Some(ext) => ext,
None => return,
};
match ext {
"as" => add_assemblyscript_file(path, files),
"ts" => check_and_add_typescript_file(path, files),
_ => {}
}
}
fn add_assemblyscript_file(path: &Path, files: &mut Vec<PathBuf>) {
files.push(path.to_path_buf());
}
fn check_and_add_typescript_file(path: &Path, files: &mut Vec<PathBuf>) {
if is_assemblyscript_typescript(path) {
files.push(path.to_path_buf());
}
}
fn is_assemblyscript_typescript(path: &Path) -> bool {
let content = match std::fs::read_to_string(path) {
Ok(content) => content,
Err(_) => return false,
};
contains_assemblyscript_markers(&content)
}
fn contains_assemblyscript_markers(content: &str) -> bool {
content.contains("@global")
|| content.contains("@inline")
|| content.contains("i32")
|| content.contains("f64")
|| content.contains("memory.")
}
fn collect_wasm_files(
project_path: &Path,
include_binary: bool,
include_text: bool,
) -> Result<Vec<PathBuf>> {
let mut files = Vec::new();
for entry in WalkDir::new(project_path)
.into_iter()
.filter_map(std::result::Result::ok)
.filter(|e| e.file_type().is_file())
{
let path = entry.path();
if let Some(ext) = path.extension().and_then(|s| s.to_str()) {
match ext {
"wasm" if include_binary => files.push(path.to_path_buf()),
"wat" if include_text => files.push(path.to_path_buf()),
_ => {}
}
}
}
Ok(files)
}