fn expand_function_arguments(
    fun: &Function,
    custom_enums: &HashMap<String, Property>,
    custom_structs: &HashMap<String, Property>,
) -> Result<(TokenStream, TokenStream), Error> {
    let mut args = Vec::with_capacity(fun.inputs.len());
    let mut call_args = Vec::with_capacity(fun.inputs.len());

    // For each [`Property`] in a function input we expand:
    // 1. The name of the argument;
    // 2. The type of the argument;
    for (i, param) in fun.inputs.iter().enumerate() {
        // This is a (likely) temporary workaround the fact that
        // Sway ABI functions require gas, coin amount, and color arguments
        // pre-pending the user-defined function arguments.
        // Since these values (gas, coin, color) are configured elsewhere when
        // creating a contract instance in the SDK, it would be noisy to keep them
        // in the signature of the function that we're expanding here.
        // It's the difference between being forced to write:
        // contract_instance.increment_counter($gas, $coin, $color, 42)
        // versus simply writing:
        // contract_instance.increment_counter(42)
        // Note that _any_ significant change in the way the JSON ABI is generated
        // could affect this function expansion.
        if param.name == "gas_" || param.name == "amount_" || param.name == "color_" {
            continue;
        }
        // TokenStream representing the name of the argument
        let name = expand_input_name(i, &param.name);

        let opt_custom_type = match param.type_field.split_whitespace().collect::<Vec<_>>()[0] {
            "enum" => Some(&CustomType::Enum),
            "struct" => Some(&CustomType::Struct),
            _ => None,
        };

        let rust_custom_name = if let Some(c) = opt_custom_type {
            match c {
                CustomType::Enum => {
                    let name =
                        extract_custom_type_name_from_abi_property(param, opt_custom_type.unwrap())
                            .unwrap();
                    custom_enums.get(&name)
                }
                CustomType::Struct => {
                    let name =
                        extract_custom_type_name_from_abi_property(param, opt_custom_type.unwrap())
                            .unwrap();
                    custom_structs.get(&name)
                }
            }
        } else {
            None
        };

        // TokenStream representing the type of the argument
        let ty = expand_input_param(fun, &param.name, &parse_param(param)?, &rust_custom_name)?;

        // Add the TokenStream to argument declarations
        args.push(quote! { #name: #ty });

        // This `name` TokenStream is also added to the call arguments
        call_args.push(name);
    }

    // The final TokenStream of the argument declaration in a function declaration
    let args = quote! { #( , #args )* };

    // The final TokenStream of the arguments being passed in a function call
    // It'll look like `&[my_arg.into_token(), another_arg.into_token()]`
    // as the [`Contract`] `method_hash` function expects a slice of Tokens
    // in order to encode the call.
    let call_args = match call_args.len() {
        0 => quote! { () },
        _ => quote! { &[ #(#call_args.into_token(), )* ] },
    };

    Ok((args, call_args))
}
/// Expands the arguments in a function declaration and the same arguments as input
/// to a function call. For instance:
/// 1. The `my_arg: u32` in `pub fn my_func(my_arg: u32) -> ()`
/// 2. The `my_arg.into_token()` in `another_fn_call(my_arg.into_token())`
Expands the arguments in a function declaration and the same arguments as input
to a function call. For instance:
1.
Expands the arguments in a function declaration and the same arguments as input to a function call . For instance : 1 .
fn expand_function_arguments(
    fun: &Function,
    custom_enums: &HashMap<String, Property>,
    custom_structs: &HashMap<String, Property>,
) -> Result<(TokenStream, TokenStream), Error> {
    let mut args = Vec::with_capacity(fun.inputs.len());
    let mut call_args = Vec::with_capacity(fun.inputs.len());
    // For each [`Property`] in a function input we expand:
    // 1. The name of the argument;
    // 2. The type of the argument;
    for (i, param) in fun.inputs.iter().enumerate() {
        // This is a (likely) temporary workaround the fact that
        // Sway ABI functions require gas, coin amount, and color arguments
        // pre-pending the user-defined function arguments.
        // Since these values (gas, coin, color) are configured elsewhere when
        // creating a contract instance in the SDK, it would be noisy to keep them
        // in the signature of the function that we're expanding here.
        // It's the difference between being forced to write:
        // contract_instance.increment_counter($gas, $coin, $color, 42)
        // versus simply writing:
        // contract_instance.increment_counter(42)
        // Note that _any_ significant change in the way the JSON ABI is generated
        // could affect this function expansion.
        if param.name == "gas_" || param.name == "amount_" || param.name == "color_" {
            continue;
        }
        // TokenStream representing the name of the argument
        let name = expand_input_name(i, &param.name);
        let opt_custom_type = match param.type_field.split_whitespace().collect::<Vec<_>>()[0] {
            "enum" => Some(&CustomType::Enum),
            "struct" => Some(&CustomType::Struct),
            _ => None,
        };
        let rust_custom_name = if let Some(c) = opt_custom_type {
            match c {
                CustomType::Enum => {
                    let name =
                        extract_custom_type_name_from_abi_property(param, opt_custom_type.unwrap())
                            .unwrap();
                    custom_enums.get(&name)
                }
                CustomType::Struct => {
                    let name =
                        extract_custom_type_name_from_abi_property(param, opt_custom_type.unwrap())
                            .unwrap();
                    custom_structs.get(&name)
                }
            }
        } else {
            None
        };
        // TokenStream representing the type of the argument
        let ty = expand_input_param(fun, &param.name, &parse_param(param)?, &rust_custom_name)?;
        // Add the TokenStream to argument declarations
        args.push(quote! { #name: #ty });
        // This `name` TokenStream is also added to the call arguments
        call_args.push(name);
    }
    // The final TokenStream of the argument declaration in a function declaration
    let args = quote! { #( , #args )* };
    // The final TokenStream of the arguments being passed in a function call
    // It'll look like `&[my_arg.into_token(), another_arg.into_token()]`
    // as the [`Contract`] `method_hash` function expects a slice of Tokens
    // in order to encode the call.
    let call_args = match call_args.len() {
        0 => quote! { () },
        _ => quote! { &[ #(#call_args.into_token(), )* ] },
    };
    Ok((args, call_args))
}
fn expand_function_arguments ( fun : & Function , custom_enums : & HashMap < String , Property > , custom_structs : & HashMap < String , Property > , ) -> Result < ( TokenStream , TokenStream ) , Error > { let mut args = Vec :: with_capacity ( fun . inputs . len ( ) ) ; let mut call_args = Vec :: with_capacity ( fun . inputs . len ( ) ) ; // For each [`Property`] in a function input we expand: // 1. The name of the argument; // 2. The type of the argument; for ( i , param ) in fun . inputs . iter ( ) . enumerate ( ) { // This is a (likely) temporary workaround the fact that // Sway ABI functions require gas, coin amount, and color arguments // pre-pending the user-defined function arguments. // Since these values (gas, coin, color) are configured elsewhere when // creating a contract instance in the SDK, it would be noisy to keep them // in the signature of the function that we're expanding here. // It's the difference between being forced to write: // contract_instance.increment_counter($gas, $coin, $color, 42) // versus simply writing: // contract_instance.increment_counter(42) // Note that _any_ significant change in the way the JSON ABI is generated // could affect this function expansion. if param . name == " " || param . name == " " || param . name == " " { continue ; } // TokenStream representing the name of the argument let name = expand_input_name ( i , & param . name ) ; let opt_custom_type = match param . type_field . split_whitespace ( ) . collect :: < Vec < _ > > ( ) [ 0 ] { " " => Some ( & CustomType :: Enum ) , " " => Some ( & CustomType :: Struct ) , _ => None , } ; let rust_custom_name = if let Some ( c ) = opt_custom_type { match c { CustomType :: Enum => { let name = extract_custom_type_name_from_abi_property ( param , opt_custom_type . unwrap ( ) ) . unwrap ( ) ; custom_enums . get ( & name ) } CustomType :: Struct => { let name = extract_custom_type_name_from_abi_property ( param , opt_custom_type . unwrap ( ) ) . unwrap ( ) ; custom_structs . get ( & name ) } } } else { None } ; // TokenStream representing the type of the argument let ty = expand_input_param ( fun , & param . name , & parse_param ( param ) ? , & rust_custom_name ) ? ; // Add the TokenStream to argument declarations args . push ( quote ! { name ty } ) ; // This `name` TokenStream is also added to the call arguments call_args . push ( name ) ; } // The final TokenStream of the argument declaration in a function declaration let args = quote ! { ( args ) } ; // The final TokenStream of the arguments being passed in a function call // It'll look like `&[my_arg.into_token(), another_arg.into_token()]` // as the [`Contract`] `method_hash` function expects a slice of Tokens // in order to encode the call. let call_args = match call_args . len ( ) { 0 => quote ! { ( ) } , _ => quote ! { [ ( call_args into_token ( ) ) ] } , } ; Ok ( ( args , call_args ) ) }
Expands the arguments in a function declaration and the same arguments as input
to a function call.
Expands the arguments in a function declaration and the same arguments as input to a function call .
fun
&Function
custom_enums
&HashMap<String, Property>
custom_structs
&HashMap<String, Property>