Skip to content

Commit 3b214e3

Browse files
committed
fix: small refactors
Signed-off-by: Andrew Steurer <94206073+asteurer@users.noreply.github.com>
1 parent 1ac2513 commit 3b214e3

3 files changed

Lines changed: 18 additions & 28 deletions

File tree

src/bindings.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::common::parse_wit;
1+
use crate::common::{make_path_absolute, parse_wit};
22
use anyhow::Result;
33
use std::path::{Path, PathBuf};
44

@@ -29,13 +29,7 @@ pub fn generate_bindings(
2929
.generate(&resolve, world, &mut files)?;
3030

3131
let output_path = match output {
32-
Some(p) => {
33-
if p.is_relative() {
34-
std::env::current_dir()?.join(p)
35-
} else {
36-
p.to_path_buf()
37-
}
38-
}
32+
Some(p) => make_path_absolute(&p.to_path_buf())?,
3933
None => PathBuf::from("."),
4034
};
4135

src/common.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use anyhow::Result;
2-
use std::path::Path;
2+
use std::path::{Path, PathBuf};
33
use wit_parser::{PackageId, Resolve, WorldId};
44

55
pub fn parse_wit(
@@ -39,3 +39,12 @@ pub fn parse_wit(
3939
let world = resolve.select_world(&main_packages, world)?;
4040
Ok((resolve, world))
4141
}
42+
43+
// Converts a relative path to an absolute path.
44+
pub fn make_path_absolute(p: &PathBuf) -> Result<PathBuf> {
45+
if p.is_relative() {
46+
Ok(std::env::current_dir()?.join(p))
47+
} else {
48+
Ok(p.to_owned())
49+
}
50+
}

src/componentize.rs

Lines changed: 6 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use crate::common::parse_wit;
1+
use crate::common::{make_path_absolute, parse_wit};
22
use anyhow::{Context, Result, anyhow};
33
use std::{path::PathBuf, process::Command};
44

@@ -91,26 +91,14 @@ pub fn build_wasm_core_module(
9191
go_path: Option<PathBuf>,
9292
) -> Result<PathBuf> {
9393
let go = match &go_path {
94-
Some(p) => {
95-
if p.is_relative() {
96-
std::env::current_dir()?.join(p)
97-
} else {
98-
p.to_path_buf()
99-
}
100-
}
94+
Some(p) => make_path_absolute(p)?,
10195
None => PathBuf::from("go"),
10296
};
10397

10498
check_go_version(&go)?;
10599

106100
let out_path_buf = match &out {
107-
Some(p) => {
108-
if p.is_relative() {
109-
std::env::current_dir()?.join(p)
110-
} else {
111-
p.to_path_buf()
112-
}
113-
}
101+
Some(p) => make_path_absolute(p)?,
114102
None => std::env::current_dir()?.join("main.wasm"),
115103
};
116104

@@ -121,9 +109,8 @@ pub fn build_wasm_core_module(
121109
}
122110

123111
let out_path = out_path_buf
124-
.into_os_string()
125-
.into_string()
126-
.map_err(|_| anyhow!("Output path is not valid unicode"))?;
112+
.to_str()
113+
.ok_or_else(|| anyhow!("Output path is not valid unicode"))?;
127114

128115
let module_path = match &go_module {
129116
Some(p) => {
@@ -144,7 +131,7 @@ pub fn build_wasm_core_module(
144131
"-buildmode=c-shared",
145132
"-ldflags=-checklinkname=0",
146133
"-o",
147-
&out_path,
134+
out_path,
148135
])
149136
.env("GOOS", "wasip1")
150137
.env("GOARCH", "wasm")

0 commit comments

Comments
 (0)