Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion crates/wac-resolver/src/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ impl FileSystemPackageResolver {

// If the path is not a directory, use a `.wasm` or `.wat` extension
if !path.is_dir() {
path.set_extension("wasm");
append_extension(&mut path, "wasm");

#[cfg(feature = "wat")]
{
Expand Down Expand Up @@ -176,3 +176,30 @@ impl Default for FileSystemPackageResolver {
Self::new("deps", Default::default(), true)
}
}

/// Similar to Path::set_extension except it always appends.
/// For example "0.0.1" -> "0.0.1.wasm" (instead of to "0.0.wasm").
fn append_extension(path: &mut PathBuf, extension: &str) {
let os_str = path.as_mut_os_string();
os_str.push(".");
os_str.push(extension)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn append_extension_if_no_existing_extension() {
let mut path = PathBuf::from("a/b/c");
append_extension(&mut path, "txt");
assert_eq!("a/b/c.txt", path.display().to_string());
}

#[test]
fn append_extension_if_existing_extension() {
let mut path = PathBuf::from("a/b/0.0.1");
append_extension(&mut path, "wasm");
assert_eq!("a/b/0.0.1.wasm", path.display().to_string());
}
}