From caa2ad48b8a6e04b7f2128b1ba4daf2cf975f7f5 Mon Sep 17 00:00:00 2001 From: Timofey Date: Fri, 17 May 2024 15:08:41 +0300 Subject: [PATCH] feat: Allow gprogram without exposed services (#242) --- Cargo.lock | 17 +++++++++++++++++ Cargo.toml | 2 ++ examples/no-svcs-prog/README.md | 3 +++ examples/no-svcs-prog/app/Cargo.toml | 7 +++++++ examples/no-svcs-prog/app/src/lib.rs | 13 +++++++++++++ examples/no-svcs-prog/wasm/Cargo.toml | 13 +++++++++++++ examples/no-svcs-prog/wasm/build.rs | 15 +++++++++++++++ examples/no-svcs-prog/wasm/no-svcs-prog.idl | 4 ++++ examples/no-svcs-prog/wasm/src/lib.rs | 4 ++++ idl-gen/src/errors.rs | 2 -- idl-gen/src/meta.rs | 3 --- macros/core/src/program.rs | 6 +++--- ...multiple_services_with_non_empty_routes.snap | 6 +++--- ...for_single_service_with_non_empty_route.snap | 6 +++--- ...ogram_generates_init_for_multiple_ctors.snap | 6 +++--- ...am__gprogram_generates_init_for_no_ctor.snap | 6 +++--- ...gprogram_generates_init_for_single_ctor.snap | 6 +++--- 17 files changed, 96 insertions(+), 23 deletions(-) create mode 100644 examples/no-svcs-prog/README.md create mode 100644 examples/no-svcs-prog/app/Cargo.toml create mode 100644 examples/no-svcs-prog/app/src/lib.rs create mode 100644 examples/no-svcs-prog/wasm/Cargo.toml create mode 100644 examples/no-svcs-prog/wasm/build.rs create mode 100644 examples/no-svcs-prog/wasm/no-svcs-prog.idl create mode 100644 examples/no-svcs-prog/wasm/src/lib.rs diff --git a/Cargo.lock b/Cargo.lock index 34186cb6..f374905d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2901,6 +2901,23 @@ dependencies = [ "pin-utils", ] +[[package]] +name = "no-svcs-prog" +version = "0.1.0" +dependencies = [ + "gear-wasm-builder", + "no-svcs-prog-app", + "sails-idl-gen", + "sails-rtl", +] + +[[package]] +name = "no-svcs-prog-app" +version = "0.1.0" +dependencies = [ + "sails-rtl", +] + [[package]] name = "nodrop" version = "0.1.14" diff --git a/Cargo.toml b/Cargo.toml index b205c216..407eafbe 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,6 +8,8 @@ license = "GPL-3.0" resolver = "2" members = [ "client-gen", + "examples/no-svcs-prog/app", + "examples/no-svcs-prog/wasm", "examples/puppeteer/app", "examples/puppeteer/wasm", "examples/rmrk/catalog/app", diff --git a/examples/no-svcs-prog/README.md b/examples/no-svcs-prog/README.md new file mode 100644 index 00000000..0e7694d1 --- /dev/null +++ b/examples/no-svcs-prog/README.md @@ -0,0 +1,3 @@ +# no-svcs-prog + +Most basic program without services to get you started diff --git a/examples/no-svcs-prog/app/Cargo.toml b/examples/no-svcs-prog/app/Cargo.toml new file mode 100644 index 00000000..30705463 --- /dev/null +++ b/examples/no-svcs-prog/app/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "no-svcs-prog-app" +version = "0.1.0" +edition = "2021" + +[dependencies] +sails-rtl.workspace = true diff --git a/examples/no-svcs-prog/app/src/lib.rs b/examples/no-svcs-prog/app/src/lib.rs new file mode 100644 index 00000000..b697a0d2 --- /dev/null +++ b/examples/no-svcs-prog/app/src/lib.rs @@ -0,0 +1,13 @@ +#![no_std] + +use sails_rtl::gstd::gprogram; + +#[derive(Default)] +pub struct Program; + +#[gprogram] +impl Program { + pub fn new() -> Self { + Self + } +} diff --git a/examples/no-svcs-prog/wasm/Cargo.toml b/examples/no-svcs-prog/wasm/Cargo.toml new file mode 100644 index 00000000..45cee713 --- /dev/null +++ b/examples/no-svcs-prog/wasm/Cargo.toml @@ -0,0 +1,13 @@ +[package] +name = "no-svcs-prog" +version = "0.1.0" +edition = "2021" + +[dependencies] +no-svcs-prog-app = { path = "../app" } +sails-rtl.workspace = true + +[build-dependencies] +gwasm-builder.workspace = true +no-svcs-prog-app = { path = "../app" } +sails-idl-gen.workspace = true diff --git a/examples/no-svcs-prog/wasm/build.rs b/examples/no-svcs-prog/wasm/build.rs new file mode 100644 index 00000000..d13f9a15 --- /dev/null +++ b/examples/no-svcs-prog/wasm/build.rs @@ -0,0 +1,15 @@ +use no_svcs_prog_app::Program; +use sails_idl_gen::program; +use std::{env, fs::File, path::PathBuf}; + +fn main() { + gwasm_builder::build(); + + let manifest_dir_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()); + + let idl_file_path = manifest_dir_path.join("no-svcs-prog.idl"); + + let idl_file = File::create(idl_file_path).unwrap(); + + program::generate_idl::(idl_file).unwrap(); +} diff --git a/examples/no-svcs-prog/wasm/no-svcs-prog.idl b/examples/no-svcs-prog/wasm/no-svcs-prog.idl new file mode 100644 index 00000000..9851675d --- /dev/null +++ b/examples/no-svcs-prog/wasm/no-svcs-prog.idl @@ -0,0 +1,4 @@ +constructor { + New : (); +}; + diff --git a/examples/no-svcs-prog/wasm/src/lib.rs b/examples/no-svcs-prog/wasm/src/lib.rs new file mode 100644 index 00000000..8d55c375 --- /dev/null +++ b/examples/no-svcs-prog/wasm/src/lib.rs @@ -0,0 +1,4 @@ +#![no_std] + +#[cfg(target_arch = "wasm32")] +pub use no_svcs_prog_app::wasm::*; diff --git a/idl-gen/src/errors.rs b/idl-gen/src/errors.rs index 494241af..f7041c53 100644 --- a/idl-gen/src/errors.rs +++ b/idl-gen/src/errors.rs @@ -26,8 +26,6 @@ pub enum Error { FuncMetaIsInvalid(String), #[error("event meta is invalid: {0}")] EventMetaIsInvalid(String), - #[error("at least one service is required")] - ServiceIsMissing, #[error("type id `{0}` is not found in the type registry")] TypeIdIsUnknown(u32), #[error("type `{0}` is not supported")] diff --git a/idl-gen/src/meta.rs b/idl-gen/src/meta.rs index 77b34c2e..6b849fcf 100644 --- a/idl-gen/src/meta.rs +++ b/idl-gen/src/meta.rs @@ -67,9 +67,6 @@ impl ExpandedProgramMeta { ) }) .collect::>(); - if services_data.is_empty() { - return Err(Error::ServiceIsMissing); - } let registry = PortableRegistry::from(registry); let ctors = Self::ctor_funcs(®istry, ctors_type_id)?; let services = services_data diff --git a/macros/core/src/program.rs b/macros/core/src/program.rs index 151491b7..edc40707 100644 --- a/macros/core/src/program.rs +++ b/macros/core/src/program.rs @@ -95,8 +95,8 @@ pub fn gprogram(program_impl_tokens: TokenStream2) -> TokenStream2 { #program_impl impl #program_type_args sails_rtl::meta::ProgramMeta for #program_type_path #program_type_constraints { - fn constructors() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn constructors() -> #scale_info_path::MetaType { + #scale_info_path::MetaType::new::() } fn services() -> impl Iterator { @@ -306,7 +306,7 @@ fn generate_handle<'a>( #[gstd::async_main] async fn main() { let mut #input_ident: &[u8] = &gstd::msg::load_bytes().expect("Failed to read input"); - let output = #(#invocation_dispatches)else*; + let output: Vec = #(#invocation_dispatches)else*; gstd::msg::reply_bytes(output, 0).expect("Failed to send output"); } ) diff --git a/macros/core/tests/snapshots/program__gprogram_generates_handle_for_multiple_services_with_non_empty_routes.snap b/macros/core/tests/snapshots/program__gprogram_generates_handle_for_multiple_services_with_non_empty_routes.snap index f0eebc56..a6860a35 100644 --- a/macros/core/tests/snapshots/program__gprogram_generates_handle_for_multiple_services_with_non_empty_routes.snap +++ b/macros/core/tests/snapshots/program__gprogram_generates_handle_for_multiple_services_with_non_empty_routes.snap @@ -46,8 +46,8 @@ impl MyProgram { } } impl sails_rtl::meta::ProgramMeta for MyProgram { - fn constructors() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn constructors() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } fn services() -> impl Iterator< Item = (&'static str, sails_rtl::meta::AnyServiceMeta), @@ -99,7 +99,7 @@ pub mod wasm { #[gstd::async_main] async fn main() { let mut input: &[u8] = &gstd::msg::load_bytes().expect("Failed to read input"); - let output = if input.starts_with(&__ROUTE_SERVICE2) { + let output: Vec = if input.starts_with(&__ROUTE_SERVICE2) { let program_ref = unsafe { PROGRAM.as_ref() } .expect("Program not initialized"); let mut service = program_ref.service2(); diff --git a/macros/core/tests/snapshots/program__gprogram_generates_handle_for_single_service_with_non_empty_route.snap b/macros/core/tests/snapshots/program__gprogram_generates_handle_for_single_service_with_non_empty_route.snap index 8f44bca9..3f3396b8 100644 --- a/macros/core/tests/snapshots/program__gprogram_generates_handle_for_single_service_with_non_empty_route.snap +++ b/macros/core/tests/snapshots/program__gprogram_generates_handle_for_single_service_with_non_empty_route.snap @@ -29,8 +29,8 @@ impl MyProgram { } } impl sails_rtl::meta::ProgramMeta for MyProgram { - fn constructors() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn constructors() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } fn services() -> impl Iterator< Item = (&'static str, sails_rtl::meta::AnyServiceMeta), @@ -78,7 +78,7 @@ pub mod wasm { #[gstd::async_main] async fn main() { let mut input: &[u8] = &gstd::msg::load_bytes().expect("Failed to read input"); - let output = if input.starts_with(&__ROUTE_SERVICE) { + let output: Vec = if input.starts_with(&__ROUTE_SERVICE) { let program_ref = unsafe { PROGRAM.as_ref() } .expect("Program not initialized"); let mut service = program_ref.service(); diff --git a/macros/core/tests/snapshots/program__gprogram_generates_init_for_multiple_ctors.snap b/macros/core/tests/snapshots/program__gprogram_generates_init_for_multiple_ctors.snap index 9e04f5f7..41ed8c1b 100644 --- a/macros/core/tests/snapshots/program__gprogram_generates_init_for_multiple_ctors.snap +++ b/macros/core/tests/snapshots/program__gprogram_generates_init_for_multiple_ctors.snap @@ -11,8 +11,8 @@ impl MyProgram { } } impl sails_rtl::meta::ProgramMeta for MyProgram { - fn constructors() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn constructors() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } fn services() -> impl Iterator< Item = (&'static str, sails_rtl::meta::AnyServiceMeta), @@ -89,7 +89,7 @@ pub mod wasm { #[gstd::async_main] async fn main() { let mut input: &[u8] = &gstd::msg::load_bytes().expect("Failed to read input"); - let output = { + let output: Vec = { let input = String::decode(&mut input) .unwrap_or_else(|_| { if input.len() <= 8 { diff --git a/macros/core/tests/snapshots/program__gprogram_generates_init_for_no_ctor.snap b/macros/core/tests/snapshots/program__gprogram_generates_init_for_no_ctor.snap index 40758e9e..46c4dcc5 100644 --- a/macros/core/tests/snapshots/program__gprogram_generates_init_for_no_ctor.snap +++ b/macros/core/tests/snapshots/program__gprogram_generates_init_for_no_ctor.snap @@ -4,8 +4,8 @@ expression: result --- impl MyProgram {} impl sails_rtl::meta::ProgramMeta for MyProgram { - fn constructors() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn constructors() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } fn services() -> impl Iterator< Item = (&'static str, sails_rtl::meta::AnyServiceMeta), @@ -53,7 +53,7 @@ pub mod wasm { #[gstd::async_main] async fn main() { let mut input: &[u8] = &gstd::msg::load_bytes().expect("Failed to read input"); - let output = { + let output: Vec = { let input = String::decode(&mut input) .unwrap_or_else(|_| { if input.len() <= 8 { diff --git a/macros/core/tests/snapshots/program__gprogram_generates_init_for_single_ctor.snap b/macros/core/tests/snapshots/program__gprogram_generates_init_for_single_ctor.snap index ac6122f7..23383a27 100644 --- a/macros/core/tests/snapshots/program__gprogram_generates_init_for_single_ctor.snap +++ b/macros/core/tests/snapshots/program__gprogram_generates_init_for_single_ctor.snap @@ -8,8 +8,8 @@ impl MyProgram { } } impl sails_rtl::meta::ProgramMeta for MyProgram { - fn constructors() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn constructors() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } fn services() -> impl Iterator< Item = (&'static str, sails_rtl::meta::AnyServiceMeta), @@ -72,7 +72,7 @@ pub mod wasm { #[gstd::async_main] async fn main() { let mut input: &[u8] = &gstd::msg::load_bytes().expect("Failed to read input"); - let output = { + let output: Vec = { let input = String::decode(&mut input) .unwrap_or_else(|_| { if input.len() <= 8 {