component bindgen: accept strs as well as identifiers for wit identifiers (#5600)

This is required because not all wit identifiers are Rust identifiers,
so we can smuggle the invalid ones inside quotes.
This commit is contained in:
Pat Hickey
2023-01-20 07:53:04 -08:00
committed by GitHub
parent f3418b760e
commit 92de180d7d
2 changed files with 19 additions and 3 deletions

View File

@@ -226,9 +226,23 @@ impl Parse for Opt {
}
fn trappable_error_field_parse(input: ParseStream<'_>) -> Result<(String, String, String)> {
let interface = input.parse::<Ident>()?.to_string();
// Accept a Rust identifier or a string literal. This is required
// because not all wit identifiers are Rust identifiers, so we can
// smuggle the invalid ones inside quotes.
fn ident_or_str(input: ParseStream<'_>) -> Result<String> {
let l = input.lookahead1();
if l.peek(syn::LitStr) {
Ok(input.parse::<syn::LitStr>()?.value())
} else if l.peek(syn::Ident) {
Ok(input.parse::<syn::Ident>()?.to_string())
} else {
Err(l.error())
}
}
let interface = ident_or_str(input)?;
input.parse::<Token![::]>()?;
let type_ = input.parse::<Ident>()?.to_string();
let type_ = ident_or_str(input)?;
input.parse::<Token![:]>()?;
let rust_type = input.parse::<Ident>()?.to_string();
Ok((interface, type_, rust_type))