Skip to content

Commit 9fdf685

Browse files
committed
fix(bindings): report wasm store error in rust bindings set_language
1 parent 733d751 commit 9fdf685

File tree

1 file changed

+25
-15
lines changed

1 file changed

+25
-15
lines changed

lib/binding_rust/lib.rs

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -364,10 +364,13 @@ pub struct QueryCapture<'tree> {
364364
}
365365

366366
/// An error that occurred when trying to assign an incompatible [`Language`] to
367-
/// a [`Parser`].
367+
/// a [`Parser`]. If the `wasm` feature is enabled, this can also indicate a failure
368+
/// to load the wasm store.
368369
#[derive(Debug, PartialEq, Eq)]
369-
pub struct LanguageError {
370-
version: usize,
370+
pub enum LanguageError {
371+
Version(usize),
372+
#[cfg(feature = "wasm")]
373+
Wasm,
371374
}
372375

373376
/// An error that occurred in [`Parser::set_included_ranges`].
@@ -666,12 +669,15 @@ impl Parser {
666669
pub fn set_language(&mut self, language: &Language) -> Result<(), LanguageError> {
667670
let version = language.abi_version();
668671
if (MIN_COMPATIBLE_LANGUAGE_VERSION..=LANGUAGE_VERSION).contains(&version) {
669-
unsafe {
670-
ffi::ts_parser_set_language(self.0.as_ptr(), language.0);
672+
#[allow(unused_variables)]
673+
let success = unsafe { ffi::ts_parser_set_language(self.0.as_ptr(), language.0) };
674+
#[cfg(feature = "wasm")]
675+
if !success {
676+
return Err(LanguageError::Wasm);
671677
}
672678
Ok(())
673679
} else {
674-
Err(LanguageError { version })
680+
Err(LanguageError::Version(version))
675681
}
676682
}
677683

@@ -2415,10 +2421,7 @@ impl Query {
24152421
row: 0,
24162422
column: 0,
24172423
offset: 0,
2418-
message: LanguageError {
2419-
version: language.abi_version(),
2420-
}
2421-
.to_string(),
2424+
message: LanguageError::Version(language.abi_version()).to_string(),
24222425
kind: QueryErrorKind::Language,
24232426
});
24242427
}
@@ -3726,11 +3729,18 @@ impl fmt::Display for IncludedRangesError {
37263729

37273730
impl fmt::Display for LanguageError {
37283731
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
3729-
write!(
3730-
f,
3731-
"Incompatible language version {}. Expected minimum {}, maximum {}",
3732-
self.version, MIN_COMPATIBLE_LANGUAGE_VERSION, LANGUAGE_VERSION,
3733-
)
3732+
match self {
3733+
Self::Version(version) => {
3734+
write!(
3735+
f,
3736+
"Incompatible language version {version}. Expected minimum {MIN_COMPATIBLE_LANGUAGE_VERSION}, maximum {LANGUAGE_VERSION}",
3737+
)
3738+
}
3739+
#[cfg(feature = "wasm")]
3740+
Self::Wasm => {
3741+
write!(f, "Failed to load the wasm store.")
3742+
}
3743+
}
37343744
}
37353745
}
37363746

0 commit comments

Comments
 (0)