From 4b236ecccf3dff2cdee45c4cf5eab653f481a7f6 Mon Sep 17 00:00:00 2001 From: Timofey Date: Thu, 16 May 2024 14:41:06 +0300 Subject: [PATCH 1/2] feat(idl-gen): Make snapshot tests integrational (#239) --- client-gen/src/generator.rs | 59 ------------ client-gen/src/lib.rs | 51 +--------- client-gen/tests/generator.rs | 93 +++++++++++++++++++ .../snapshots/generator__basic_works.snap} | 5 +- .../snapshots/generator__full.snap} | 5 +- .../generator__multiple_services.snap} | 5 +- .../snapshots/generator__rmrk_works.snap} | 5 +- 7 files changed, 102 insertions(+), 121 deletions(-) create mode 100644 client-gen/tests/generator.rs rename client-gen/{src/snapshots/sails_client_gen__generator__tests__basic_works.snap => tests/snapshots/generator__basic_works.snap} (96%) rename client-gen/{src/snapshots/sails_client_gen__tests__full.snap => tests/snapshots/generator__full.snap} (98%) rename client-gen/{src/snapshots/sails_client_gen__generator__tests__multiple_services.snap => tests/snapshots/generator__multiple_services.snap} (97%) rename client-gen/{src/snapshots/sails_client_gen__generator__tests__rmrk_works.snap => tests/snapshots/generator__rmrk_works.snap} (99%) diff --git a/client-gen/src/generator.rs b/client-gen/src/generator.rs index a7b0dedd..36a3c345 100644 --- a/client-gen/src/generator.rs +++ b/client-gen/src/generator.rs @@ -718,62 +718,3 @@ impl<'ast> Visitor<'ast> for CallBuilderGenerator { self.code.push_str(") -> Vec"); } } -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn test_basic_works() { - let idl = r" - type MyParam = struct { - f1: u32, - f2: vec str, - f3: opt struct { u8, u32 }, - }; - - type MyParam2 = enum { - Variant1, - Variant2: u32, - Variant3: struct { u32 }, - Variant4: struct { u8, u32 }, - Variant5: struct { f1: str, f2: vec u8 }, - }; - - service { - DoThis: (p1: u32, p2: MyParam) -> u16; - DoThat: (p1: struct { u8, u32 }) -> u8; - }; - "; - - let program = sails_idl_parser::ast::parse_idl(idl).expect("parse IDL"); - - insta::assert_snapshot!(generate(program, "Basic").unwrap()); - } - - #[test] - fn test_multiple_services() { - let idl = r" - service { - DoThis: (p1: u32, p2: MyParam) -> u16; - DoThat: (p1: struct { u8, u32 }) -> u8; - }; - - service Named { - query That: (p1: u32) -> str; - }; - "; - - let program = sails_idl_parser::ast::parse_idl(idl).expect("parse IDL"); - - insta::assert_snapshot!(generate(program, "Multiple").unwrap()); - } - - #[test] - fn test_rmrk_works() { - let idl = include_str!("../../examples/rmrk/catalog/wasm/rmrk-catalog.idl"); - - let program = sails_idl_parser::ast::parse_idl(idl).expect("parse IDL"); - - insta::assert_snapshot!(generate(program, "RmrkCatalog").unwrap()); - } -} diff --git a/client-gen/src/lib.rs b/client-gen/src/lib.rs index 1c5966a8..24158556 100644 --- a/client-gen/src/lib.rs +++ b/client-gen/src/lib.rs @@ -1,4 +1,4 @@ -mod generator; +pub mod generator; use anyhow::{Context, Result}; use convert_case::{Case, Casing}; @@ -32,52 +32,3 @@ pub fn generate_client_from_idl( Ok(()) } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn full() { - const IDL: &str = r#" - type ThisThatSvcAppTupleStruct = struct { - bool, - }; - - type ThisThatSvcAppDoThatParam = struct { - p1: u32, - p2: str, - p3: ThisThatSvcAppManyVariants, - }; - - type ThisThatSvcAppManyVariants = enum { - One, - Two: u32, - Three: opt u32, - Four: struct { a: u32, b: opt u16 }, - Five: struct { str, u32 }, - Six: struct { u32 }, - }; - - type T = enum { One }; - - constructor { - New : (a: u32); - }; - - service { - DoThis : (p1: u32, p2: str, p3: struct { opt str, u8 }, p4: ThisThatSvcAppTupleStruct) -> struct { str, u32 }; - DoThat : (param: ThisThatSvcAppDoThatParam) -> result (struct { str, u32 }, struct { str }); - query This : (v1: vec u16) -> u32; - query That : (v1: null) -> result (str, str); - }; - "#; - let program = sails_idl_parser::ast::parse_idl(IDL).expect("parse IDL"); - - let generated = generator::generate(program, "Service").unwrap(); - - dbg!(&generated); - - insta::assert_snapshot!(generated); - } -} diff --git a/client-gen/tests/generator.rs b/client-gen/tests/generator.rs new file mode 100644 index 00000000..571829bc --- /dev/null +++ b/client-gen/tests/generator.rs @@ -0,0 +1,93 @@ +#[test] +fn full() { + const IDL: &str = r#" + type ThisThatSvcAppTupleStruct = struct { + bool, + }; + + type ThisThatSvcAppDoThatParam = struct { + p1: u32, + p2: str, + p3: ThisThatSvcAppManyVariants, + }; + + type ThisThatSvcAppManyVariants = enum { + One, + Two: u32, + Three: opt u32, + Four: struct { a: u32, b: opt u16 }, + Five: struct { str, u32 }, + Six: struct { u32 }, + }; + + type T = enum { One }; + + constructor { + New : (a: u32); + }; + + service { + DoThis : (p1: u32, p2: str, p3: struct { opt str, u8 }, p4: ThisThatSvcAppTupleStruct) -> struct { str, u32 }; + DoThat : (param: ThisThatSvcAppDoThatParam) -> result (struct { str, u32 }, struct { str }); + query This : (v1: vec u16) -> u32; + query That : (v1: null) -> result (str, str); + }; + "#; + + insta::assert_snapshot!(gen(IDL, "Service")); +} + +#[test] +fn test_basic_works() { + let idl = r" + type MyParam = struct { + f1: u32, + f2: vec str, + f3: opt struct { u8, u32 }, + }; + + type MyParam2 = enum { + Variant1, + Variant2: u32, + Variant3: struct { u32 }, + Variant4: struct { u8, u32 }, + Variant5: struct { f1: str, f2: vec u8 }, + }; + + service { + DoThis: (p1: u32, p2: MyParam) -> u16; + DoThat: (p1: struct { u8, u32 }) -> u8; + }; + "; + + insta::assert_snapshot!(gen(idl, "Basic")); +} + +#[test] +fn test_multiple_services() { + let idl = r" + service { + DoThis: (p1: u32, p2: MyParam) -> u16; + DoThat: (p1: struct { u8, u32 }) -> u8; + }; + + service Named { + query That: (p1: u32) -> str; + }; + "; + + insta::assert_snapshot!(gen(idl, "Multiple")); +} + +#[test] +fn test_rmrk_works() { + let idl = include_str!("../../examples/rmrk/catalog/wasm/rmrk-catalog.idl"); + + insta::assert_snapshot!(gen(idl, "RmrkCatalog")); +} + +fn gen(program: &str, service_name: &str) -> String { + let program = sails_idl_parser::ast::parse_idl(program).expect("parse IDL"); + + sails_client_gen::generator::generate(program, service_name).expect("generate client") +} diff --git a/client-gen/src/snapshots/sails_client_gen__generator__tests__basic_works.snap b/client-gen/tests/snapshots/generator__basic_works.snap similarity index 96% rename from client-gen/src/snapshots/sails_client_gen__generator__tests__basic_works.snap rename to client-gen/tests/snapshots/generator__basic_works.snap index fdf7dfea..ef741c9e 100644 --- a/client-gen/src/snapshots/sails_client_gen__generator__tests__basic_works.snap +++ b/client-gen/tests/snapshots/generator__basic_works.snap @@ -1,6 +1,6 @@ --- -source: client-gen/src/generator.rs -expression: "generate(program, \"Basic\").unwrap()" +source: client-gen/tests/generator.rs +expression: "gen(idl, \"Basic\")" --- // Code generated by sails-client-gen. DO NOT EDIT. use core::marker::PhantomData; @@ -96,4 +96,3 @@ pub mod traits { fn do_that(&mut self, p1: (u8, u32)) -> impl Call; } } - diff --git a/client-gen/src/snapshots/sails_client_gen__tests__full.snap b/client-gen/tests/snapshots/generator__full.snap similarity index 98% rename from client-gen/src/snapshots/sails_client_gen__tests__full.snap rename to client-gen/tests/snapshots/generator__full.snap index 7f84e33a..e244ec7a 100644 --- a/client-gen/src/snapshots/sails_client_gen__tests__full.snap +++ b/client-gen/tests/snapshots/generator__full.snap @@ -1,6 +1,6 @@ --- -source: client-gen/src/lib.rs -expression: generated +source: client-gen/tests/generator.rs +expression: "gen(IDL, \"Service\")" --- // Code generated by sails-client-gen. DO NOT EDIT. use core::marker::PhantomData; @@ -175,4 +175,3 @@ pub mod traits { fn that(&self, v1: ()) -> impl Call>; } } - diff --git a/client-gen/src/snapshots/sails_client_gen__generator__tests__multiple_services.snap b/client-gen/tests/snapshots/generator__multiple_services.snap similarity index 97% rename from client-gen/src/snapshots/sails_client_gen__generator__tests__multiple_services.snap rename to client-gen/tests/snapshots/generator__multiple_services.snap index 4ec5392c..dbded327 100644 --- a/client-gen/src/snapshots/sails_client_gen__generator__tests__multiple_services.snap +++ b/client-gen/tests/snapshots/generator__multiple_services.snap @@ -1,6 +1,6 @@ --- -source: client-gen/src/generator.rs -expression: "generate(program, \"Multiple\").unwrap()" +source: client-gen/tests/generator.rs +expression: "gen(idl, \"Multiple\")" --- // Code generated by sails-client-gen. DO NOT EDIT. use core::marker::PhantomData; @@ -134,4 +134,3 @@ pub mod traits { fn that(&self, p1: u32) -> impl Call; } } - diff --git a/client-gen/src/snapshots/sails_client_gen__generator__tests__rmrk_works.snap b/client-gen/tests/snapshots/generator__rmrk_works.snap similarity index 99% rename from client-gen/src/snapshots/sails_client_gen__generator__tests__rmrk_works.snap rename to client-gen/tests/snapshots/generator__rmrk_works.snap index 22e26761..e1af3380 100644 --- a/client-gen/src/snapshots/sails_client_gen__generator__tests__rmrk_works.snap +++ b/client-gen/tests/snapshots/generator__rmrk_works.snap @@ -1,6 +1,6 @@ --- -source: client-gen/src/generator.rs -expression: "generate(program, \"RmrkCatalog\").unwrap()" +source: client-gen/tests/generator.rs +expression: "gen(idl, \"RmrkCatalog\")" --- // Code generated by sails-client-gen. DO NOT EDIT. use core::marker::PhantomData; @@ -305,4 +305,3 @@ pub mod traits { fn part(&self, part_id: u32) -> impl Call>; } } - From ab6a7868fd0ad48033a80a977716f13329d7c142 Mon Sep 17 00:00:00 2001 From: Dennis Diatlov Date: Thu, 16 May 2024 13:03:45 +0100 Subject: [PATCH 2/2] refactor(rtl): Make sails-rtl crate self-sufficient (#251) --- Cargo.lock | 9 +--- Cargo.toml | 1 + examples/puppeteer/app/Cargo.toml | 1 - examples/puppeteer/app/src/lib.rs | 2 +- examples/rmrk/catalog/app/Cargo.toml | 1 - examples/rmrk/catalog/app/src/lib.rs | 3 +- examples/rmrk/catalog/app/src/services/mod.rs | 3 +- examples/rmrk/resource/app/Cargo.toml | 1 - examples/rmrk/resource/app/src/lib.rs | 3 +- .../rmrk/resource/app/src/services/mod.rs | 10 +++-- examples/this-that-svc/app/Cargo.toml | 4 -- examples/this-that-svc/app/src/lib.rs | 9 +++- macros/core/Cargo.toml | 1 + macros/core/src/lib.rs | 1 + macros/core/src/program.rs | 21 ++++++++-- macros/core/src/sails_paths.rs | 17 ++++++++ macros/core/src/service.rs | 41 ++++++++++++++----- ...ltiple_services_with_non_empty_routes.snap | 5 ++- ...r_single_service_with_non_empty_route.snap | 5 ++- ...ram_generates_init_for_multiple_ctors.snap | 10 +++-- ...__gprogram_generates_init_for_no_ctor.snap | 6 +-- ...rogram_generates_init_for_single_ctor.snap | 8 ++-- .../snapshots/service__gservice_works.snap | 32 ++++++++++----- ...vice_works_for_lifetimes_and_generics.snap | 28 ++++++++----- .../gservice_fails_route_fn_must_be_public.rs | 4 +- ...rvice_fails_route_fn_must_be_public.stderr | 4 +- macros/tests/ui/gservice_works.rs | 9 ++-- ...rvice_works_for_lifecycles_and_generics.rs | 8 ++-- rtl/Cargo.toml | 1 + rtl/src/calls.rs | 8 +++- rtl/src/gstd/mod.rs | 1 + rtl/src/lib.rs | 12 +----- rtl/src/prelude.rs | 4 +- rtl/src/types.rs | 3 +- 34 files changed, 172 insertions(+), 104 deletions(-) create mode 100644 macros/core/src/sails_paths.rs diff --git a/Cargo.lock b/Cargo.lock index f37bf88a..34186cb6 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3402,7 +3402,6 @@ dependencies = [ "gstd", "parity-scale-codec", "sails-client-gen", - "sails-macros", "sails-rtl", "scale-info", ] @@ -3693,7 +3692,6 @@ name = "rmrk-catalog-app" version = "0.1.0" dependencies = [ "parity-scale-codec", - "sails-macros", "sails-rtl", "scale-info", ] @@ -3719,7 +3717,6 @@ dependencies = [ "gstd", "parity-scale-codec", "sails-client-gen", - "sails-macros", "sails-rtl", "scale-info", ] @@ -3844,6 +3841,7 @@ dependencies = [ name = "sails-macros-core" version = "0.0.1" dependencies = [ + "const_format", "convert_case 0.6.0", "insta", "parity-scale-codec", @@ -3866,6 +3864,7 @@ dependencies = [ "hex", "parity-scale-codec", "primitive-types", + "sails-macros", "scale-info", "thiserror-no-std", ] @@ -4807,11 +4806,7 @@ name = "this-that-svc-app" version = "0.1.0" dependencies = [ "gstd", - "parity-scale-codec", - "primitive-types", - "sails-macros", "sails-rtl", - "scale-info", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index 2b7ed6b0..b205c216 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/examples/puppeteer/app/Cargo.toml b/examples/puppeteer/app/Cargo.toml index 0726eeec..e0509d99 100644 --- a/examples/puppeteer/app/Cargo.toml +++ b/examples/puppeteer/app/Cargo.toml @@ -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] diff --git a/examples/puppeteer/app/src/lib.rs b/examples/puppeteer/app/src/lib.rs index 64ca619c..b4e61141 100644 --- a/examples/puppeteer/app/src/lib.rs +++ b/examples/puppeteer/app/src/lib.rs @@ -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, }; diff --git a/examples/rmrk/catalog/app/Cargo.toml b/examples/rmrk/catalog/app/Cargo.toml index 2eb6a15b..f292946f 100644 --- a/examples/rmrk/catalog/app/Cargo.toml +++ b/examples/rmrk/catalog/app/Cargo.toml @@ -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"] } diff --git a/examples/rmrk/catalog/app/src/lib.rs b/examples/rmrk/catalog/app/src/lib.rs index 27b47e27..9d1995df 100644 --- a/examples/rmrk/catalog/app/src/lib.rs +++ b/examples/rmrk/catalog/app/src/lib.rs @@ -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 diff --git a/examples/rmrk/catalog/app/src/services/mod.rs b/examples/rmrk/catalog/app/src/services/mod.rs index a88ee71c..cac83a67 100644 --- a/examples/rmrk/catalog/app/src/services/mod.rs +++ b/examples/rmrk/catalog/app/src/services/mod.rs @@ -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, }; diff --git a/examples/rmrk/resource/app/Cargo.toml b/examples/rmrk/resource/app/Cargo.toml index a803e808..a8dae6b9 100644 --- a/examples/rmrk/resource/app/Cargo.toml +++ b/examples/rmrk/resource/app/Cargo.toml @@ -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"] } diff --git a/examples/rmrk/resource/app/src/lib.rs b/examples/rmrk/resource/app/src/lib.rs index 7267f55c..b6b92143 100644 --- a/examples/rmrk/resource/app/src/lib.rs +++ b/examples/rmrk/resource/app/src/lib.rs @@ -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}; diff --git a/examples/rmrk/resource/app/src/services/mod.rs b/examples/rmrk/resource/app/src/services/mod.rs index bab9555e..d89b8047 100644 --- a/examples/rmrk/resource/app/src/services/mod.rs +++ b/examples/rmrk/resource/app/src/services/mod.rs @@ -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, }; diff --git a/examples/this-that-svc/app/Cargo.toml b/examples/this-that-svc/app/Cargo.toml index 39f06d22..77218190 100644 --- a/examples/this-that-svc/app/Cargo.toml +++ b/examples/this-that-svc/app/Cargo.toml @@ -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"] } diff --git a/examples/this-that-svc/app/src/lib.rs b/examples/this-that-svc/app/src/lib.rs index ebea21e2..1b515442 100644 --- a/examples/this-that-svc/app/src/lib.rs +++ b/examples/this-that-svc/app/src/lib.rs @@ -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; @@ -47,9 +46,13 @@ 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, @@ -57,6 +60,8 @@ pub struct DoThatParam { } #[derive(Debug, Decode, TypeInfo)] +#[codec(crate = sails_rtl::scale_codec)] +#[scale_info(crate = sails_rtl::scale_info)] pub enum ManyVariants { One, Two(u32), diff --git a/macros/core/Cargo.toml b/macros/core/Cargo.toml index 22e163f2..1a48df6b 100644 --- a/macros/core/Cargo.toml +++ b/macros/core/Cargo.toml @@ -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 diff --git a/macros/core/src/lib.rs b/macros/core/src/lib.rs index 4c0255cc..1831d639 100644 --- a/macros/core/src/lib.rs +++ b/macros/core/src/lib.rs @@ -24,5 +24,6 @@ pub use service::gservice; mod program; mod route; +mod sails_paths; mod service; mod shared; diff --git a/macros/core/src/program.rs b/macros/core/src/program.rs index 46610043..151491b7 100644 --- a/macros/core/src/program.rs +++ b/macros/core/src/program.rs @@ -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; @@ -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)* @@ -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),* } diff --git a/macros/core/src/sails_paths.rs b/macros/core/src/sails_paths.rs new file mode 100644 index 00000000..f5dcd648 --- /dev/null +++ b/macros/core/src/sails_paths.rs @@ -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() +} diff --git a/macros/core/src/service.rs b/macros/core/src/service.rs index 24f91c92..f8904db0 100644 --- a/macros/core/src/service.rs +++ b/macros/core/src/service.rs @@ -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}; @@ -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 @@ -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::() + fn commands() -> #scale_info_path ::MetaType { + #scale_info_path ::MetaType::new::() } - fn queries() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn queries() -> #scale_info_path ::MetaType { + #scale_info_path ::MetaType::new::() } - fn events() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn events() -> #scale_info_path ::MetaType { + #scale_info_path ::MetaType::new::() } } - #(#[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; 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 8da0c81c..f0eebc56 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 @@ -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")] 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 fb11725f..8f44bca9 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 @@ -38,11 +38,12 @@ impl sails_rtl::meta::ProgramMeta for MyProgram { [("Service", sails_rtl::meta::AnyServiceMeta::new::())].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")] 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 e3c2f0ff..9e04f5f7 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 @@ -20,14 +20,18 @@ 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; #[derive(__ProgramDecode, __ProgramTypeInfo)] +#[codec(crate = sails_rtl::scale_codec)] +#[scale_info(crate = sails_rtl::scale_info)] struct __NewParams { p1: u32, p2: String, } #[derive(__ProgramDecode, __ProgramTypeInfo)] +#[codec(crate = sails_rtl::scale_codec)] +#[scale_info(crate = sails_rtl::scale_info)] struct __New2Params { p2: String, p1: u32, @@ -35,6 +39,7 @@ struct __New2Params { mod meta { use super::*; #[derive(__ProgramTypeInfo)] + #[scale_info(crate = sails_rtl::scale_info)] pub enum ConstructorsMeta { New(__NewParams), New2(__New2Params), @@ -101,4 +106,3 @@ pub mod wasm { gstd::msg::reply_bytes(output, 0).expect("Failed to send output"); } } - 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 a08fd0cc..40758e9e 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 @@ -13,11 +13,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")] @@ -69,4 +70,3 @@ pub mod wasm { gstd::msg::reply_bytes(output, 0).expect("Failed to send output"); } } - 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 0dbd3dc5..ac6122f7 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 @@ -17,9 +17,11 @@ 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; #[derive(__ProgramDecode, __ProgramTypeInfo)] +#[codec(crate = sails_rtl::scale_codec)] +#[scale_info(crate = sails_rtl::scale_info)] struct __NewParams { p1: u32, p2: String, @@ -27,6 +29,7 @@ struct __NewParams { mod meta { use super::*; #[derive(__ProgramTypeInfo)] + #[scale_info(crate = sails_rtl::scale_info)] pub enum ConstructorsMeta { New(__NewParams), } @@ -86,4 +89,3 @@ pub mod wasm { gstd::msg::reply_bytes(output, 0).expect("Failed to send output"); } } - diff --git a/macros/core/tests/snapshots/service__gservice_works.snap b/macros/core/tests/snapshots/service__gservice_works.snap index 1cead7a4..418ed671 100644 --- a/macros/core/tests/snapshots/service__gservice_works.snap +++ b/macros/core/tests/snapshots/service__gservice_works.snap @@ -83,36 +83,46 @@ impl sails_rtl::gstd::services::Service for SomeService { } } impl sails_rtl::meta::ServiceMeta for SomeService { - fn commands() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn commands() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } - fn queries() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn queries() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } - fn events() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn events() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } } -#[derive(Decode, TypeInfo)] +use sails_rtl::Decode as __ServiceDecode; +use sails_rtl::Encode as __ServiceEncode; +use sails_rtl::TypeInfo as __ServiceTypeInfo; +#[derive(__ServiceDecode, __ServiceTypeInfo)] +#[codec(crate = sails_rtl::scale_codec)] +#[scale_info(crate = sails_rtl::scale_info)] pub struct __DoThisParams { p1: u32, p2: String, } -#[derive(Decode, TypeInfo)] +#[derive(__ServiceDecode, __ServiceTypeInfo)] +#[codec(crate = sails_rtl::scale_codec)] +#[scale_info(crate = sails_rtl::scale_info)] pub struct __ThisParams { p1: bool, } mod meta { use super::*; - #[derive(TypeInfo)] + #[derive(__ServiceTypeInfo)] + #[scale_info(crate = sails_rtl::scale_info)] pub enum CommandsMeta { DoThis(__DoThisParams, u32), } - #[derive(TypeInfo)] + #[derive(__ServiceTypeInfo)] + #[scale_info(crate = sails_rtl::scale_info)] pub enum QueriesMeta { This(__ThisParams, bool), } - #[derive(TypeInfo)] + #[derive(__ServiceTypeInfo)] + #[scale_info(crate = sails_rtl::scale_info)] pub enum NoEvents {} pub type EventsMeta = NoEvents; } diff --git a/macros/core/tests/snapshots/service__gservice_works_for_lifetimes_and_generics.snap b/macros/core/tests/snapshots/service__gservice_works_for_lifetimes_and_generics.snap index 6850cf79..322fe73e 100644 --- a/macros/core/tests/snapshots/service__gservice_works_for_lifetimes_and_generics.snap +++ b/macros/core/tests/snapshots/service__gservice_works_for_lifetimes_and_generics.snap @@ -88,27 +88,35 @@ where T: Clone, TEventTrigger: EventTrigger, { - fn commands() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn commands() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } - fn queries() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn queries() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } - fn events() -> scale_info::MetaType { - scale_info::MetaType::new::() + fn events() -> sails_rtl::scale_info::MetaType { + sails_rtl::scale_info::MetaType::new::() } } -#[derive(Decode, TypeInfo)] +use sails_rtl::Decode as __ServiceDecode; +use sails_rtl::Encode as __ServiceEncode; +use sails_rtl::TypeInfo as __ServiceTypeInfo; +#[derive(__ServiceDecode, __ServiceTypeInfo)] +#[codec(crate = sails_rtl::scale_codec)] +#[scale_info(crate = sails_rtl::scale_info)] pub struct __DoThisParams {} mod meta { use super::*; - #[derive(TypeInfo)] + #[derive(__ServiceTypeInfo)] + #[scale_info(crate = sails_rtl::scale_info)] pub enum CommandsMeta { DoThis(__DoThisParams, u32), } - #[derive(TypeInfo)] + #[derive(__ServiceTypeInfo)] + #[scale_info(crate = sails_rtl::scale_info)] pub enum QueriesMeta {} - #[derive(TypeInfo)] + #[derive(__ServiceTypeInfo)] + #[scale_info(crate = sails_rtl::scale_info)] pub enum NoEvents {} pub type EventsMeta = events::SomeEvents; } diff --git a/macros/tests/ui/gservice_fails_route_fn_must_be_public.rs b/macros/tests/ui/gservice_fails_route_fn_must_be_public.rs index 0f18c2ab..ea26a0b9 100644 --- a/macros/tests/ui/gservice_fails_route_fn_must_be_public.rs +++ b/macros/tests/ui/gservice_fails_route_fn_must_be_public.rs @@ -1,6 +1,4 @@ -use parity_scale_codec::{Decode, Encode}; -use sails_macros::{gservice, groute}; -use scale_info::TypeInfo; +use sails_macros::{groute, gservice}; struct MyService; diff --git a/macros/tests/ui/gservice_fails_route_fn_must_be_public.stderr b/macros/tests/ui/gservice_fails_route_fn_must_be_public.stderr index d3188e89..e5ff9e19 100644 --- a/macros/tests/ui/gservice_fails_route_fn_must_be_public.stderr +++ b/macros/tests/ui/gservice_fails_route_fn_must_be_public.stderr @@ -1,5 +1,5 @@ error: `groute` attribute can be applied to public methods only - --> tests/ui/gservice_fails_route_fn_must_be_public.rs:14:5 + --> tests/ui/gservice_fails_route_fn_must_be_public.rs:12:5 | -14 | fn this(&self, p1: bool) -> bool { +12 | fn this(&self, p1: bool) -> bool { | ^^ diff --git a/macros/tests/ui/gservice_works.rs b/macros/tests/ui/gservice_works.rs index 4d5af31b..9407692a 100644 --- a/macros/tests/ui/gservice_works.rs +++ b/macros/tests/ui/gservice_works.rs @@ -1,7 +1,8 @@ -use parity_scale_codec::{Decode, Encode}; -use sails_macros::gservice; -use sails_rtl::{gstd::services::Service, MessageId}; -use scale_info::TypeInfo; +use parity_scale_codec::Encode; +use sails_rtl::{ + gstd::{gservice, services::Service}, + MessageId, +}; struct MyService; diff --git a/macros/tests/ui/gservice_works_for_lifecycles_and_generics.rs b/macros/tests/ui/gservice_works_for_lifecycles_and_generics.rs index a6834561..c1e01085 100644 --- a/macros/tests/ui/gservice_works_for_lifecycles_and_generics.rs +++ b/macros/tests/ui/gservice_works_for_lifecycles_and_generics.rs @@ -1,8 +1,8 @@ use core::marker::PhantomData; -use parity_scale_codec::{Decode, Encode}; -use sails_macros::gservice; -use sails_rtl::{gstd::services::Service, MessageId}; -use scale_info::TypeInfo; +use sails_rtl::{ + gstd::{gservice, services::Service}, + MessageId, +}; struct MyService<'a, T> { _a: PhantomData<&'a T>, diff --git a/rtl/Cargo.toml b/rtl/Cargo.toml index 1456aec6..83bea0b7 100644 --- a/rtl/Cargo.toml +++ b/rtl/Cargo.toml @@ -13,6 +13,7 @@ hashbrown.workspace = true hex.workspace = true parity-scale-codec = { workspace = true, features = ["derive"] } primitive-types.workspace = true +sails-macros.workspace = true scale-info = { workspace = true, features = ["derive"] } thiserror-no-std.workspace = true diff --git a/rtl/src/calls.rs b/rtl/src/calls.rs index c49270e4..0eb3437f 100644 --- a/rtl/src/calls.rs +++ b/rtl/src/calls.rs @@ -1,6 +1,10 @@ -use crate::errors::{Result, RtlError}; -use crate::prelude::*; +use crate::{ + errors::{Result, RtlError}, + types::*, + Vec, +}; use core::{future::Future, marker::PhantomData}; +use parity_scale_codec::{Decode, Encode}; pub trait Action { fn with_value(self, value: ValueUnit) -> Self; diff --git a/rtl/src/gstd/mod.rs b/rtl/src/gstd/mod.rs index d8a15212..6dd39485 100644 --- a/rtl/src/gstd/mod.rs +++ b/rtl/src/gstd/mod.rs @@ -1,6 +1,7 @@ use crate::{ActorId, MessageId}; use core::cell::OnceCell; pub use gstd::{async_init, async_main, handle_signal, message_loop, msg, record_reply}; +pub use sails_macros::*; pub mod calls; pub mod events; diff --git a/rtl/src/lib.rs b/rtl/src/lib.rs index 528ad972..7b020f04 100644 --- a/rtl/src/lib.rs +++ b/rtl/src/lib.rs @@ -1,5 +1,6 @@ #![no_std] +pub use hex::{self}; pub use prelude::*; pub mod calls; @@ -8,16 +9,7 @@ pub mod gstd; #[cfg(not(target_arch = "wasm32"))] pub mod gtest; pub mod prelude; -pub mod types; - -pub mod hex { - pub use ::hex::*; -} - -pub mod scale { - pub use parity_scale_codec::{Decode, Encode, EncodeLike}; - pub use scale_info::TypeInfo; -} +mod types; pub mod meta { use scale_info::MetaType; diff --git a/rtl/src/prelude.rs b/rtl/src/prelude.rs index 26bfd5e2..a2f69cff 100644 --- a/rtl/src/prelude.rs +++ b/rtl/src/prelude.rs @@ -47,5 +47,5 @@ pub mod ffi { pub use crate::types::*; -// Reexports from third-party libraries -pub use crate::scale::*; +pub use parity_scale_codec::{self as scale_codec, Decode, Encode, EncodeLike}; +pub use scale_info::{self as scale_info, TypeInfo}; diff --git a/rtl/src/types.rs b/rtl/src/types.rs index e33ba501..709c8666 100644 --- a/rtl/src/types.rs +++ b/rtl/src/types.rs @@ -1,5 +1,6 @@ -use crate::prelude::*; +use parity_scale_codec::{Decode, Encode}; pub use primitive_types::*; +use scale_info::TypeInfo; #[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Encode, Decode, TypeInfo)] pub struct ActorId([u8; 32]);