From 5bf030047682729c04e555549a93f98a0da05c43 Mon Sep 17 00:00:00 2001 From: Lukas Lihotzki Date: Wed, 7 Sep 2022 17:04:59 +0200 Subject: [PATCH 1/3] Refactor: module_from_opts function --- crates/macro-support/src/parser.rs | 54 +++++++++++++++++------------- 1 file changed, 31 insertions(+), 23 deletions(-) diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index a3148fdb184..9586247905f 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -1372,29 +1372,7 @@ impl MacroParse for syn::ItemForeignMod { )); } } - let module = if let Some((name, span)) = opts.module() { - if opts.inline_js().is_some() { - let msg = "cannot specify both `module` and `inline_js`"; - errors.push(Diagnostic::span_error(span, msg)); - } - if opts.raw_module().is_some() { - let msg = "cannot specify both `module` and `raw_module`"; - errors.push(Diagnostic::span_error(span, msg)); - } - Some(ast::ImportModule::Named(name.to_string(), span)) - } else if let Some((name, span)) = opts.raw_module() { - if opts.inline_js().is_some() { - let msg = "cannot specify both `raw_module` and `inline_js`"; - errors.push(Diagnostic::span_error(span, msg)); - } - Some(ast::ImportModule::RawNamed(name.to_string(), span)) - } else if let Some((js, span)) = opts.inline_js() { - let i = program.inline_js.len(); - program.inline_js.push(js.to_string()); - Some(ast::ImportModule::Inline(i, span)) - } else { - None - }; + let module = module_from_opts(program, &opts, &mut errors); for item in self.items.into_iter() { if let Err(e) = item.macro_parse(program, module.clone()) { errors.push(e); @@ -1439,6 +1417,36 @@ impl MacroParse> for syn::ForeignItem { } } +pub fn module_from_opts( + program: &mut ast::Program, + opts: &BindgenAttrs, + errors: &mut Vec, +) -> Option { + if let Some((name, span)) = opts.module() { + if opts.inline_js().is_some() { + let msg = "cannot specify both `module` and `inline_js`"; + errors.push(Diagnostic::span_error(span, msg)); + } + if opts.raw_module().is_some() { + let msg = "cannot specify both `module` and `raw_module`"; + errors.push(Diagnostic::span_error(span, msg)); + } + Some(ast::ImportModule::Named(name.to_string(), span)) + } else if let Some((name, span)) = opts.raw_module() { + if opts.inline_js().is_some() { + let msg = "cannot specify both `raw_module` and `inline_js`"; + errors.push(Diagnostic::span_error(span, msg)); + } + Some(ast::ImportModule::RawNamed(name.to_string(), span)) + } else if let Some((js, span)) = opts.inline_js() { + let i = program.inline_js.len(); + program.inline_js.push(js.to_string()); + Some(ast::ImportModule::Inline(i, span)) + } else { + None + } +} + /// Get the first type parameter of a generic type, errors on incorrect input. fn extract_first_ty_param(ty: Option<&syn::Type>) -> Result, Diagnostic> { let t = match ty { From e9b30bdd7df60cf50cc43dd10cd5fd8c65bfcf2a Mon Sep 17 00:00:00 2001 From: Lukas Lihotzki Date: Wed, 7 Sep 2022 17:38:04 +0200 Subject: [PATCH 2/3] Refactor: Return Diagnostic from module_from_opts --- crates/macro-support/src/parser.rs | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index 9586247905f..e43141be1aa 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -1372,7 +1372,9 @@ impl MacroParse for syn::ItemForeignMod { )); } } - let module = module_from_opts(program, &opts, &mut errors); + let module = module_from_opts(program, &opts) + .map_err(|e| errors.push(e)) + .unwrap_or_default(); for item in self.items.into_iter() { if let Err(e) = item.macro_parse(program, module.clone()) { errors.push(e); @@ -1420,9 +1422,9 @@ impl MacroParse> for syn::ForeignItem { pub fn module_from_opts( program: &mut ast::Program, opts: &BindgenAttrs, - errors: &mut Vec, -) -> Option { - if let Some((name, span)) = opts.module() { +) -> Result, Diagnostic> { + let mut errors = Vec::new(); + let module = if let Some((name, span)) = opts.module() { if opts.inline_js().is_some() { let msg = "cannot specify both `module` and `inline_js`"; errors.push(Diagnostic::span_error(span, msg)); @@ -1444,7 +1446,9 @@ pub fn module_from_opts( Some(ast::ImportModule::Inline(i, span)) } else { None - } + }; + Diagnostic::from_vec(errors)?; + Ok(module) } /// Get the first type parameter of a generic type, errors on incorrect input. From daea56fadc090706c7511889143a72c34d65b106 Mon Sep 17 00:00:00 2001 From: Lukas Lihotzki Date: Wed, 7 Sep 2022 18:04:08 +0200 Subject: [PATCH 3/3] Refactor: Use Option::filter --- crates/macro-support/src/parser.rs | 14 +++++--------- 1 file changed, 5 insertions(+), 9 deletions(-) diff --git a/crates/macro-support/src/parser.rs b/crates/macro-support/src/parser.rs index e43141be1aa..e526dc48d9c 100644 --- a/crates/macro-support/src/parser.rs +++ b/crates/macro-support/src/parser.rs @@ -1362,15 +1362,11 @@ impl MacroParse for syn::ItemConst { impl MacroParse for syn::ItemForeignMod { fn macro_parse(self, program: &mut ast::Program, opts: BindgenAttrs) -> Result<(), Diagnostic> { let mut errors = Vec::new(); - match self.abi.name { - Some(ref l) if l.value() == "C" => {} - None => {} - Some(ref other) => { - errors.push(err_span!( - other, - "only foreign mods with the `C` ABI are allowed" - )); - } + if let Some(other) = self.abi.name.filter(|l| l.value() != "C") { + errors.push(err_span!( + other, + "only foreign mods with the `C` ABI are allowed" + )); } let module = module_from_opts(program, &opts) .map_err(|e| errors.push(e))