Generate structs in a separate mod (#36)

This commit is contained in:
Mike Voronov 2021-05-17 14:58:00 +03:00 committed by GitHub
parent 3de1d949e4
commit 2ac0699905
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 85 additions and 73 deletions

100
Cargo.lock generated
View File

@ -382,18 +382,6 @@ dependencies = [
"libc",
]
[[package]]
name = "fluence"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f91d764c08b24ba9b4e849151c0adef603759d6e1bba445c8efccebf1bafd83a"
dependencies = [
"fluence-sdk-main 0.6.6",
"marine-macro 0.6.6",
"marine-timestamp-macro 0.6.6",
"serde",
]
[[package]]
name = "fluence"
version = "0.6.8"
@ -405,6 +393,18 @@ dependencies = [
"trybuild",
]
[[package]]
name = "fluence"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a92116bbed3be3afee952d62d6009f1a365bde985a1cf59fb933144308660743"
dependencies = [
"fluence-sdk-main 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
"marine-macro 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
"marine-timestamp-macro 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde",
]
[[package]]
name = "fluence-app-service"
version = "0.7.2"
@ -428,8 +428,8 @@ source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9421599c5540e50f3854a0a48702c31408ac1cfb06314fe391792daa3a9d800b"
dependencies = [
"cmd_lib",
"fluence 0.6.6",
"fluence-sdk-main 0.6.6",
"fluence 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
"fluence-sdk-main 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
"itertools 0.9.0",
"log",
"marine-runtime",
@ -458,17 +458,6 @@ dependencies = [
"wast",
]
[[package]]
name = "fluence-sdk-main"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "887afdd8429bfc0f5d27258ddf95e3140c404932b8ace09b28468638320f6bf3"
dependencies = [
"log",
"marine-macro 0.6.6",
"serde",
]
[[package]]
name = "fluence-sdk-main"
version = "0.6.8"
@ -480,6 +469,17 @@ dependencies = [
"simple_logger",
]
[[package]]
name = "fluence-sdk-main"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fcbf30472768d331ea6a6853bdf1fc6166ba697d46a9ce74c3ccf5764a58414a"
dependencies = [
"log",
"marine-macro 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
"serde",
]
[[package]]
name = "fluence-sdk-wit"
version = "0.6.1"
@ -496,7 +496,7 @@ dependencies = [
[[package]]
name = "fluence-test"
version = "0.1.8"
version = "0.1.9"
dependencies = [
"fluence-app-service",
"marine-test-macro",
@ -812,15 +812,6 @@ dependencies = [
"wasmer-runtime-core-fl",
]
[[package]]
name = "marine-macro"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "67b8d5e91818871ad7d2464a32f5a158f85b6cc5eda289d97478badadb73e141"
dependencies = [
"marine-macro-impl 0.6.6",
]
[[package]]
name = "marine-macro"
version = "0.6.8"
@ -829,17 +820,12 @@ dependencies = [
]
[[package]]
name = "marine-macro-impl"
version = "0.6.6"
name = "marine-macro"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2e216a77d77323575dd491cd5e531205e2ed8e47c65dd4ff69fb3804045b45cc"
checksum = "b9107f5956f578d4815a17c963836cffadf481801ae453e0247693e87f1de734"
dependencies = [
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn",
"uuid",
"marine-macro-impl 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
]
[[package]]
@ -855,6 +841,20 @@ dependencies = [
"uuid",
]
[[package]]
name = "marine-macro-impl"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7244dd6e5d624bb9d1e63777c3bbc3a8c857973d2b7633aa8d0b612d7770a55d"
dependencies = [
"proc-macro2",
"quote",
"serde",
"serde_json",
"syn",
"uuid",
]
[[package]]
name = "marine-module-info-parser"
version = "0.1.0"
@ -863,7 +863,7 @@ checksum = "003017e314cebb5c774569a4257f5eca82106cff170b124c6712f2fcd3ceefef"
dependencies = [
"anyhow",
"chrono",
"fluence-sdk-main 0.6.6",
"fluence-sdk-main 0.6.8 (registry+https://github.com/rust-lang/crates.io-index)",
"semver 0.11.0",
"serde",
"thiserror",
@ -902,7 +902,7 @@ dependencies = [
[[package]]
name = "marine-test-macro"
version = "0.1.8"
version = "0.1.9"
dependencies = [
"marine-test-macro-impl",
"proc-macro-error",
@ -913,7 +913,7 @@ dependencies = [
[[package]]
name = "marine-test-macro-impl"
version = "0.1.8"
version = "0.1.9"
dependencies = [
"darling",
"fluence-app-service",
@ -927,9 +927,7 @@ dependencies = [
[[package]]
name = "marine-timestamp-macro"
version = "0.6.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "54d65dd757302f2a19d2ea8d231627ca3246a58ac677d22f40bb61ba649d97a0"
version = "0.6.8"
dependencies = [
"chrono",
"quote",
@ -938,6 +936,8 @@ dependencies = [
[[package]]
name = "marine-timestamp-macro"
version = "0.6.8"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ecb3037577a262360b7e2f0fb5470b7ad0aabcefcb8d2421a4871a854d0a37b2"
dependencies = [
"chrono",
"quote",

View File

@ -1,6 +1,6 @@
[package]
name = "marine-test-macro-impl"
version = "0.1.8" # remember to update html_root_url
version = "0.1.9" # remember to update html_root_url
edition = "2018"
description = "Implementation of the `#[marine_test]` macro"
documentation = "https://docs.rs/fluence/marine-test-macro-impl"

View File

@ -225,7 +225,7 @@ fn generate_module_ctors<'n>(
.map(|name| -> TResult<_> {
// TODO: optimize these two call because they are called twice for each module name
// and internally allocate memory in format call.
let module_name = marine_test::utils::generate_module_name(&name)?;
let module_name = marine_test::utils::generate_structs_module_ident(&name)?;
let struct_name = marine_test::utils::generate_struct_name(&name)?;
let name_for_user = marine_test::utils::new_ident(&name)?;

View File

@ -65,8 +65,9 @@ pub(super) fn generate_module_definitions<'i>(
fn generate_module_definition(module: &Module<'_>) -> TResult<TokenStream> {
let module_name = module.name;
let module_name_ident = utils::generate_module_name(module_name)?;
let struct_name_ident = utils::generate_struct_name(module_name)?;
let module_ident = utils::generate_module_ident(module_name)?;
let structs_module_ident = utils::generate_structs_module_ident(module_name)?;
let struct_ident = utils::generate_struct_name(module_name)?;
let module_interface = &module.interface;
let module_records = record_type_generator::generate_records(&module_interface.record_types)?;
@ -77,21 +78,27 @@ fn generate_module_definition(module: &Module<'_>) -> TResult<TokenStream> {
)?;
let module_definition = quote! {
pub mod #module_name_ident {
#(#module_records)*
// it's a sort of hack: this module structure allows user to import structs by
// use module_name_structs::StructName;
pub mod #structs_module_ident {
pub use #module_ident::*;
pub struct #struct_name_ident {
marine: std::rc::Rc<std::cell::RefCell<fluence_test::internal::AppService>>,
}
pub mod #module_ident {
#(#module_records)*
impl #struct_name_ident {
pub fn new(marine: std::rc::Rc<std::cell::RefCell<fluence_test::internal::AppService>>) -> Self {
Self { marine }
pub struct #struct_ident {
marine: std::rc::Rc<std::cell::RefCell<fluence_test::internal::AppService>>,
}
}
impl #struct_name_ident {
#(#module_functions)*
impl #struct_ident {
pub fn new(marine: std::rc::Rc<std::cell::RefCell<fluence_test::internal::AppService>>) -> Self {
Self { marine }
}
}
impl #struct_ident {
#(#module_functions)*
}
}
}
};

View File

@ -31,7 +31,7 @@ pub(super) fn generate_records(records: &MRecordTypes) -> TResult<Vec<TokenStrea
let fields = prepare_field(record.fields.deref().iter(), records)?;
let generated_record = quote! {
#[derive(Clone, fluence_test::internal::serde::Serialize, fluence_test::internal::serde::Deserialize)]
#[derive(Clone, Debug, fluence_test::internal::serde::Serialize, fluence_test::internal::serde::Deserialize)]
#[serde(crate = "fluence_test::internal::serde")]
pub struct #record_name_ident {
#(pub #fields),*

View File

@ -21,9 +21,14 @@ use marine_it_parser::interface::it::IType;
use proc_macro2::TokenStream;
use quote::quote;
pub(super) fn generate_module_name(module_name: &str) -> TResult<syn::Ident> {
let extended_module_name = format!("__m_generated_{}", module_name);
new_ident(&extended_module_name)
pub(super) fn generate_module_ident(module_name: &str) -> TResult<syn::Ident> {
let generated_module_name = format!("__m_generated_{}", module_name);
new_ident(&generated_module_name)
}
pub(super) fn generate_structs_module_ident(module_name: &str) -> TResult<syn::Ident> {
let generated_module_name = format!("{}_structs", module_name);
new_ident(&generated_module_name)
}
pub(super) fn generate_record_name(record_name: &str) -> TResult<syn::Ident> {

View File

@ -1,6 +1,6 @@
[package]
name = "marine-test-macro"
version = "0.1.8" # remember to update html_root_url
version = "0.1.9" # remember to update html_root_url
edition = "2018"
description = "Definition of the `#[marine_test]` macro"
documentation = "https://docs.rs/fluence/marine-test-macro"
@ -18,7 +18,7 @@ proc-macro = true
doctest = false
[dependencies]
marine-test-macro-impl = { path = "../marine-test-macro-impl", version = "0.1.8" }
marine-test-macro-impl = { path = "../marine-test-macro-impl", version = "=0.1.9" }
quote = "1.0.9"
proc-macro2 = "1.0.24"

View File

@ -1,6 +1,6 @@
[package]
name = "fluence-test"
version = "0.1.8" # remember to update html_root_url
version = "0.1.9" # remember to update html_root_url
description = "Fluence backend SDK for testing"
documentation = "https://docs.rs/fluence-test"
repository = "https://github.com/fluencelabs/rust-sdk/tree/master/fluence-test"
@ -18,7 +18,7 @@ path = "src/lib.rs"
doctest = false
[dependencies]
marine-test-macro = { path = "../crates/marine-test-macro", version = "0.1.8" }
marine-test-macro = { path = "../crates/marine-test-macro", version = "=0.1.9" }
fluence-app-service = { version = "0.7.0", features = ["raw-module-api"] }
serde = { version = "1.0.118", features = ["derive"] }