diff --git a/azure-pipelines.yml b/azure-pipelines.yml index feac9bdf..4067d89b 100644 --- a/azure-pipelines.yml +++ b/azure-pipelines.yml @@ -5,6 +5,12 @@ trigger: - refs/tags/* jobs: + - job: test_formatting + displayName: "Run cargo fmt" + steps: + - template: ci/azure-install-rust.yml + - script: cargo fmt --all -- --check + - job: test_wasm_bindgen displayName: "Run wasm-bindgen crate tests (unix)" steps: diff --git a/crates/backend/src/codegen.rs b/crates/backend/src/codegen.rs index 315c8912..fff2275e 100644 --- a/crates/backend/src/codegen.rs +++ b/crates/backend/src/codegen.rs @@ -42,7 +42,8 @@ impl TryToTokens for ast::Program { DescribeImport { kind: &i.kind, unstable_api: i.unstable_api, - }.to_tokens(tokens); + } + .to_tokens(tokens); // If there is a js namespace, check that name isn't a type. If it is, // this import might be a method on that type. diff --git a/crates/backend/src/util.rs b/crates/backend/src/util.rs index 766aac06..756bc14a 100644 --- a/crates/backend/src/util.rs +++ b/crates/backend/src/util.rs @@ -157,7 +157,6 @@ impl fmt::Display for ShortHash { } } - /// Create syn attribute for `#[cfg(web_sys_unstable_apis)]` and a doc comment. pub fn unstable_api_attrs() -> proc_macro2::TokenStream { quote::quote!( diff --git a/crates/cli-support/src/descriptors.rs b/crates/cli-support/src/descriptors.rs index 9fbe1915..f4fec11a 100644 --- a/crates/cli-support/src/descriptors.rs +++ b/crates/cli-support/src/descriptors.rs @@ -136,10 +136,11 @@ impl WasmBindgenDescriptorsSection { let (id, import_id) = module.add_import_func("__wbindgen_placeholder__", &import_name, ty); module.funcs.get_mut(id).name = Some(import_name); - self.closure_imports.insert(import_id, descriptor.clone().unwrap_closure()); + self.closure_imports + .insert(import_id, descriptor.clone().unwrap_closure()); self.cached_closures.insert(descriptor, id); id - }, + } }; let local = match &mut module.funcs.get_mut(func).kind { diff --git a/crates/cli-support/src/js/mod.rs b/crates/cli-support/src/js/mod.rs index a3a91e9a..bed08f8a 100644 --- a/crates/cli-support/src/js/mod.rs +++ b/crates/cli-support/src/js/mod.rs @@ -216,10 +216,7 @@ impl<'a> Context<'a> { js.push_str("const __exports = {};\n"); js.push_str("let wasm;\n"); init = self.gen_init(needs_manual_start, None)?; - footer.push_str(&format!( - "{} = Object.assign(init, __exports);\n", - global - )); + footer.push_str(&format!("{} = Object.assign(init, __exports);\n", global)); } // With normal CommonJS node we need to defer requiring the wasm @@ -319,7 +316,7 @@ impl<'a> Context<'a> { let mut imports = String::new(); if self.config.omit_imports { - return Ok(imports) + return Ok(imports); } match &self.config.mode { @@ -759,7 +756,10 @@ impl<'a> Context<'a> { return; } assert!(!self.config.anyref); - self.global(&format!("const heap = new Array({}).fill(undefined);", INITIAL_HEAP_OFFSET)); + self.global(&format!( + "const heap = new Array({}).fill(undefined);", + INITIAL_HEAP_OFFSET + )); self.global(&format!("heap.push({});", INITIAL_HEAP_VALUES.join(", "))); } @@ -2440,7 +2440,6 @@ impl<'a> Context<'a> { dtor = dtor, call = call, )) - } else { self.expose_make_closure()?; diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index 4727a665..678e16a9 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -187,7 +187,10 @@ impl Default for BindgenAttrs { // sanity check that we call `check_used` an appropriate number of // times. ATTRS.with(|state| state.parsed.set(state.parsed.get() + 1)); - BindgenAttrs { attrs: Vec::new(), unstable_api_attr: None, } + BindgenAttrs { + attrs: Vec::new(), + unstable_api_attr: None, + } } } diff --git a/crates/typescript-tests/src/simple_struct.rs b/crates/typescript-tests/src/simple_struct.rs index a1a44d2c..c31b61ba 100644 --- a/crates/typescript-tests/src/simple_struct.rs +++ b/crates/typescript-tests/src/simple_struct.rs @@ -14,7 +14,9 @@ impl A { pub fn foo(&self) {} - pub fn ret_bool(&self) -> bool { true } + pub fn ret_bool(&self) -> bool { + true + } pub fn take_bool(&self, _: bool) {} pub fn take_many(&self, _: bool, _: f64, _: u32) {} } diff --git a/crates/web-sys/build.rs b/crates/web-sys/build.rs index 6a40df49..eba79a90 100644 --- a/crates/web-sys/build.rs +++ b/crates/web-sys/build.rs @@ -72,26 +72,27 @@ fn main() -> Result<()> { let source = read_source_from_path("webidls/enabled")?; let unstable_source = read_source_from_path("webidls/unstable")?; - let bindings = match wasm_bindgen_webidl::compile(&source.contents, &unstable_source.contents, allowed) { - Ok(bindings) => bindings, - Err(e) => { - if let Some(err) = e.downcast_ref::() { - if let Some(pos) = source.resolve_offset(err.0) { - let ctx = format!( - "compiling WebIDL into wasm-bindgen bindings in file \ + let bindings = + match wasm_bindgen_webidl::compile(&source.contents, &unstable_source.contents, allowed) { + Ok(bindings) => bindings, + Err(e) => { + if let Some(err) = e.downcast_ref::() { + if let Some(pos) = source.resolve_offset(err.0) { + let ctx = format!( + "compiling WebIDL into wasm-bindgen bindings in file \ \"{}\", line {} column {}", - pos.filename, - pos.line + 1, - pos.col + 1 - ); - return Err(e.context(ctx)); - } else { - return Err(e.context("compiling WebIDL into wasm-bindgen bindings")); + pos.filename, + pos.line + 1, + pos.col + 1 + ); + return Err(e.context(ctx)); + } else { + return Err(e.context("compiling WebIDL into wasm-bindgen bindings")); + } } + return Err(e.context("compiling WebIDL into wasm-bindgen bindings")); } - return Err(e.context("compiling WebIDL into wasm-bindgen bindings")); - } - }; + }; let out_dir = env::var("OUT_DIR").context("reading OUT_DIR environment variable")?; let out_file_path = path::Path::new(&out_dir).join("bindings.rs"); diff --git a/crates/web-sys/tests/wasm/main.rs b/crates/web-sys/tests/wasm/main.rs index 92f5fcb8..a12f8181 100644 --- a/crates/web-sys/tests/wasm/main.rs +++ b/crates/web-sys/tests/wasm/main.rs @@ -5,10 +5,10 @@ use wasm_bindgen_test::*; wasm_bindgen_test_configure!(run_in_browser); pub mod anchor_element; +pub mod blob; pub mod body_element; pub mod br_element; pub mod button_element; -pub mod blob; pub mod console; pub mod div_element; pub mod element; diff --git a/crates/web-sys/tests/wasm/whitelisted_immutable_slices.rs b/crates/web-sys/tests/wasm/whitelisted_immutable_slices.rs index 223f2750..14bda73c 100644 --- a/crates/web-sys/tests/wasm/whitelisted_immutable_slices.rs +++ b/crates/web-sys/tests/wasm/whitelisted_immutable_slices.rs @@ -10,8 +10,8 @@ //! //! @see https://github.com/rustwasm/wasm-bindgen/issues/1005 +use wasm_bindgen::{JsCast, JsValue}; use web_sys::{WebGl2RenderingContext, WebGlRenderingContext, WebSocket}; -use wasm_bindgen::{JsValue, JsCast}; // Ensure that our whitelisted WebGlRenderingContext methods compile with immutable slices. fn test_webgl_rendering_context_immutable_slices() { @@ -60,9 +60,9 @@ fn test_webgl_rendering_context_immutable_slices() { fn test_webgl2_rendering_context_immutable_slices() { let gl = JsValue::null().unchecked_into::(); - gl.tex_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1])); - gl.tex_sub_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1])); - gl.compressed_tex_image_3d_with_u8_array(0, 0, 0, 0, 0, 0, 0, &[1]); + gl.tex_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1])); + gl.tex_sub_image_3d_with_opt_u8_array(0, 0, 0, 0, 0, 0, 0, 0, 0, 0, Some(&[1])); + gl.compressed_tex_image_3d_with_u8_array(0, 0, 0, 0, 0, 0, 0, &[1]); } // Ensure that our whitelisted WebSocket methods compile with immutable slices. diff --git a/crates/webidl-tests/build.rs b/crates/webidl-tests/build.rs index 8804e803..f43fb639 100644 --- a/crates/webidl-tests/build.rs +++ b/crates/webidl-tests/build.rs @@ -26,7 +26,8 @@ fn main() { } else { (idl, String::new()) }; - let mut generated_rust = wasm_bindgen_webidl::compile(&stable_source, &experimental_source, None).unwrap(); + let mut generated_rust = + wasm_bindgen_webidl::compile(&stable_source, &experimental_source, None).unwrap(); generated_rust.insert_str( 0, diff --git a/crates/webidl/src/first_pass.rs b/crates/webidl/src/first_pass.rs index 05129298..c2473943 100644 --- a/crates/webidl/src/first_pass.rs +++ b/crates/webidl/src/first_pass.rs @@ -20,7 +20,10 @@ use weedle::CallbackInterfaceDefinition; use weedle::{DictionaryDefinition, PartialDictionaryDefinition}; use super::Result; -use crate::{util::{self, camel_case_ident}, ApiStability}; +use crate::{ + util::{self, camel_case_ident}, + ApiStability, +}; /// Collection of constructs that may use partial. #[derive(Default)] @@ -139,7 +142,11 @@ pub(crate) trait FirstPass<'src, Ctx> { } impl<'src> FirstPass<'src, ApiStability> for [weedle::Definition<'src>] { - fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> { + fn first_pass( + &'src self, + record: &mut FirstPassRecord<'src>, + stability: ApiStability, + ) -> Result<()> { for def in self { def.first_pass(record, stability)?; } @@ -149,7 +156,11 @@ impl<'src> FirstPass<'src, ApiStability> for [weedle::Definition<'src>] { } impl<'src> FirstPass<'src, ApiStability> for weedle::Definition<'src> { - fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> { + fn first_pass( + &'src self, + record: &mut FirstPassRecord<'src>, + stability: ApiStability, + ) -> Result<()> { use weedle::Definition::*; match self { @@ -172,15 +183,16 @@ impl<'src> FirstPass<'src, ApiStability> for weedle::Definition<'src> { } impl<'src> FirstPass<'src, ApiStability> for weedle::DictionaryDefinition<'src> { - fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> { + fn first_pass( + &'src self, + record: &mut FirstPassRecord<'src>, + stability: ApiStability, + ) -> Result<()> { if util::is_chrome_only(&self.attributes) { return Ok(()); } - let dictionary_data = record - .dictionaries - .entry(self.identifier.0) - .or_default(); + let dictionary_data = record.dictionaries.entry(self.identifier.0).or_default(); dictionary_data.definition = Some(self); dictionary_data.stability = stability; @@ -207,7 +219,11 @@ impl<'src> FirstPass<'src, ()> for weedle::PartialDictionaryDefinition<'src> { } impl<'src> FirstPass<'src, ApiStability> for weedle::EnumDefinition<'src> { - fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> { + fn first_pass( + &'src self, + record: &mut FirstPassRecord<'src>, + stability: ApiStability, + ) -> Result<()> { if util::is_chrome_only(&self.attributes) { return Ok(()); } @@ -325,7 +341,11 @@ fn first_pass_operation<'src>( } impl<'src> FirstPass<'src, ApiStability> for weedle::InterfaceDefinition<'src> { - fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> { + fn first_pass( + &'src self, + record: &mut FirstPassRecord<'src>, + stability: ApiStability, + ) -> Result<()> { if util::is_chrome_only(&self.attributes) { return Ok(()); } @@ -410,7 +430,11 @@ fn process_interface_attribute<'src>( } impl<'src> FirstPass<'src, ApiStability> for weedle::PartialInterfaceDefinition<'src> { - fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> { + fn first_pass( + &'src self, + record: &mut FirstPassRecord<'src>, + stability: ApiStability, + ) -> Result<()> { if util::is_chrome_only(&self.attributes) { return Ok(()); } @@ -425,7 +449,7 @@ impl<'src> FirstPass<'src, ApiStability> for weedle::PartialInterfaceDefinition< for member in &self.members.body { member.first_pass(record, (self.identifier.0, stability))?; } - + Ok(()) } } @@ -513,7 +537,9 @@ impl<'src> FirstPass<'src, &'src str> for weedle::interface::OperationInterfaceM } } -impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::interface::AttributeInterfaceMember<'src> { +impl<'src> FirstPass<'src, (&'src str, ApiStability)> + for weedle::interface::AttributeInterfaceMember<'src> +{ fn first_pass( &'src self, record: &mut FirstPassRecord<'src>, @@ -530,14 +556,18 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::interface::Att .attributes .push(AttributeInterfaceData { definition: self, - stability: ctx.1 + stability: ctx.1, }); Ok(()) } } impl<'src> FirstPass<'src, ApiStability> for weedle::InterfaceMixinDefinition<'src> { - fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> { + fn first_pass( + &'src self, + record: &mut FirstPassRecord<'src>, + stability: ApiStability, + ) -> Result<()> { if util::is_chrome_only(&self.attributes) { return Ok(()); } @@ -558,7 +588,11 @@ impl<'src> FirstPass<'src, ApiStability> for weedle::InterfaceMixinDefinition<'s } impl<'src> FirstPass<'src, ApiStability> for weedle::PartialInterfaceMixinDefinition<'src> { - fn first_pass(&'src self, record: &mut FirstPassRecord<'src>, stability: ApiStability) -> Result<()> { + fn first_pass( + &'src self, + record: &mut FirstPassRecord<'src>, + stability: ApiStability, + ) -> Result<()> { if util::is_chrome_only(&self.attributes) { return Ok(()); } @@ -604,7 +638,9 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::MixinMe } } -impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::OperationMixinMember<'src> { +impl<'src> FirstPass<'src, (&'src str, ApiStability)> + for weedle::mixin::OperationMixinMember<'src> +{ fn first_pass( &'src self, record: &mut FirstPassRecord<'src>, @@ -629,7 +665,9 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::Operati } } -impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::AttributeMixinMember<'src> { +impl<'src> FirstPass<'src, (&'src str, ApiStability)> + for weedle::mixin::AttributeMixinMember<'src> +{ fn first_pass( &'src self, record: &mut FirstPassRecord<'src>, @@ -645,7 +683,7 @@ impl<'src> FirstPass<'src, (&'src str, ApiStability)> for weedle::mixin::Attribu .attributes .push(AttributeMixinData { definition: self, - stability: ctx.1 + stability: ctx.1, }); Ok(()) } diff --git a/crates/webidl/src/lib.rs b/crates/webidl/src/lib.rs index d145dcc5..2806c37f 100644 --- a/crates/webidl/src/lib.rs +++ b/crates/webidl/src/lib.rs @@ -97,7 +97,11 @@ fn parse_source(source: &str) -> Result> { } /// Parse a string of WebIDL source text into a wasm-bindgen AST. -fn parse(webidl_source: &str, unstable_source: &str, allowed_types: Option<&[&str]>) -> Result { +fn parse( + webidl_source: &str, + unstable_source: &str, + allowed_types: Option<&[&str]>, +) -> Result { let mut first_pass_record: FirstPassRecord = Default::default(); first_pass_record.builtin_idents = builtin_idents(); first_pass_record.immutable_slice_whitelist = immutable_slice_whitelist(); @@ -164,7 +168,11 @@ fn parse(webidl_source: &str, unstable_source: &str, allowed_types: Option<&[&st /// Compile the given WebIDL source text into Rust source text containing /// `wasm-bindgen` bindings to the things described in the WebIDL. -pub fn compile(webidl_source: &str, experimental_source: &str, allowed_types: Option<&[&str]>) -> Result { +pub fn compile( + webidl_source: &str, + experimental_source: &str, + allowed_types: Option<&[&str]>, +) -> Result { let ast = parse(webidl_source, experimental_source, allowed_types)?; Ok(compile_ast(ast)) } @@ -785,7 +793,9 @@ impl<'src> FirstPassRecord<'src> { OperationId::IndexingDeleter => Some(format!("The indexing deleter\n\n")), }; let attrs = data.definition_attributes; - for mut method in self.create_imports(attrs, kind, id, op_data, data.stability.is_unstable()) { + for mut method in + self.create_imports(attrs, kind, id, op_data, data.stability.is_unstable()) + { let mut doc = doc.clone(); self.append_required_features_doc(&method, &mut doc, &[]); method.doc_comment = doc; diff --git a/examples/fetch/src/lib.rs b/examples/fetch/src/lib.rs index d1dd7524..96438753 100644 --- a/examples/fetch/src/lib.rs +++ b/examples/fetch/src/lib.rs @@ -40,10 +40,7 @@ pub async fn run(repo: String) -> Result { let url = format!("https://api.github.com/repos/{}/branches/master", repo); - let request = Request::new_with_str_and_init( - &url, - &opts, - )?; + let request = Request::new_with_str_and_init(&url, &opts)?; request .headers()