Skip to content

Commit

Permalink
refactor(rtl): Make sails-rtl crate self-sufficient (#251)
Browse files Browse the repository at this point in the history
  • Loading branch information
DennisInSky authored May 16, 2024
1 parent 4b236ec commit ab6a786
Show file tree
Hide file tree
Showing 34 changed files with 172 additions and 104 deletions.
9 changes: 2 additions & 7 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ members = [

[workspace.dependencies]
anyhow = "1"
const-format = { package = "const_format", version = "0.2" }
convert-case = { package = "convert_case", version = "0.6" }
futures = { version = "0.3", default-features = false }
gear-core-errors = "1.4.1"
Expand Down
1 change: 0 additions & 1 deletion examples/puppeteer/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2021"
gstd = { workspace = true, features = ["debug"] }
parity-scale-codec = { workspace = true, features = ["derive"] }
sails-rtl.workspace = true
sails-macros.workspace = true
scale-info = { workspace = true, features = ["derive"] }

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion examples/puppeteer/app/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,9 @@ pub mod puppet;
use core::marker::PhantomData;
use gstd::prelude::*;
use puppet::traits::ThisThatSvc;
use sails_macros::gservice;
use sails_rtl::{
calls::{Call, Remoting},
gstd::gservice,
ActorId,
};

Expand Down
1 change: 0 additions & 1 deletion examples/rmrk/catalog/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,5 @@ edition = "2021"

[dependencies]
parity-scale-codec = { workspace = true, features = ["derive"] }
sails-macros.workspace = true
sails-rtl.workspace = true
scale-info = { workspace = true, features = ["derive"] }
3 changes: 1 addition & 2 deletions examples/rmrk/catalog/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#![no_std]

use sails_macros::{gprogram, groute};
use sails_rtl::gstd::GStdExecContext;
use sails_rtl::gstd::{gprogram, groute, GStdExecContext};
use services::Catalog;

// Exposed publicly because of tests which use generated data
Expand Down
3 changes: 1 addition & 2 deletions examples/rmrk/catalog/app/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use errors::Error;
use parts::{CollectionId, Part, PartId, SlotPart};
use sails_macros::gservice;
use sails_rtl::{
collections::{BTreeMap, BTreeSet},
gstd::ExecContext,
gstd::{gservice, ExecContext},
prelude::*,
Result as RtlResult,
};
Expand Down
1 change: 0 additions & 1 deletion examples/rmrk/resource/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ edition = "2021"
[dependencies]
gstd.workspace = true
parity-scale-codec = { workspace = true, features = ["derive"] }
sails-macros.workspace = true
sails-rtl.workspace = true
scale-info = { workspace = true, features = ["derive"] }

Expand Down
3 changes: 1 addition & 2 deletions examples/rmrk/resource/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
#![no_std]

use sails_macros::{gprogram, groute};
use sails_rtl::gstd::{
calls::{GStdArgs, GStdRemoting},
events::GStdEventTrigger,
GStdExecContext,
gprogram, groute, GStdExecContext,
};
use services::{ResourceStorage, ResourceStorageEvent};

Expand Down
10 changes: 6 additions & 4 deletions examples/rmrk/resource/app/src/services/mod.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
use crate::catalogs::traits::RmrkCatalog;
use errors::{Error, Result};
use resources::{ComposedResource, PartId, Resource, ResourceId};
use sails_macros::gservice;
use sails_rtl::calls::Call;
use sails_rtl::gstd::calls::{GStdArgs, GStdRemoting};
use sails_rtl::{
calls::Call,
collections::HashMap,
gstd::{events::EventTrigger, ExecContext},
gstd::{
calls::{GStdArgs, GStdRemoting},
events::EventTrigger,
gservice, ExecContext,
},
prelude::*,
ActorId,
};
Expand Down
4 changes: 0 additions & 4 deletions examples/this-that-svc/app/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,4 @@ edition = "2021"

[dependencies]
gstd = { workspace = true, features = ["debug"] }
sails-macros.workspace = true
sails-rtl.workspace = true
parity-scale-codec = { workspace = true, features = ["derive"] }
primitive-types.workspace = true
scale-info = { workspace = true, features = ["derive"] }
9 changes: 7 additions & 2 deletions examples/this-that-svc/app/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
#![no_std]

use gstd::{debug, prelude::*};
use primitive_types::{H256, U256};
use sails_macros::gservice;
use sails_rtl::{gstd::gservice, H256, U256};

#[derive(Default)]
pub struct MyService;
Expand Down Expand Up @@ -47,16 +46,22 @@ impl MyService {

#[allow(dead_code)]
#[derive(Debug, Decode, TypeInfo)]
#[codec(crate = sails_rtl::scale_codec)]
#[scale_info(crate = sails_rtl::scale_info)]
pub struct TupleStruct(bool);

#[derive(Debug, Decode, TypeInfo)]
#[codec(crate = sails_rtl::scale_codec)]
#[scale_info(crate = sails_rtl::scale_info)]
pub struct DoThatParam {
pub p1: u32,
pub p2: String,
pub p3: ManyVariants,
}

#[derive(Debug, Decode, TypeInfo)]
#[codec(crate = sails_rtl::scale_codec)]
#[scale_info(crate = sails_rtl::scale_info)]
pub enum ManyVariants {
One,
Two(u32),
Expand Down
1 change: 1 addition & 0 deletions macros/core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition.workspace = true
license.workspace = true

[dependencies]
const-format.workspace = true
convert-case.workspace = true
parity-scale-codec.workspace = true
proc-macro-error.workspace = true
Expand Down
1 change: 1 addition & 0 deletions macros/core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,5 +24,6 @@ pub use service::gservice;

mod program;
mod route;
mod sails_paths;
mod service;
mod shared;
21 changes: 17 additions & 4 deletions macros/core/src/program.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
use crate::shared::{self, Func, ImplType};
use crate::{
sails_paths,
shared::{self, Func, ImplType},
};
use parity_scale_codec::Encode;
use proc_macro2::{Span, TokenStream as TokenStream2};
use proc_macro_error::abort;
Expand Down Expand Up @@ -82,6 +85,10 @@ pub fn gprogram(program_impl_tokens: TokenStream2) -> TokenStream2 {
quote!(#ctor_route(#ctor_params_struct_ident))
});

let scale_types_path = sails_paths::scale_types_path();
let scale_codec_path = sails_paths::scale_codec_path();
let scale_info_path = sails_paths::scale_info_path();

quote!(
#(#services_routes)*

Expand All @@ -99,15 +106,21 @@ pub fn gprogram(program_impl_tokens: TokenStream2) -> TokenStream2 {
}
}

use sails_rtl::prelude::Decode as __ProgramDecode;
use sails_rtl::prelude::TypeInfo as __ProgramTypeInfo;
use #scale_types_path ::Decode as __ProgramDecode;
use #scale_types_path ::TypeInfo as __ProgramTypeInfo;

#(#[derive(__ProgramDecode, __ProgramTypeInfo)] #ctors_params_structs )*
#(
#[derive(__ProgramDecode, __ProgramTypeInfo)]
#[codec(crate = #scale_codec_path )]
#[scale_info(crate = #scale_info_path )]
#ctors_params_structs
)*

mod meta {
use super::*;

#[derive(__ProgramTypeInfo)]
#[scale_info(crate = #scale_info_path )]
pub enum ConstructorsMeta {
#(#ctors_meta_variants),*
}
Expand Down
17 changes: 17 additions & 0 deletions macros/core/src/sails_paths.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use const_format::concatcp;

const SAILS: &str = "sails_rtl";
const SAILS_SCALE_CODEC: &str = concatcp!(SAILS, "::scale_codec");
const SAILS_SCALE_INFO: &str = concatcp!(SAILS, "::scale_info");

pub(crate) fn scale_types_path() -> syn::Path {
syn::parse_str(SAILS).unwrap()
}

pub(crate) fn scale_codec_path() -> syn::Path {
syn::parse_str(SAILS_SCALE_CODEC).unwrap()
}

pub(crate) fn scale_info_path() -> syn::Path {
syn::parse_str(SAILS_SCALE_INFO).unwrap()
}
41 changes: 30 additions & 11 deletions macros/core/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

//! Supporting functions and structures for the `gservice` macro.
use crate::shared::{self, Func, ImplType};
use crate::{
sails_paths,
shared::{self, Func, ImplType},
};
use convert_case::{Case, Casing};
use parity_scale_codec::Encode;
use proc_macro2::{Span, TokenStream as TokenStream2};
Expand Down Expand Up @@ -121,6 +124,10 @@ pub fn gservice(service_impl_tokens: TokenStream2) -> TokenStream2 {
let message_id_ident = Ident::new("message_id", Span::call_site());
let route_ident = Ident::new("route", Span::call_site());

let scale_types_path = sails_paths::scale_types_path();
let scale_codec_path = sails_paths::scale_codec_path();
let scale_info_path = sails_paths::scale_info_path();

quote!(
#service_impl

Expand Down Expand Up @@ -161,35 +168,47 @@ pub fn gservice(service_impl_tokens: TokenStream2) -> TokenStream2 {
}

impl #service_type_args sails_rtl::meta::ServiceMeta for #service_type_path #service_type_constraints {
fn commands() -> scale_info::MetaType {
scale_info::MetaType::new::<meta::CommandsMeta>()
fn commands() -> #scale_info_path ::MetaType {
#scale_info_path ::MetaType::new::<meta::CommandsMeta>()
}

fn queries() -> scale_info::MetaType {
scale_info::MetaType::new::<meta::QueriesMeta>()
fn queries() -> #scale_info_path ::MetaType {
#scale_info_path ::MetaType::new::<meta::QueriesMeta>()
}

fn events() -> scale_info::MetaType {
scale_info::MetaType::new::<meta::EventsMeta>()
fn events() -> #scale_info_path ::MetaType {
#scale_info_path ::MetaType::new::<meta::EventsMeta>()
}
}

#(#[derive(Decode, TypeInfo)] #invocation_params_structs)*
use #scale_types_path ::Decode as __ServiceDecode;
use #scale_types_path ::Encode as __ServiceEncode;
use #scale_types_path ::TypeInfo as __ServiceTypeInfo;

#(
#[derive(__ServiceDecode, __ServiceTypeInfo)]
#[codec(crate = #scale_codec_path )]
#[scale_info(crate = #scale_info_path )]
#invocation_params_structs
)*

mod meta {
use super::*;

#[derive(TypeInfo)]
#[derive(__ServiceTypeInfo)]
#[scale_info(crate = #scale_info_path )]
pub enum CommandsMeta {
#(#commands_meta_variants),*
}

#[derive(TypeInfo)]
#[derive(__ServiceTypeInfo)]
#[scale_info(crate = #scale_info_path )]
pub enum QueriesMeta {
#(#queries_meta_variants),*
}

#[derive(TypeInfo)]
#[derive(__ServiceTypeInfo)]
#[scale_info(crate = #scale_info_path )]
pub enum #no_events_type {}

pub type EventsMeta = #events_type;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,12 @@ impl sails_rtl::meta::ProgramMeta for MyProgram {
.into_iter()
}
}
use sails_rtl::prelude::Decode as __ProgramDecode;
use sails_rtl::prelude::TypeInfo as __ProgramTypeInfo;
use sails_rtl::Decode as __ProgramDecode;
use sails_rtl::TypeInfo as __ProgramTypeInfo;
mod meta {
use super::*;
#[derive(__ProgramTypeInfo)]
#[scale_info(crate = sails_rtl::scale_info)]
pub enum ConstructorsMeta {}
}
#[cfg(target_arch = "wasm32")]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,11 +38,12 @@ impl sails_rtl::meta::ProgramMeta for MyProgram {
[("Service", sails_rtl::meta::AnyServiceMeta::new::<MyService>())].into_iter()
}
}
use sails_rtl::prelude::Decode as __ProgramDecode;
use sails_rtl::prelude::TypeInfo as __ProgramTypeInfo;
use sails_rtl::Decode as __ProgramDecode;
use sails_rtl::TypeInfo as __ProgramTypeInfo;
mod meta {
use super::*;
#[derive(__ProgramTypeInfo)]
#[scale_info(crate = sails_rtl::scale_info)]
pub enum ConstructorsMeta {}
}
#[cfg(target_arch = "wasm32")]
Expand Down
Loading

0 comments on commit ab6a786

Please sign in to comment.