Generate strerror from witx; tweak Display for WasiError (#832)
This commit introduces two small changes: * it adds `gen_errno_strerror` to `wig` crate which generates a `strerror` function for `__wasi_errno_t` directly from `*.witx`, similarly to how it's done in the `wasi` crate * it tweaks `WasiError` type to include the error message generated with `strerror` when displaying the error
This commit is contained in:
committed by
Alex Crichton
parent
5b8be5f262
commit
5f1c0eb86b
@@ -148,6 +148,38 @@ fn gen_datatype(output: &mut TokenStream, mode: Mode, namedtype: &witx::NamedTyp
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
if namedtype.name.as_str() == "errno" {
|
||||
// Generate strerror for errno type
|
||||
gen_errno_strerror(output, namedtype);
|
||||
}
|
||||
}
|
||||
|
||||
fn gen_errno_strerror(output: &mut TokenStream, namedtype: &witx::NamedType) {
|
||||
let inner = match &namedtype.dt {
|
||||
witx::TypeRef::Value(v) => match &**v {
|
||||
witx::Type::Enum(e) => e,
|
||||
x => panic!("expected Enum('errno'), instead received {:?}", x),
|
||||
},
|
||||
x => panic!("expected Enum('errno'), instead received {:?}", x),
|
||||
};
|
||||
let mut inner_group = TokenStream::new();
|
||||
for variant in &inner.variants {
|
||||
let value_name = format_ident!(
|
||||
"__WASI_ERRNO_{}",
|
||||
variant.name.as_str().to_shouty_snake_case()
|
||||
);
|
||||
let docs = variant.docs.trim();
|
||||
inner_group.extend(quote!(#value_name => #docs,));
|
||||
}
|
||||
output.extend(
|
||||
quote!(pub fn strerror(errno: __wasi_errno_t) -> &'static str {
|
||||
match errno {
|
||||
#inner_group
|
||||
other => panic!("Undefined errno value {:?}", other),
|
||||
}
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
fn int_repr_tokens(int_repr: witx::IntRepr) -> TokenStream {
|
||||
|
||||
Reference in New Issue
Block a user