Skip to content

Commit 8a9831c

Browse files
committed
make WebIDL enums non-exhaustive
1 parent 398be48 commit 8a9831c

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

crates/backend/src/codegen.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -580,7 +580,7 @@ impl ToTokens for ast::ImportEnum {
580580
fn to_tokens(&self, tokens: &mut TokenStream) {
581581
let vis = &self.vis;
582582
let name = &self.name;
583-
let expect_string = format!("attempted to convert invalid JSValue into {}", name);
583+
let expect_string = format!("attempted to convert invalid {} into JSValue", name);
584584
let variants = &self.variants;
585585
let variant_strings = &self.variant_values;
586586
let attrs = &self.rust_attrs;
@@ -613,6 +613,8 @@ impl ToTokens for ast::ImportEnum {
613613
#(#attrs)*
614614
#vis enum #name {
615615
#(#variants = #variant_indexes_ref,)*
616+
#[doc(hidden)]
617+
__Nonexhaustive,
616618
}
617619

618620
impl #name {
@@ -647,14 +649,15 @@ impl ToTokens for ast::ImportEnum {
647649
js: Self::Abi,
648650
extra: &mut ::wasm_bindgen::convert::Stack,
649651
) -> Self {
650-
#name::from_js_value(&::wasm_bindgen::JsValue::from_abi(js, extra)).expect(#expect_string)
652+
#name::from_js_value(&::wasm_bindgen::JsValue::from_abi(js, extra)).unwrap_or(#name::__Nonexhaustive)
651653
}
652654
}
653655

654656
impl From<#name> for ::wasm_bindgen::JsValue {
655657
fn from(obj: #name) -> ::wasm_bindgen::JsValue {
656658
match obj {
657-
#(#variant_paths_ref => ::wasm_bindgen::JsValue::from_str(#variant_strings)),*
659+
#(#variant_paths_ref => ::wasm_bindgen::JsValue::from_str(#variant_strings),)*
660+
#name::__Nonexhaustive => panic!(#expect_string),
658661
}
659662
}
660663
}

0 commit comments

Comments
 (0)