Skip to content

Commit

Permalink
feat: Allow gprogram without exposed services (#242)
Browse files Browse the repository at this point in the history
  • Loading branch information
holykol authored May 17, 2024
1 parent cd3d7bf commit caa2ad4
Show file tree
Hide file tree
Showing 17 changed files with 96 additions and 23 deletions.
17 changes: 17 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
3 changes: 3 additions & 0 deletions examples/no-svcs-prog/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# no-svcs-prog

Most basic program without services to get you started
7 changes: 7 additions & 0 deletions examples/no-svcs-prog/app/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[package]
name = "no-svcs-prog-app"
version = "0.1.0"
edition = "2021"

[dependencies]
sails-rtl.workspace = true
13 changes: 13 additions & 0 deletions examples/no-svcs-prog/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#![no_std]

use sails_rtl::gstd::gprogram;

#[derive(Default)]
pub struct Program;

#[gprogram]
impl Program {
pub fn new() -> Self {
Self
}
}
13 changes: 13 additions & 0 deletions examples/no-svcs-prog/wasm/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
15 changes: 15 additions & 0 deletions examples/no-svcs-prog/wasm/build.rs
Original file line number Diff line number Diff line change
@@ -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::<Program>(idl_file).unwrap();
}
4 changes: 4 additions & 0 deletions examples/no-svcs-prog/wasm/no-svcs-prog.idl
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
constructor {
New : ();
};

4 changes: 4 additions & 0 deletions examples/no-svcs-prog/wasm/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
#![no_std]

#[cfg(target_arch = "wasm32")]
pub use no_svcs_prog_app::wasm::*;
2 changes: 0 additions & 2 deletions idl-gen/src/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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")]
Expand Down
3 changes: 0 additions & 3 deletions idl-gen/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,6 @@ impl ExpandedProgramMeta {
)
})
.collect::<Vec<_>>();
if services_data.is_empty() {
return Err(Error::ServiceIsMissing);
}
let registry = PortableRegistry::from(registry);
let ctors = Self::ctor_funcs(&registry, ctors_type_id)?;
let services = services_data
Expand Down
6 changes: 3 additions & 3 deletions macros/core/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<meta::ConstructorsMeta>()
fn constructors() -> #scale_info_path::MetaType {
#scale_info_path::MetaType::new::<meta::ConstructorsMeta>()
}

fn services() -> impl Iterator<Item = (&'static str, sails_rtl::meta::AnyServiceMeta)> {
Expand Down Expand Up @@ -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<u8> = #(#invocation_dispatches)else*;
gstd::msg::reply_bytes(output, 0).expect("Failed to send output");
}
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ impl MyProgram {
}
}
impl sails_rtl::meta::ProgramMeta for MyProgram {
fn constructors() -> scale_info::MetaType {
scale_info::MetaType::new::<meta::ConstructorsMeta>()
fn constructors() -> sails_rtl::scale_info::MetaType {
sails_rtl::scale_info::MetaType::new::<meta::ConstructorsMeta>()
}
fn services() -> impl Iterator<
Item = (&'static str, sails_rtl::meta::AnyServiceMeta),
Expand Down Expand Up @@ -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<u8> = if input.starts_with(&__ROUTE_SERVICE2) {
let program_ref = unsafe { PROGRAM.as_ref() }
.expect("Program not initialized");
let mut service = program_ref.service2();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ impl MyProgram {
}
}
impl sails_rtl::meta::ProgramMeta for MyProgram {
fn constructors() -> scale_info::MetaType {
scale_info::MetaType::new::<meta::ConstructorsMeta>()
fn constructors() -> sails_rtl::scale_info::MetaType {
sails_rtl::scale_info::MetaType::new::<meta::ConstructorsMeta>()
}
fn services() -> impl Iterator<
Item = (&'static str, sails_rtl::meta::AnyServiceMeta),
Expand Down Expand Up @@ -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<u8> = if input.starts_with(&__ROUTE_SERVICE) {
let program_ref = unsafe { PROGRAM.as_ref() }
.expect("Program not initialized");
let mut service = program_ref.service();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ impl MyProgram {
}
}
impl sails_rtl::meta::ProgramMeta for MyProgram {
fn constructors() -> scale_info::MetaType {
scale_info::MetaType::new::<meta::ConstructorsMeta>()
fn constructors() -> sails_rtl::scale_info::MetaType {
sails_rtl::scale_info::MetaType::new::<meta::ConstructorsMeta>()
}
fn services() -> impl Iterator<
Item = (&'static str, sails_rtl::meta::AnyServiceMeta),
Expand Down Expand Up @@ -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<u8> = {
let input = String::decode(&mut input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ expression: result
---
impl MyProgram {}
impl sails_rtl::meta::ProgramMeta for MyProgram {
fn constructors() -> scale_info::MetaType {
scale_info::MetaType::new::<meta::ConstructorsMeta>()
fn constructors() -> sails_rtl::scale_info::MetaType {
sails_rtl::scale_info::MetaType::new::<meta::ConstructorsMeta>()
}
fn services() -> impl Iterator<
Item = (&'static str, sails_rtl::meta::AnyServiceMeta),
Expand Down Expand Up @@ -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<u8> = {
let input = String::decode(&mut input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ impl MyProgram {
}
}
impl sails_rtl::meta::ProgramMeta for MyProgram {
fn constructors() -> scale_info::MetaType {
scale_info::MetaType::new::<meta::ConstructorsMeta>()
fn constructors() -> sails_rtl::scale_info::MetaType {
sails_rtl::scale_info::MetaType::new::<meta::ConstructorsMeta>()
}
fn services() -> impl Iterator<
Item = (&'static str, sails_rtl::meta::AnyServiceMeta),
Expand Down Expand Up @@ -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<u8> = {
let input = String::decode(&mut input)
.unwrap_or_else(|_| {
if input.len() <= 8 {
Expand Down

0 comments on commit caa2ad4

Please sign in to comment.