Skip to content

Commit 0e6508c

Browse files
refactor(splicer): add explicit error for invalid WIT source
Signed-off-by: Victor Adossi <vadossi@cosmonic.com>
1 parent 2973656 commit 0e6508c

File tree

2 files changed

+20
-12
lines changed

2 files changed

+20
-12
lines changed

crates/spidermonkey-embedding-splicer/src/lib.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,17 @@ fn map_core_fn(cfn: &bindgen::CoreFn) -> CoreFn {
7070
}
7171
}
7272

73-
fn parse_wit(path: &Path) -> Result<(Resolve, PackageId)> {
73+
fn parse_wit(path: impl AsRef<Path>) -> Result<(Resolve, PackageId)> {
7474
let mut resolve = Resolve::default();
75+
let path = path.as_ref();
7576
let id = if path.is_dir() {
76-
resolve.push_dir(&path)?.0
77+
resolve
78+
.push_dir(&path)
79+
.with_context(|| format!("resolving WIT in {}", path.display()))?
80+
.0
7781
} else {
7882
let contents =
79-
std::fs::read(&path).with_context(|| format!("failed to read file {path:?}"))?;
83+
std::fs::read(&path).with_context(|| format!("reading file {}", path.display()))?;
8084
let text = match std::str::from_utf8(&contents) {
8185
Ok(s) => s,
8286
Err(_) => bail!("input file is not valid utf-8"),

crates/spidermonkey-embedding-splicer/src/splice.rs

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,15 +33,19 @@ pub fn splice_bindings(
3333
wit_source: Option<String>,
3434
debug: bool,
3535
) -> Result<SpliceResult, String> {
36-
let (mut resolve, id) = if let Some(wit_source) = wit_source {
37-
let mut resolve = Resolve::default();
38-
let path = PathBuf::from("component.wit");
39-
let id = resolve
40-
.push_str(&path, &wit_source)
41-
.map_err(|e| e.to_string())?;
42-
(resolve, id)
43-
} else {
44-
parse_wit(&PathBuf::from(wit_path.unwrap())).map_err(|e| format!("{:?}", e))?
36+
let (mut resolve, id) = match (wit_source, wit_path) {
37+
(Some(wit_source), _) => {
38+
let mut resolve = Resolve::default();
39+
let path = PathBuf::from("component.wit");
40+
let id = resolve
41+
.push_str(&path, &wit_source)
42+
.map_err(|e| e.to_string())?;
43+
(resolve, id)
44+
}
45+
(_, Some(wit_path)) => parse_wit(&wit_path).map_err(|e| format!("{:?}", e))?,
46+
(None, None) => {
47+
return Err("neither wit source nor path have been specified".into());
48+
}
4549
};
4650

4751
let world = resolve

0 commit comments

Comments
 (0)