backend::ast: rustfmt

This commit is contained in:
Nick Fitzgerald
2018-05-31 22:51:40 -07:00
parent ce8238cdd8
commit c7819930b4

View File

@ -1,4 +1,4 @@
use proc_macro2::{TokenTree, TokenStream, Ident, Span}; use proc_macro2::{Ident, Span, TokenStream, TokenTree};
use quote::ToTokens; use quote::ToTokens;
use shared; use shared;
use syn; use syn;
@ -121,7 +121,12 @@ pub enum TypeLocation {
} }
impl Program { impl Program {
pub fn push_item(&mut self, item: syn::Item, opts: Option<BindgenAttrs>, tokens: &mut TokenStream) { pub fn push_item(
&mut self,
item: syn::Item,
opts: Option<BindgenAttrs>,
tokens: &mut TokenStream,
) {
match item { match item {
syn::Item::Fn(mut f) => { syn::Item::Fn(mut f) => {
let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut f.attrs)); let opts = opts.unwrap_or_else(|| BindgenAttrs::find(&mut f.attrs));
@ -309,7 +314,10 @@ impl Program {
BindgenAttrs::find(attrs) BindgenAttrs::find(attrs)
}; };
let module = item_opts.module().or(opts.module()).map(|s| s.to_string()); let module = item_opts.module().or(opts.module()).map(|s| s.to_string());
let version = item_opts.version().or(opts.version()).map(|s| s.to_string()); let version = item_opts
.version()
.or(opts.version())
.map(|s| s.to_string());
let js_namespace = item_opts.js_namespace().or(opts.js_namespace()).cloned(); let js_namespace = item_opts.js_namespace().or(opts.js_namespace()).cloned();
let mut kind = match item { let mut kind = match item {
syn::ForeignItem::Fn(f) => self.push_foreign_fn(f, item_opts), syn::ForeignItem::Fn(f) => self.push_foreign_fn(f, item_opts),
@ -329,14 +337,7 @@ impl Program {
pub fn push_foreign_fn(&mut self, f: syn::ForeignItemFn, opts: BindgenAttrs) -> ImportKind { pub fn push_foreign_fn(&mut self, f: syn::ForeignItemFn, opts: BindgenAttrs) -> ImportKind {
let js_name = opts.js_name().unwrap_or(&f.ident).clone(); let js_name = opts.js_name().unwrap_or(&f.ident).clone();
let mut wasm = Function::from_decl( let mut wasm = Function::from_decl(&js_name, f.decl, f.attrs, opts, f.vis, false).0;
&js_name,
f.decl,
f.attrs,
opts,
f.vis,
false,
).0;
if wasm.opts.catch() { if wasm.opts.catch() {
// TODO: this assumes a whole bunch: // TODO: this assumes a whole bunch:
// //
@ -565,9 +566,7 @@ impl Export {
pub fn export_name(&self) -> String { pub fn export_name(&self) -> String {
let fn_name = self.function.name.to_string(); let fn_name = self.function.name.to_string();
match &self.class { match &self.class {
Some(class) => { Some(class) => shared::struct_function_export_name(&class.to_string(), &fn_name),
shared::struct_function_export_name(&class.to_string(), &fn_name)
}
None => shared::free_function_export_name(&fn_name), None => shared::free_function_export_name(&fn_name),
} }
} }
@ -605,20 +604,24 @@ impl Import {
match (&self.module, &self.version) { match (&self.module, &self.version) {
(&Some(ref m), None) if m.starts_with("./") => {} (&Some(ref m), None) if m.starts_with("./") => {}
(&Some(ref m), &Some(_)) if m.starts_with("./") => { (&Some(ref m), &Some(_)) if m.starts_with("./") => {
panic!("when a module path starts with `./` that indicates \ panic!(
that a local file is being imported so the `version` \ "when a module path starts with `./` that indicates \
key cannot also be specified"); that a local file is being imported so the `version` \
key cannot also be specified"
);
} }
(&Some(_), &Some(_)) => {} (&Some(_), &Some(_)) => {}
(&Some(_), &None) => { (&Some(_), &None) => panic!(
panic!("when the `module` directive doesn't start with `./` \ "when the `module` directive doesn't start with `./` \
then it's interpreted as an NPM package which requires \ then it's interpreted as an NPM package which requires \
a `version` to be specified as well, try using \ a `version` to be specified as well, try using \
#[wasm_bindgen(module = \"...\", version = \"...\")]") #[wasm_bindgen(module = \"...\", version = \"...\")]"
} ),
(&None, &Some(_)) => { (&None, &Some(_)) => {
panic!("the #[wasm_bindgen(version = \"...\")] attribute can only \ panic!(
be used when `module = \"...\"` is also specified"); "the #[wasm_bindgen(version = \"...\")] attribute can only \
be used when `module = \"...\"` is also specified"
);
} }
(&None, &None) => {} (&None, &None) => {}
} }
@ -711,15 +714,17 @@ impl ImportStatic {
impl ImportType { impl ImportType {
fn shared(&self) -> shared::ImportType { fn shared(&self) -> shared::ImportType {
shared::ImportType { } shared::ImportType {}
} }
} }
impl Struct { impl Struct {
fn from(s: &mut syn::ItemStruct, _opts: BindgenAttrs) -> Struct { fn from(s: &mut syn::ItemStruct, _opts: BindgenAttrs) -> Struct {
if s.generics.params.len() > 0 { if s.generics.params.len() > 0 {
panic!("structs with #[wasm_bindgen] cannot have lifetime or \ panic!(
type parameters currently"); "structs with #[wasm_bindgen] cannot have lifetime or \
type parameters currently"
);
} }
let mut fields = Vec::new(); let mut fields = Vec::new();
if let syn::Fields::Named(names) = &mut s.fields { if let syn::Fields::Named(names) = &mut s.fields {
@ -802,11 +807,9 @@ impl BindgenAttrs {
fn module(&self) -> Option<&str> { fn module(&self) -> Option<&str> {
self.attrs self.attrs
.iter() .iter()
.filter_map(|a| { .filter_map(|a| match a {
match a { BindgenAttr::Module(s) => Some(&s[..]),
BindgenAttr::Module(s) => Some(&s[..]), _ => None,
_ => None,
}
}) })
.next() .next()
} }
@ -814,50 +817,40 @@ impl BindgenAttrs {
fn version(&self) -> Option<&str> { fn version(&self) -> Option<&str> {
self.attrs self.attrs
.iter() .iter()
.filter_map(|a| { .filter_map(|a| match a {
match a { BindgenAttr::Version(s) => Some(&s[..]),
BindgenAttr::Version(s) => Some(&s[..]), _ => None,
_ => None,
}
}) })
.next() .next()
} }
pub fn catch(&self) -> bool { pub fn catch(&self) -> bool {
self.attrs.iter().any(|a| { self.attrs.iter().any(|a| match a {
match a { BindgenAttr::Catch => true,
BindgenAttr::Catch => true, _ => false,
_ => false,
}
}) })
} }
fn constructor(&self) -> bool { fn constructor(&self) -> bool {
self.attrs.iter().any(|a| { self.attrs.iter().any(|a| match a {
match a { BindgenAttr::Constructor => true,
BindgenAttr::Constructor => true, _ => false,
_ => false,
}
}) })
} }
fn method(&self) -> bool { fn method(&self) -> bool {
self.attrs.iter().any(|a| { self.attrs.iter().any(|a| match a {
match a { BindgenAttr::Method => true,
BindgenAttr::Method => true, _ => false,
_ => false,
}
}) })
} }
fn js_namespace(&self) -> Option<&Ident> { fn js_namespace(&self) -> Option<&Ident> {
self.attrs self.attrs
.iter() .iter()
.filter_map(|a| { .filter_map(|a| match a {
match a { BindgenAttr::JsNamespace(s) => Some(s),
BindgenAttr::JsNamespace(s) => Some(s), _ => None,
_ => None,
}
}) })
.next() .next()
} }
@ -865,11 +858,9 @@ impl BindgenAttrs {
pub fn getter(&self) -> Option<Option<&Ident>> { pub fn getter(&self) -> Option<Option<&Ident>> {
self.attrs self.attrs
.iter() .iter()
.filter_map(|a| { .filter_map(|a| match a {
match a { BindgenAttr::Getter(s) => Some(s.as_ref()),
BindgenAttr::Getter(s) => Some(s.as_ref()), _ => None,
_ => None,
}
}) })
.next() .next()
} }
@ -877,41 +868,33 @@ impl BindgenAttrs {
pub fn setter(&self) -> Option<Option<&Ident>> { pub fn setter(&self) -> Option<Option<&Ident>> {
self.attrs self.attrs
.iter() .iter()
.filter_map(|a| { .filter_map(|a| match a {
match a { BindgenAttr::Setter(s) => Some(s.as_ref()),
BindgenAttr::Setter(s) => Some(s.as_ref()), _ => None,
_ => None,
}
}) })
.next() .next()
} }
pub fn structural(&self) -> bool { pub fn structural(&self) -> bool {
self.attrs.iter().any(|a| { self.attrs.iter().any(|a| match *a {
match *a { BindgenAttr::Structural => true,
BindgenAttr::Structural => true, _ => false,
_ => false,
}
}) })
} }
pub fn readonly(&self) -> bool { pub fn readonly(&self) -> bool {
self.attrs.iter().any(|a| { self.attrs.iter().any(|a| match *a {
match *a { BindgenAttr::Readonly => true,
BindgenAttr::Readonly => true, _ => false,
_ => false,
}
}) })
} }
pub fn js_name(&self) -> Option<&Ident> { pub fn js_name(&self) -> Option<&Ident> {
self.attrs self.attrs
.iter() .iter()
.filter_map(|a| { .filter_map(|a| match a {
match a { BindgenAttr::JsName(s) => Some(s),
BindgenAttr::JsName(s) => Some(s), _ => None,
_ => None,
}
}) })
.next() .next()
} }
@ -1046,12 +1029,10 @@ fn term<'a>(cursor: syn::buffer::Cursor<'a>, name: &str) -> syn::synom::PResult<
syn::parse_error() syn::parse_error()
} }
fn term2ident<'a>(cursor: syn::buffer::Cursor<'a>) fn term2ident<'a>(cursor: syn::buffer::Cursor<'a>) -> syn::synom::PResult<'a, Ident> {
-> syn::synom::PResult<'a, Ident>
{
match cursor.ident() { match cursor.ident() {
Some(pair) => Ok(pair), Some(pair) => Ok(pair),
None => syn::parse_error() None => syn::parse_error(),
} }
} }
@ -1060,8 +1041,10 @@ fn assert_no_lifetimes(decl: &mut syn::FnDecl) {
impl<'ast> syn::visit_mut::VisitMut for Walk { impl<'ast> syn::visit_mut::VisitMut for Walk {
fn visit_lifetime_mut(&mut self, _i: &mut syn::Lifetime) { fn visit_lifetime_mut(&mut self, _i: &mut syn::Lifetime) {
panic!("it is currently not sound to use lifetimes in function \ panic!(
signatures"); "it is currently not sound to use lifetimes in function \
signatures"
);
} }
} }