1
0
mirror of https://github.com/fluencelabs/marine-rs-sdk-test synced 2024-12-04 15:20:18 +00:00
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
crates/fce-test-macro-impl/src/fce_test/module_generator
fluence-test/src

View File

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

View File

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