This commit is contained in:
vms 2021-05-07 14:57:35 +03:00
parent 1d2e2ffe84
commit 0a83fd10d2
2 changed files with 28 additions and 14 deletions

View File

@ -14,9 +14,9 @@
* limitations under the License.
*/
use crate::fce_test::utils;
use crate::fce_test::utils::new_ident;
use crate::fce_test::utils::itype_to_tokens;
use crate::TResult;
use crate::TestGeneratorError;
use fce_wit_parser::interface::it::IType;
use fce_wit_parser::interface::it::IFunctionArg;
@ -32,21 +32,33 @@ pub(super) enum CallParametersSettings {
UserDefined,
}
#[allow(unused_variables)]
pub(super) fn generate_module_method(
module_name: &str,
signature: &FCEFunctionSignature,
cp_setting: CallParametersSettings,
records: &FCERecordTypes,
) -> TResult<TokenStream> {
let func_name = utils::new_ident(&signature.name)?;
let arguments = generate_arguments(signature.arguments.iter(), records)?;
let output_type = generate_output_type(&signature.outputs, records)?;
let fce_call = generate_fce_call(module_name, cp_setting, &signature, records)?;
let cp = match cp_setting {
CallParametersSettings::Default => TokenStream::new(),
let (cp, func_name) = match cp_setting {
CallParametersSettings::Default => {
let func_name = new_ident(&signature.name)?;
(TokenStream::new(), func_name)
}
CallParametersSettings::UserDefined => {
quote! { , cp: fluence_test::internal::CallParameters }
let maybe_comma = if signature.arguments.is_empty() {
TokenStream::new()
} else {
quote! { , }
};
let cp = quote! { #maybe_comma cp: fluence_test::CallParameters };
let func_name = format!("{}_cp", signature.name);
let func_name = new_ident(&func_name)?;
(cp, func_name)
}
};
@ -93,7 +105,7 @@ fn generate_fce_call(
fn generate_arguments_converter<'a>(
args: impl ExactSizeIterator<Item = &'a str>,
) -> TResult<TokenStream> {
let arg_idents: Vec<syn::Ident> = args.map(utils::new_ident).collect::<Result<_, _>>()?;
let arg_idents: Vec<syn::Ident> = args.map(new_ident).collect::<Result<_, _>>()?;
let args_converter =
quote! { let arguments = fluence_test::internal::serde_json::json!([#(#arg_idents),*]); };
@ -127,7 +139,7 @@ fn generate_convert_to_output(
) -> TResult<TokenStream> {
let result_stream = match output_type {
Some(ty) => {
let ty = utils::itype_to_tokens(ty, records)?;
let ty = itype_to_tokens(ty, records)?;
quote! {
let result: #ty = fluence_test::internal::serde_json::from_value(result).expect("the default deserializer shouldn't fail");
}
@ -151,8 +163,8 @@ fn generate_arguments<'a, 'r>(
) -> TResult<Vec<TokenStream>> {
arguments
.map(|argument| -> TResult<_> {
let arg_name = utils::new_ident(&argument.name)?;
let arg_type = utils::itype_to_tokens(&argument.ty, records)?;
let arg_name = new_ident(&argument.name)?;
let arg_type = itype_to_tokens(&argument.ty, records)?;
let arg = quote! { #arg_name: #arg_type };
Ok(arg)
@ -165,7 +177,7 @@ fn generate_output_type(output_types: &[IType], records: &FCERecordTypes) -> TRe
match output_type {
None => Ok(TokenStream::new()),
Some(ty) => {
let output_type = utils::itype_to_tokens(&ty, records)?;
let output_type = itype_to_tokens(&ty, records)?;
let output_type = quote! { -> #output_type };
Ok(output_type)
@ -174,9 +186,11 @@ fn generate_output_type(output_types: &[IType], records: &FCERecordTypes) -> TRe
}
fn get_output_type(output_types: &[IType]) -> TResult<Option<&IType>> {
use crate::TestGeneratorError::ManyFnOutputsUnsupported;
match output_types.len() {
0 => Ok(None),
1 => Ok(Some(&output_types[0])),
_ => Err(TestGeneratorError::ManyFnOutputsUnsupported),
_ => Err(ManyFnOutputsUnsupported),
}
}

View File

@ -27,14 +27,14 @@
#![warn(rust_2018_idioms)]
pub use fluence_sdk_test_macro::fce_test;
pub use fluence_app_service::CallParameters;
pub use fluence_app_service::SecurityTetraplet;
/// These API functions are intended for internal usage in generated code.
/// Normally, you shouldn't use them.
pub mod internal {
pub use fluence_app_service::AppService;
pub use fluence_app_service::TomlAppServiceConfig;
pub use fluence_app_service::CallParameters;
pub use fluence_app_service::SecurityTetraplet;
pub use serde;
pub use serde_json;