From e4c7ed319bd9200d85215ef5032a4fe7fe9742dd Mon Sep 17 00:00:00 2001 From: vms Date: Sun, 28 Mar 2021 22:51:50 +0300 Subject: [PATCH] cleanup --- Cargo.toml | 38 ++-------------------- crates/fce-test-macro/Cargo.toml | 3 +- crates/fce-test-macro/src/attributes.rs | 43 ++----------------------- crates/fce-test-macro/src/fce_test.rs | 43 +++++++++++++------------ crates/fce-test-macro/src/lib.rs | 7 ++-- crates/main/src/module_manifest.rs | 10 +++--- fluence-test/Cargo.toml | 22 +++++++++++++ fluence-test/src/lib.rs | 36 +++++++++++++++++++++ fluence/Cargo.toml | 29 +++++++++++++++++ {src => fluence/src}/lib.rs | 6 ---- 10 files changed, 121 insertions(+), 116 deletions(-) create mode 100644 fluence-test/Cargo.toml create mode 100644 fluence-test/src/lib.rs create mode 100644 fluence/Cargo.toml rename {src => fluence/src}/lib.rs (94%) diff --git a/Cargo.toml b/Cargo.toml index 2cd774f..42176e5 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,43 +1,9 @@ -[package] -name = "fluence" -version = "0.5.0" # remember to update html_root_url -description = "Fluence backend SDK for developing backend applications for the Fluence network" -documentation = "https://docs.rs/fluence/" -repository = "https://github.com/fluencelabs/rust-sdk" -authors = ["Fluence Labs"] -readme = "README.md" -keywords = ["fluence", "sdk", "webassembly"] -categories = ["api-bindings", "wasm"] -license = "Apache-2.0" -edition = "2018" - -[package.metadata.docs.rs] # https://docs.rs/about -all-features = true - -[lib] -path = "src/lib.rs" - -[dependencies] -fluence-sdk-macro = { path = "crates/fce-macro", version = "=0.5.0" } -fluence-sdk-test-macro = { path = "crates/fce-test-macro", version = "=0.5.0", optional = true } -fluence-sdk-main = { path = "crates/main", version = "=0.5.0" } - -fluence-app-service= { version = "0.5.2", features = ["raw-module-api"], optional = true } - -[features] -# Print some internal logs by log_utf8_string -debug = ["fluence-sdk-main/debug"] - -# Enable logger (this will cause log_utf8_string to appear in imports) -logger = ["fluence-sdk-main/logger"] - -# Enable the fce-test features (it'll bring fluence-app-service as a dependency) -fce-test = ["fluence-sdk-test-macro", "fluence-app-service"] - [workspace] members = [ "crates/fce-macro", "crates/fce-test-macro", "crates/main", "crates/wit", + "fluence", + "fluence-test" ] diff --git a/crates/fce-test-macro/Cargo.toml b/crates/fce-test-macro/Cargo.toml index e95cb65..e0b9cd5 100644 --- a/crates/fce-test-macro/Cargo.toml +++ b/crates/fce-test-macro/Cargo.toml @@ -18,7 +18,6 @@ proc-macro = true [dependencies] quote = "1.0.9" proc-macro2 = "1.0.24" -serde = { version = "=1.0.118", features = ["derive"] } -serde_json = "1.0.56" syn = { version = '1.0.64', features = ['full'] } uuid = { version = "0.8.2", features = ["v4"] } +darling = "0.12.2" diff --git a/crates/fce-test-macro/src/attributes.rs b/crates/fce-test-macro/src/attributes.rs index 49f36c3..1393ff8 100644 --- a/crates/fce-test-macro/src/attributes.rs +++ b/crates/fce-test-macro/src/attributes.rs @@ -14,48 +14,9 @@ * limitations under the License. */ -use syn::parse::Parse; -use syn::parse::ParseStream; +use darling::FromMeta; -#[derive(Debug, Default, Clone)] +#[derive(Debug, Default, Clone, FromMeta)] pub(crate) struct FCETestAttributes { pub(crate) config_path: String, } - -impl Parse for FCETestAttributes { - fn parse(input: ParseStream<'_>) -> syn::Result { - let config_file_path = parse_config_file_path(input)?; - let attributes = FCETestAttributes { - config_path: config_file_path, - }; - - Ok(attributes) - } -} - -pub(crate) fn parse_config_file_path(token_stream: ParseStream<'_>) -> syn::Result { - let attr_name = token_stream.step(|cursor| match cursor.ident() { - Some((ident, rem)) => Ok((ident, rem)), - None => Err(cursor.error("Expected a valid identifier")), - })?; - - match attr_name.to_string().as_str() { - "config" => { - // trying to parse `=` - token_stream.parse::()?; - - match token_stream.parse::() { - Ok(config_file_path) => Ok(config_file_path.to_string()), - Err(e) => Err(syn::Error::new( - attr_name.span(), - format!("failed to parse a config file path: {}", e), - )), - } - } - - attr => Err(syn::Error::new( - attr_name.span(), - format!("Expected 'config' identifier, but {} found", attr), - )), - } -} diff --git a/crates/fce-test-macro/src/fce_test.rs b/crates/fce-test-macro/src/fce_test.rs index 678432d..2d63100 100644 --- a/crates/fce-test-macro/src/fce_test.rs +++ b/crates/fce-test-macro/src/fce_test.rs @@ -14,22 +14,27 @@ * limitations under the License. */ -use proc_macro2::TokenStream; +use crate::attributes::FCETestAttributes; + +use proc_macro::TokenStream; +use proc_macro2::TokenStream as TokenStream2; use quote::quote; -pub(super) fn fce_test_impl( - attr: TokenStream, - func_input: syn::ItemFn, -) -> Result { - use crate::attributes::FCETestAttributes; +pub(super) fn fce_test_impl(attrs: TokenStream, func_input: syn::ItemFn) -> TokenStream { + use darling::FromMeta; - let attrs = syn::parse2::(attr).map_err(|e| e.into_compile_error())?; - let generated_test = generate_test_glue_code(func_input, &attrs.config_path); + let attrs = syn::parse_macro_input!(attrs as syn::AttributeArgs); + let attrs = match FCETestAttributes::from_list(&attrs) { + Ok(v) => v, + Err(e) => { + return TokenStream::from(e.write_errors()); + } + }; - Ok(generated_test) + generate_test_glue_code(func_input, &attrs.config_path).into() } -fn generate_test_glue_code(func: syn::ItemFn, config_path: &str) -> TokenStream { +fn generate_test_glue_code(func: syn::ItemFn, config_path: &str) -> TokenStream2 { let fce_ctor = generate_fce_ctor(config_path); let original_block = func.block; let signature = func.sig; @@ -44,27 +49,23 @@ fn generate_test_glue_code(func: syn::ItemFn, config_path: &str) -> TokenStream } } -fn generate_fce_ctor(config_path: &str) -> TokenStream { - let config_path = new_ident(config_path); +fn generate_fce_ctor(config_path: &str) -> TokenStream2 { + let config_path = quote! { #config_path }; let tmp_file_path = std::env::temp_dir(); let random_uuid = uuid::Uuid::new_v4().to_string(); - let service_id = new_ident(&random_uuid); + let service_id = quote! { #random_uuid }; let tmp_file_path = tmp_file_path.join(random_uuid); let tmp_file_path = tmp_file_path.to_string_lossy().to_string(); - let tmp_file_path = new_ident(&tmp_file_path); + let tmp_file_path = quote! { #tmp_file_path }; quote! { - let mut __fce__generated_fce_config = fluence::internal::test::TomlAppServiceConfig::load(#config_path) + let mut __fce__generated_fce_config = fluence_test::internal::TomlAppServiceConfig::load(#config_path.to_string()) .unwrap_or_else(|e| panic!("app service located at `{}` config can't be loaded: {}", #config_path, e)); - __fce__generated_fce_config.service_base_dir = Some(#tmp_file_path); + __fce__generated_fce_config.service_base_dir = Some(#tmp_file_path.to_string()); - let fce = fluence::internal::test::AppService::new_with_empty_facade(__fce__generated_fce_config, #service_id, std::collections::HashMap::new()) + let mut fce = fluence_test::internal::AppService::new_with_empty_facade(__fce__generated_fce_config, #service_id, std::collections::HashMap::new()) .unwrap_or_else(|e| panic!("app service can't be created: {}", e)); } } - -fn new_ident(name: &str) -> syn::Ident { - syn::Ident::new(name, proc_macro2::Span::call_site()) -} diff --git a/crates/fce-test-macro/src/lib.rs b/crates/fce-test-macro/src/lib.rs index 7e693b1..8b3b49d 100644 --- a/crates/fce-test-macro/src/lib.rs +++ b/crates/fce-test-macro/src/lib.rs @@ -15,16 +15,15 @@ */ #![doc(html_root_url = "https://docs.rs/fluence-sdk-macro/0.4.2")] -/* #![deny( dead_code, nonstandard_style, unused_imports, unused_mut, + unused_variables, unused_unsafe, unreachable_patterns )] - */ #![warn(rust_2018_idioms)] #![recursion_limit = "1024"] @@ -56,7 +55,5 @@ use proc_macro::TokenStream; #[proc_macro_attribute] pub fn fce_test(attrs: TokenStream, input: TokenStream) -> TokenStream { let func_input = syn::parse_macro_input!(input as syn::ItemFn); - let result = fce_test_impl(attrs.into(), func_input).unwrap_or_else(std::convert::identity); - - result.into() + fce_test_impl(attrs.into(), func_input) } diff --git a/crates/main/src/module_manifest.rs b/crates/main/src/module_manifest.rs index 95328eb..003864e 100644 --- a/crates/main/src/module_manifest.rs +++ b/crates/main/src/module_manifest.rs @@ -32,7 +32,7 @@ macro_rules! module_manifest { + __FCE_SDK_REPOSITORY_SIZE + __FCE_SDK_FIELD_PREFIX_SIZE * 4; - const fn append_data( + const fn __fce_sdk_append_data( mut manifest: [u8; __FCE_MANIFEST_SIZE], data: &'static str, offset: usize, @@ -63,10 +63,10 @@ macro_rules! module_manifest { let manifest: [u8; __FCE_MANIFEST_SIZE] = [0; __FCE_MANIFEST_SIZE]; let offset = 0; - let (manifest, offset) = append_data(manifest, $authors, offset); - let (manifest, offset) = append_data(manifest, $version, offset); - let (manifest, offset) = append_data(manifest, $description, offset); - let (manifest, _) = append_data(manifest, $repository, offset); + let (manifest, offset) = __fce_sdk_append_data(manifest, $authors, offset); + let (manifest, offset) = __fce_sdk_append_data(manifest, $version, offset); + let (manifest, offset) = __fce_sdk_append_data(manifest, $description, offset); + let (manifest, _) = __fce_sdk_append_data(manifest, $repository, offset); manifest } diff --git a/fluence-test/Cargo.toml b/fluence-test/Cargo.toml new file mode 100644 index 0000000..bf7fcef --- /dev/null +++ b/fluence-test/Cargo.toml @@ -0,0 +1,22 @@ +[package] +name = "fluence-test" +version = "0.5.0" # remember to update html_root_url +description = "Fluence backend SDK for testing" +documentation = "https://docs.rs/fluence/" +repository = "https://github.com/fluencelabs/rust-sdk" +authors = ["Fluence Labs"] +readme = "README.md" +keywords = ["fluence", "sdk", "webassembly"] +categories = ["api-bindings", "wasm"] +license = "Apache-2.0" +edition = "2018" + +[package.metadata.docs.rs] # https://docs.rs/about +all-features = true + +[lib] +path = "src/lib.rs" + +[dependencies] +fluence-sdk-test-macro = { path = "../crates/fce-test-macro", version = "=0.5.0" } +fluence-app-service= { version = "0.5.2", features = ["raw-module-api"] } diff --git a/fluence-test/src/lib.rs b/fluence-test/src/lib.rs new file mode 100644 index 0000000..a3a4c6d --- /dev/null +++ b/fluence-test/src/lib.rs @@ -0,0 +1,36 @@ +/* + * Copyright 2020 Fluence Labs Limited + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#![doc(html_root_url = "https://docs.rs/fluence/0.5.0")] +#![deny( + dead_code, + nonstandard_style, + unused_imports, + unused_mut, + unused_variables, + unused_unsafe, + unreachable_patterns +)] +#![warn(rust_2018_idioms)] + +pub use fluence_sdk_test_macro::fce_test; + +/// 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; +} diff --git a/fluence/Cargo.toml b/fluence/Cargo.toml new file mode 100644 index 0000000..e093b47 --- /dev/null +++ b/fluence/Cargo.toml @@ -0,0 +1,29 @@ +[package] +name = "fluence" +version = "0.5.0" # remember to update html_root_url +description = "Fluence backend SDK for developing backend applications for the Fluence network" +documentation = "https://docs.rs/fluence/" +repository = "https://github.com/fluencelabs/rust-sdk" +authors = ["Fluence Labs"] +readme = "README.md" +keywords = ["fluence", "sdk", "webassembly"] +categories = ["api-bindings", "wasm"] +license = "Apache-2.0" +edition = "2018" + +[package.metadata.docs.rs] # https://docs.rs/about +all-features = true + +[lib] +path = "src/lib.rs" + +[dependencies] +fluence-sdk-macro = { path = "../crates/fce-macro", version = "=0.5.0" } +fluence-sdk-main = { path = "../crates/main", version = "=0.5.0" } + +[features] +# Print some internal logs by log_utf8_string +debug = ["fluence-sdk-main/debug"] + +# Enable logger (this will cause log_utf8_string to appear in imports) +logger = ["fluence-sdk-main/logger"] diff --git a/src/lib.rs b/fluence/src/lib.rs similarity index 94% rename from src/lib.rs rename to fluence/src/lib.rs index 87eab47..24d6e59 100644 --- a/src/lib.rs +++ b/fluence/src/lib.rs @@ -94,10 +94,4 @@ pub mod internal { pub use fluence_sdk_main::get_result_size; pub use fluence_sdk_main::set_result_ptr; pub use fluence_sdk_main::set_result_size; - - #[cfg(feature = "fce-test")] - pub mod test { - pub use fluence_app_service::AppService; - pub use fluence_app_service::AppServiceConfig; - } }