Skip to content

Refactor: module_from_opts function #3074

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 40 additions & 32 deletions crates/macro-support/src/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1362,39 +1362,15 @@ impl MacroParse<BindgenAttrs> for syn::ItemConst {
impl MacroParse<BindgenAttrs> 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 = 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)
.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);
Expand Down Expand Up @@ -1439,6 +1415,38 @@ impl MacroParse<Option<ast::ImportModule>> for syn::ForeignItem {
}
}

pub fn module_from_opts(
program: &mut ast::Program,
opts: &BindgenAttrs,
) -> Result<Option<ast::ImportModule>, 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));
}
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
};
Diagnostic::from_vec(errors)?;
Ok(module)
}

/// Get the first type parameter of a generic type, errors on incorrect input.
fn extract_first_ty_param(ty: Option<&syn::Type>) -> Result<Option<syn::Type>, Diagnostic> {
let t = match ty {
Expand Down