Implement a polyfill attribute for imports

Allow using imported APIs under alternative names, such as prefixed
names, for web APIs when the exact API differs across browsers.
This commit is contained in:
Alex Crichton
2018-09-28 13:17:37 -07:00
parent 11bcaf42d5
commit 3c14f7a6eb
11 changed files with 172 additions and 2 deletions

View File

@ -185,6 +185,13 @@ impl BindgenAttrs {
})
}
fn polyfills(&self) -> impl Iterator<Item = &Ident> {
self.attrs.iter().filter_map(|a| match a {
BindgenAttr::Polyfill(s) => Some(s),
_ => None,
})
}
/// Whether the variadic attributes is present
fn variadic(&self) -> bool {
self.attrs.iter().any(|a| match *a {
@ -226,6 +233,7 @@ pub enum BindgenAttr {
JsName(String, Span),
JsClass(String),
Extends(Ident),
Polyfill(Ident),
Variadic,
}
@ -286,6 +294,10 @@ impl Parse for BindgenAttr {
input.parse::<Token![=]>()?;
return Ok(BindgenAttr::Extends(input.parse::<AnyIdent>()?.0));
}
if attr == "polyfill" {
input.parse::<Token![=]>()?;
return Ok(BindgenAttr::Polyfill(input.parse::<AnyIdent>()?.0));
}
if attr == "module" {
input.parse::<Token![=]>()?;
return Ok(BindgenAttr::Module(input.parse::<syn::LitStr>()?.value()));
@ -556,6 +568,7 @@ impl ConvertToAst<BindgenAttrs> for syn::ForeignItemType {
rust_name: self.ident,
js_name,
extends: attrs.extends().cloned().collect(),
polyfills: attrs.polyfills().cloned().collect(),
}))
}
}