Move get_arguments_possibilities into a function

This commit is contained in:
Anton Danilkin 2018-08-10 22:17:29 +03:00
parent ce2c0ded74
commit 2c0e13a033

View File

@ -844,27 +844,17 @@ impl<'a> GetTypeName for Type<'a> {
}
impl<'src> FirstPassRecord<'src> {
/// Use the first pass to convert webidl function arguments to rust arguments.
///
/// `kind` is whether the function is a method, in which case we would need a `self`
/// parameter.
///
/// Uses the first pass to convert webidl function arguments to all possible argument combinations.
/// Returns option that contains a value if the conversion succeeds.
/// The value is a vector of argument variants.
/// Each variant is a vector of original arguments, converted argument types and type names.
fn get_variants(
&self,
arguments: &'src [Argument],
kind: &backend::ast::ImportFunctionKind,
) -> Option<Vec<Vec<(Option<&'src Argument>, (syn::ArgCaptured, Option<String>))>>>
{
let arguments_possibilities = {
/// The value is a vector of argument possibilities.
/// Each possibility is a vector of tuples of converted argument (syn) types and type names
fn get_arguments_possibilities(&self, arguments: &'src [Argument]) -> Option<Vec<Vec<(syn::Type, String)>>> {
fn get_argument_possibilities(record: &FirstPassRecord, argument: &Argument) -> Option<Vec<(syn::Type, String)>> {
let single = match argument {
Argument::Single(single) => single,
Argument::Variadic(_) => return None,
let type_ = match argument {
Argument::Single(single) => &single.type_.type_,
Argument::Variadic(variadic) => &variadic.type_,
};
match single.type_.type_.get_argument_possibilities(record) {
match type_.get_argument_possibilities(record) {
None => {
warn!("Argument's type is not yet supported: {:?}", argument);
None
@ -911,11 +901,27 @@ impl<'src> FirstPassRecord<'src> {
arguments_possibilities = new_arguments_possibilities
}
optional_arguments_possibilities.extend(arguments_possibilities.into_iter());
optional_arguments_possibilities
Some(optional_arguments_possibilities)
} else {
vec![Vec::new()]
Some(vec![Vec::new()])
}
};
}
/// Uses the first pass to convert webidl function arguments to rust arguments.
///
/// `kind` is whether the function is a method, in which case we would need a `self`
/// parameter.
///
/// Returns option that contains a value if the conversion succeeds.
/// The value is a vector of argument variants.
/// Each variant is a vector of tuples of original arguments, converted argument types and type names.
fn get_variants(
&self,
arguments: &'src [Argument],
kind: &backend::ast::ImportFunctionKind,
) -> Option<Vec<Vec<(Option<&'src Argument>, syn::ArgCaptured, Option<String>)>>>
{
let arguments_possibilities = self.get_arguments_possibilities(arguments)?;
let mut result = Vec::new();
for arguments_possibility in arguments_possibilities {
let mut res = if let backend::ast::ImportFunctionKind::Method {
@ -931,11 +937,9 @@ impl<'src> FirstPassRecord<'src> {
res.push(
(
None,
(
simple_fn_arg(raw_ident("self_"), shared_ref(ty.clone())),
None,
),
)
);
res
} else {
@ -949,14 +953,12 @@ impl<'src> FirstPassRecord<'src> {
res.push(
(
Some(argument),
(
simple_fn_arg(
rust_ident(&single.identifier.0.to_snake_case()),
argument_possibility.0.clone()
),
Some(argument_possibility.1.clone()),
),
)
);
}
result.push(res);
@ -1011,7 +1013,6 @@ impl<'src> FirstPassRecord<'src> {
}
let variants = self.get_variants(arguments, &kind)?;
let multiple_variants = variants.len() > 1;
let mut arguments_count_variants_count = BTreeMap::new();
for variant in &variants {
arguments_count_variants_count
@ -1020,14 +1021,11 @@ impl<'src> FirstPassRecord<'src> {
.or_insert(1usize);
}
let mut result = Vec::new();
for variant in variants {
let (arguments, variant_types_variant_names): (Vec<_>, Vec<(_, _)>) = variant.into_iter().unzip();
let (variant_types, variant_names): (Vec<_>, Vec<_>) = variant_types_variant_names.into_iter().unzip();
let variant_names_len = variant_names.len();
let rust_name = if multiple_variants {
for variant in &variants {
let rust_name = if variants.len() > 1 {
let mut rust_name = rust_name.clone();
let mut first = true;
for (argument, variant_name) in arguments.iter().zip(variant_names) {
for (argument, _, variant_name) in variant {
if let Some(type_name) = variant_name {
if first {
rust_name.push_str("_using_");
@ -1035,7 +1033,7 @@ impl<'src> FirstPassRecord<'src> {
} else {
rust_name.push_str("_and_");
}
if arguments_count_variants_count[&variant_names_len] == 1 {
if arguments_count_variants_count[&variant.len()] == 1 {
if let Some(argument) = argument {
let argument = match argument {
Argument::Single(single) => single,
@ -1067,7 +1065,7 @@ impl<'src> FirstPassRecord<'src> {
result.push(backend::ast::ImportFunction {
function: backend::ast::Function {
name: name.to_string(),
arguments: variant_types,
arguments: variant.iter().map(|variant| variant.1.clone()).collect(),
ret: ret.clone(),
rust_attrs: vec![],
rust_vis: public(),