diff --git a/benchmarks/ping-pong/app/src/ping_pong_client.rs b/benchmarks/ping-pong/app/src/ping_pong_client.rs index 0d62ed0a0..49b47633f 100644 --- a/benchmarks/ping-pong/app/src/ping_pong_client.rs +++ b/benchmarks/ping-pong/app/src/ping_pong_client.rs @@ -3,7 +3,7 @@ use sails_rs::{client::*, collections::*, prelude::*}; pub struct PingPongProgram; impl PingPongProgram { - pub const PING_PONG_SERVICE_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_PING_PONG_SERVICE: u8 = 1; } impl sails_rs::client::Program for PingPongProgram {} pub trait PingPong { @@ -17,7 +17,7 @@ impl PingPong for sails_rs::client::Actor sails_rs::client::Service { - self.service(PingPongProgram::PING_PONG_SERVICE_ROUTE_ID) + self.service(PingPongProgram::ROUTE_ID_PING_PONG_SERVICE) } } pub trait PingPongCtors { @@ -39,7 +39,7 @@ impl PingPongCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(NewForBench () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(NewForBench () -> (), 0); } pub mod ping_pong_service { diff --git a/benchmarks/src/alloc_stress_program.rs b/benchmarks/src/alloc_stress_program.rs index f6bb81e2e..e65c68825 100644 --- a/benchmarks/src/alloc_stress_program.rs +++ b/benchmarks/src/alloc_stress_program.rs @@ -3,7 +3,7 @@ use sails_rs::{client::*, collections::*, prelude::*}; pub struct AllocStressProgram; impl AllocStressProgram { - pub const ALLOC_STRESS_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_ALLOC_STRESS: u8 = 1; } impl sails_rs::client::Program for AllocStressProgram {} pub trait AllocStress { @@ -13,7 +13,7 @@ pub trait AllocStress { impl AllocStress for sails_rs::client::Actor { type Env = E; fn alloc_stress(&self) -> sails_rs::client::Service { - self.service(AllocStressProgram::ALLOC_STRESS_ROUTE_ID) + self.service(AllocStressProgram::ROUTE_ID_ALLOC_STRESS) } } pub trait AllocStressCtors { @@ -35,7 +35,7 @@ impl AllocStressCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(NewForBench () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(NewForBench () -> (), 0); } pub mod alloc_stress { diff --git a/benchmarks/src/benchmarks.rs b/benchmarks/src/benchmarks.rs index ea309201f..ad75475f8 100644 --- a/benchmarks/src/benchmarks.rs +++ b/benchmarks/src/benchmarks.rs @@ -77,7 +77,7 @@ async fn compute_stress_bench() { let (payload, gas) = extract_reply_and_gas(env.system(), message_id); // Low-level approach: decoding using generated io module let stress_resp = - crate::clients::compute_stress_client::compute_stress::io::ComputeStress::decode_reply(ComputeStressProgram::COMPUTE_STRESS_ROUTE_ID, payload.as_slice()) + crate::clients::compute_stress_client::compute_stress::io::ComputeStress::decode_reply(ComputeStressProgram::ROUTE_ID_COMPUTE_STRESS, payload.as_slice()) .unwrap(); assert_eq!(stress_resp.res, expected_sum); gas @@ -108,7 +108,7 @@ async fn counter_bench() { let (payload, gas) = extract_reply_and_gas(env.system(), message_id); // Low-level approach: decoding using generated io module let stress_resp = crate::clients::counter_bench_client::counter_bench::io::Inc::decode_reply( - CounterBenchProgram::COUNTER_BENCH_ROUTE_ID, + CounterBenchProgram::ROUTE_ID_COUNTER_BENCH, payload.as_slice(), ) .unwrap(); @@ -121,7 +121,7 @@ async fn counter_bench() { let (payload, gas) = extract_reply_and_gas(env.system(), message_id); // Low-level approach: decoding using generated io module let stress_resp = crate::clients::counter_bench_client::counter_bench::io::IncAsync::decode_reply( - CounterBenchProgram::COUNTER_BENCH_ROUTE_ID, + CounterBenchProgram::ROUTE_ID_COUNTER_BENCH, payload.as_slice(), ) .unwrap(); @@ -170,7 +170,7 @@ async fn cross_program_bench() { // Low-level approach: decoding using generated io module let stress_resp = ping_pong_bench_app::client::ping_pong_service::io::Ping::decode_reply( - PingPongProgram::PING_PONG_SERVICE_ROUTE_ID, + PingPongProgram::ROUTE_ID_PING_PONG_SERVICE, payload.as_slice(), ) .unwrap(); @@ -211,7 +211,7 @@ async fn redirect_bench() { let (payload, _gas) = extract_reply_and_gas(env.system(), message_id); // Low-level approach: decoding using generated io module let resp = redirect_proxy_client::proxy::io::GetProgramId::decode_reply( - RedirectProxyClientProgram::PROXY_ROUTE_ID, + RedirectProxyClientProgram::ROUTE_ID_PROXY, payload.as_slice(), ) .unwrap(); @@ -236,7 +236,7 @@ async fn redirect_bench() { let (payload, gas) = extract_reply_and_gas(env.system(), message_id); // Low-level approach: decoding using generated io module let resp = redirect_proxy_client::proxy::io::GetProgramId::decode_reply( - RedirectProxyClientProgram::PROXY_ROUTE_ID, + RedirectProxyClientProgram::ROUTE_ID_PROXY, payload.as_slice(), ) .unwrap(); @@ -284,7 +284,7 @@ async fn alloc_stress_test(n: u32) -> (usize, u64) { // Low-level approach: decoding using generated io module let stress_resp = crate::clients::alloc_stress_client::alloc_stress::io::AllocStress::decode_reply( - AllocStressProgram::ALLOC_STRESS_ROUTE_ID, + AllocStressProgram::ROUTE_ID_ALLOC_STRESS, payload.as_slice(), ) .unwrap(); diff --git a/benchmarks/src/compute_stress_program.rs b/benchmarks/src/compute_stress_program.rs index f41111441..72fc6c467 100644 --- a/benchmarks/src/compute_stress_program.rs +++ b/benchmarks/src/compute_stress_program.rs @@ -3,7 +3,7 @@ use sails_rs::{client::*, collections::*, prelude::*}; pub struct ComputeStressProgram; impl ComputeStressProgram { - pub const COMPUTE_STRESS_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_COMPUTE_STRESS: u8 = 1; } impl sails_rs::client::Program for ComputeStressProgram {} pub trait ComputeStress { @@ -19,7 +19,7 @@ impl ComputeStress fn compute_stress( &self, ) -> sails_rs::client::Service { - self.service(ComputeStressProgram::COMPUTE_STRESS_ROUTE_ID) + self.service(ComputeStressProgram::ROUTE_ID_COMPUTE_STRESS) } } pub trait ComputeStressCtors { @@ -41,7 +41,7 @@ impl ComputeStressCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(NewForBench () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(NewForBench () -> (), 0); } pub mod compute_stress { diff --git a/benchmarks/src/counter_bench_program.rs b/benchmarks/src/counter_bench_program.rs index 2d8b959dc..46287d684 100644 --- a/benchmarks/src/counter_bench_program.rs +++ b/benchmarks/src/counter_bench_program.rs @@ -3,7 +3,7 @@ use sails_rs::{client::*, collections::*, prelude::*}; pub struct CounterBenchProgram; impl CounterBenchProgram { - pub const COUNTER_BENCH_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_COUNTER_BENCH: u8 = 1; } impl sails_rs::client::Program for CounterBenchProgram {} pub trait CounterBench { @@ -19,7 +19,7 @@ impl CounterBench fn counter_bench( &self, ) -> sails_rs::client::Service { - self.service(CounterBenchProgram::COUNTER_BENCH_ROUTE_ID) + self.service(CounterBenchProgram::ROUTE_ID_COUNTER_BENCH) } } pub trait CounterBenchCtors { @@ -41,7 +41,7 @@ impl CounterBenchCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(NewForBench () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(NewForBench () -> (), 0); } pub mod counter_bench { diff --git a/examples/demo/app/tests/gclient.rs b/examples/demo/app/tests/gclient.rs index 8fa824e4e..c6f96d3e6 100644 --- a/examples/demo/app/tests/gclient.rs +++ b/examples/demo/app/tests/gclient.rs @@ -112,7 +112,7 @@ async fn ping_pong_works() { // Use generated `io` module for encoding/decoding calls and replies // and send/receive bytes using `gclient` native means (env is just a wrapper) let ping_call_payload = - ping_pong::io::Ping::encode_call(DemoClientProgram::PING_PONG_ROUTE_ID, "ping".into()); + ping_pong::io::Ping::encode_call(DemoClientProgram::ROUTE_ID_PING_PONG, "ping".into()); // Act let ping_reply_payload = env @@ -125,7 +125,7 @@ async fn ping_pong_works() { .unwrap(); let ping_reply = ping_pong::io::Ping::decode_reply( - DemoClientProgram::PING_PONG_ROUTE_ID, + DemoClientProgram::ROUTE_ID_PING_PONG, ping_reply_payload, ) .unwrap(); diff --git a/examples/demo/app/tests/gtest.rs b/examples/demo/app/tests/gtest.rs index af6f8c5d2..7f01bcf15 100644 --- a/examples/demo/app/tests/gtest.rs +++ b/examples/demo/app/tests/gtest.rs @@ -177,7 +177,7 @@ async fn ping_pong_low_level_works() { // Use generated `io` module for encoding/decoding calls and replies // and send/receive bytes using `gtest` native means - let ping_call_payload = Ping::encode_call(DemoClientProgram::PING_PONG_ROUTE_ID, "ping".into()); + let ping_call_payload = Ping::encode_call(DemoClientProgram::ROUTE_ID_PING_PONG, "ping".into()); let message_id = demo_program.send_bytes(ACTOR_ID, ping_call_payload); let run_result = system.run_next_block(); @@ -191,7 +191,7 @@ async fn ping_pong_low_level_works() { let ping_reply_payload = reply_log_record.payload(); let ping_reply = - Ping::decode_reply(DemoClientProgram::PING_PONG_ROUTE_ID, ping_reply_payload).unwrap(); + Ping::decode_reply(DemoClientProgram::ROUTE_ID_PING_PONG, ping_reply_payload).unwrap(); assert_eq!(ping_reply, Ok("pong".to_string())); @@ -456,7 +456,7 @@ fn counter_add_low_level_works() { // Use generated `io` module for encoding/decoding calls and replies // and send/receive bytes using `gtest` native means - let call_payload = Add::encode_call(DemoClientProgram::COUNTER_ROUTE_ID, 10); + let call_payload = Add::encode_call(DemoClientProgram::ROUTE_ID_COUNTER, 10); let message_id = demo_program.send_bytes(ACTOR_ID, call_payload); let run_result = system.run_next_block(); @@ -469,7 +469,7 @@ fn counter_add_low_level_works() { let reply_payload = reply_log_record.payload(); - let reply = Add::decode_reply(DemoClientProgram::COUNTER_ROUTE_ID, reply_payload).unwrap(); + let reply = Add::decode_reply(DemoClientProgram::ROUTE_ID_COUNTER, reply_payload).unwrap(); assert_eq!(reply, 10); @@ -560,7 +560,7 @@ fn chaos_service_panic_after_wait_works() { let msg_id = program.send_bytes( ACTOR_ID, - PanicAfterWait::encode_call(DemoClientProgram::CHAOS_ROUTE_ID), + PanicAfterWait::encode_call(DemoClientProgram::ROUTE_ID_CHAOS), ); system.run_next_block(); @@ -595,7 +595,7 @@ fn chaos_service_timeout_wait() { program.send_bytes( ACTOR_ID, - TimeoutWait::encode_call(DemoClientProgram::CHAOS_ROUTE_ID), + TimeoutWait::encode_call(DemoClientProgram::ROUTE_ID_CHAOS), ); //#1 system.run_next_block(); @@ -614,13 +614,13 @@ fn chaos_service_timeout_wait() { let msg_id = program.send_bytes( ACTOR_ID, - ReplyHookCounter::encode_call(DemoClientProgram::CHAOS_ROUTE_ID), + ReplyHookCounter::encode_call(DemoClientProgram::ROUTE_ID_CHAOS), ); let run = system.run_next_block(); let val = extract_reply(&run, msg_id, |p| { - ReplyHookCounter::decode_reply(DemoClientProgram::CHAOS_ROUTE_ID, p).unwrap() + ReplyHookCounter::decode_reply(DemoClientProgram::ROUTE_ID_CHAOS, p).unwrap() }); assert_eq!(val, 0, "handle_reply should not trigger before reply"); @@ -633,11 +633,11 @@ fn chaos_service_timeout_wait() { let msg_id = program.send_bytes( ACTOR_ID, - ReplyHookCounter::encode_call(DemoClientProgram::CHAOS_ROUTE_ID), + ReplyHookCounter::encode_call(DemoClientProgram::ROUTE_ID_CHAOS), ); let run = system.run_next_block(); let val = extract_reply(&run, msg_id, |p| { - ReplyHookCounter::decode_reply(DemoClientProgram::CHAOS_ROUTE_ID, p).unwrap() + ReplyHookCounter::decode_reply(DemoClientProgram::ROUTE_ID_CHAOS, p).unwrap() }); assert_eq!( val, 1, diff --git a/examples/demo/client/src/demo_client.rs b/examples/demo/client/src/demo_client.rs index d1f078c7b..19b55fb06 100644 --- a/examples/demo/client/src/demo_client.rs +++ b/examples/demo/client/src/demo_client.rs @@ -10,13 +10,13 @@ use core::num::NonZeroU32; use sails_rs::{client::*, collections::*, prelude::*}; pub struct DemoClientProgram; impl DemoClientProgram { - pub const PING_PONG_ROUTE_ID: u8 = 1; - pub const COUNTER_ROUTE_ID: u8 = 2; - pub const DOG_ROUTE_ID: u8 = 3; - pub const REFERENCES_ROUTE_ID: u8 = 4; - pub const THIS_THAT_ROUTE_ID: u8 = 5; - pub const VALUE_FEE_ROUTE_ID: u8 = 6; - pub const CHAOS_ROUTE_ID: u8 = 7; + pub const ROUTE_ID_PING_PONG: u8 = 1; + pub const ROUTE_ID_COUNTER: u8 = 2; + pub const ROUTE_ID_DOG: u8 = 3; + pub const ROUTE_ID_REFERENCES: u8 = 4; + pub const ROUTE_ID_THIS_THAT: u8 = 5; + pub const ROUTE_ID_VALUE_FEE: u8 = 6; + pub const ROUTE_ID_CHAOS: u8 = 7; } impl sails_rs::client::Program for DemoClientProgram {} pub trait DemoClient { @@ -32,25 +32,25 @@ pub trait DemoClient { impl DemoClient for sails_rs::client::Actor { type Env = E; fn ping_pong(&self) -> sails_rs::client::Service { - self.service(DemoClientProgram::PING_PONG_ROUTE_ID) + self.service(DemoClientProgram::ROUTE_ID_PING_PONG) } fn counter(&self) -> sails_rs::client::Service { - self.service(DemoClientProgram::COUNTER_ROUTE_ID) + self.service(DemoClientProgram::ROUTE_ID_COUNTER) } fn dog(&self) -> sails_rs::client::Service { - self.service(DemoClientProgram::DOG_ROUTE_ID) + self.service(DemoClientProgram::ROUTE_ID_DOG) } fn references(&self) -> sails_rs::client::Service { - self.service(DemoClientProgram::REFERENCES_ROUTE_ID) + self.service(DemoClientProgram::ROUTE_ID_REFERENCES) } fn this_that(&self) -> sails_rs::client::Service { - self.service(DemoClientProgram::THIS_THAT_ROUTE_ID) + self.service(DemoClientProgram::ROUTE_ID_THIS_THAT) } fn value_fee(&self) -> sails_rs::client::Service { - self.service(DemoClientProgram::VALUE_FEE_ROUTE_ID) + self.service(DemoClientProgram::ROUTE_ID_VALUE_FEE) } fn chaos(&self) -> sails_rs::client::Service { - self.service(DemoClientProgram::CHAOS_ROUTE_ID) + self.service(DemoClientProgram::ROUTE_ID_CHAOS) } } pub trait DemoClientCtors { @@ -84,8 +84,8 @@ impl DemoClientCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(Default () -> (), 0, sails_rs::meta::InterfaceId::zero()); - sails_rs::io_struct_impl!(New (counter: super::Option, dog_position: super::Option<(i32, i32, ), >) -> (), 1, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(Default () -> (), 0); + sails_rs::io_struct_impl!(New (counter: super::Option, dog_position: super::Option<(i32, i32, ), >) -> (), 1); } pub mod ping_pong { @@ -170,6 +170,14 @@ pub mod counter { #[codec(index = 1)] Subtracted(u32), } + impl CounterEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::Added { .. } => 0, + Self::Subtracted { .. } => 1, + } + } + } impl sails_rs::client::Event for CounterEvents {} impl sails_rs::client::Identifiable for CounterEvents { const INTERFACE_ID: sails_rs::InterfaceId = @@ -229,6 +237,13 @@ pub mod walker_service { #[codec(index = 0)] Walked { from: (i32, i32), to: (i32, i32) }, } + impl WalkerServiceEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::Walked { .. } => 0, + } + } + } impl sails_rs::client::Event for WalkerServiceEvents {} impl sails_rs::client::Identifiable for WalkerServiceEvents { const INTERFACE_ID: sails_rs::InterfaceId = @@ -338,6 +353,13 @@ pub mod dog { #[codec(index = 0)] Barked, } + impl DogEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::Barked { .. } => 0, + } + } + } impl sails_rs::client::Event for DogEvents {} impl sails_rs::client::Identifiable for DogEvents { const INTERFACE_ID: sails_rs::InterfaceId = @@ -582,6 +604,13 @@ pub mod value_fee { #[codec(index = 0)] Withheld(u128), } + impl ValueFeeEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::Withheld { .. } => 0, + } + } + } impl sails_rs::client::Event for ValueFeeEvents {} impl sails_rs::client::Identifiable for ValueFeeEvents { const INTERFACE_ID: sails_rs::InterfaceId = diff --git a/examples/no-svcs-prog/wasm/src/no_svcs_prog.rs b/examples/no-svcs-prog/wasm/src/no_svcs_prog.rs index 14290706c..b85ac55f0 100644 --- a/examples/no-svcs-prog/wasm/src/no_svcs_prog.rs +++ b/examples/no-svcs-prog/wasm/src/no_svcs_prog.rs @@ -25,5 +25,5 @@ impl NoSvcsProgCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(Create () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(Create () -> (), 0); } diff --git a/examples/ping-pong-stack/src/ping_pong_stack.rs b/examples/ping-pong-stack/src/ping_pong_stack.rs index 3a22aab1f..652595bc2 100644 --- a/examples/ping-pong-stack/src/ping_pong_stack.rs +++ b/examples/ping-pong-stack/src/ping_pong_stack.rs @@ -3,7 +3,7 @@ use sails_rs::{client::*, collections::*, prelude::*}; pub struct PingPongStackProgram; impl PingPongStackProgram { - pub const PING_PONG_STACK_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_PING_PONG_STACK: u8 = 1; } impl sails_rs::client::Program for PingPongStackProgram {} pub trait PingPongStack { @@ -19,7 +19,7 @@ impl PingPongStack fn ping_pong_stack( &self, ) -> sails_rs::client::Service { - self.service(PingPongStackProgram::PING_PONG_STACK_ROUTE_ID) + self.service(PingPongStackProgram::ROUTE_ID_PING_PONG_STACK) } } pub trait PingPongStackCtors { @@ -51,8 +51,8 @@ impl PingPongStackCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(CreatePing (code_id: CodeId) -> (), 0, sails_rs::meta::InterfaceId::zero()); - sails_rs::io_struct_impl!(CreatePong () -> (), 1, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(CreatePing (code_id: CodeId) -> (), 0); + sails_rs::io_struct_impl!(CreatePong () -> (), 1); } pub mod ping_pong_stack { diff --git a/examples/redirect/client/src/redirect_client.rs b/examples/redirect/client/src/redirect_client.rs index 54a077371..7d4885d76 100644 --- a/examples/redirect/client/src/redirect_client.rs +++ b/examples/redirect/client/src/redirect_client.rs @@ -3,7 +3,7 @@ use sails_rs::{client::*, collections::*, prelude::*}; pub struct RedirectClientProgram; impl RedirectClientProgram { - pub const REDIRECT_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_REDIRECT: u8 = 1; } impl sails_rs::client::Program for RedirectClientProgram {} pub trait RedirectClient { @@ -15,7 +15,7 @@ impl RedirectClient { type Env = E; fn redirect(&self) -> sails_rs::client::Service { - self.service(RedirectClientProgram::REDIRECT_ROUTE_ID) + self.service(RedirectClientProgram::ROUTE_ID_REDIRECT) } } pub trait RedirectClientCtors { @@ -35,7 +35,7 @@ impl RedirectClientCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(New () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(New () -> (), 0); } pub mod redirect { diff --git a/examples/redirect/proxy-client/src/redirect_proxy_client.rs b/examples/redirect/proxy-client/src/redirect_proxy_client.rs index c6c9e08d9..7b7efd046 100644 --- a/examples/redirect/proxy-client/src/redirect_proxy_client.rs +++ b/examples/redirect/proxy-client/src/redirect_proxy_client.rs @@ -3,7 +3,7 @@ use sails_rs::{client::*, collections::*, prelude::*}; pub struct RedirectProxyClientProgram; impl RedirectProxyClientProgram { - pub const PROXY_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_PROXY: u8 = 1; } impl sails_rs::client::Program for RedirectProxyClientProgram {} pub trait RedirectProxyClient { @@ -15,7 +15,7 @@ impl RedirectProxyClient { type Env = E; fn proxy(&self) -> sails_rs::client::Service { - self.service(RedirectProxyClientProgram::PROXY_ROUTE_ID) + self.service(RedirectProxyClientProgram::ROUTE_ID_PROXY) } } pub trait RedirectProxyClientCtors { @@ -42,7 +42,7 @@ impl RedirectProxyClientCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(New (target: ActorId) -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(New (target: ActorId) -> (), 0); } pub mod proxy { diff --git a/examples/rmrk/resource/app/src/rmrk_catalog.rs b/examples/rmrk/resource/app/src/rmrk_catalog.rs index 968fff150..3f48060f1 100644 --- a/examples/rmrk/resource/app/src/rmrk_catalog.rs +++ b/examples/rmrk/resource/app/src/rmrk_catalog.rs @@ -6,7 +6,7 @@ extern crate std; use sails_rs::{client::*, collections::*, prelude::*}; pub struct RmrkCatalogProgram; impl RmrkCatalogProgram { - pub const RMRK_CATALOG_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_RMRK_CATALOG: u8 = 1; } impl sails_rs::client::Program for RmrkCatalogProgram {} pub trait RmrkCatalog { @@ -16,7 +16,7 @@ pub trait RmrkCatalog { impl RmrkCatalog for sails_rs::client::Actor { type Env = E; fn rmrk_catalog(&self) -> sails_rs::client::Service { - self.service(RmrkCatalogProgram::RMRK_CATALOG_ROUTE_ID) + self.service(RmrkCatalogProgram::ROUTE_ID_RMRK_CATALOG) } } pub trait RmrkCatalogCtors { @@ -36,7 +36,7 @@ impl RmrkCatalogCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(New () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(New () -> (), 0); } pub mod rmrk_catalog { diff --git a/examples/rmrk/resource/wasm/src/rmrk_resource.rs b/examples/rmrk/resource/wasm/src/rmrk_resource.rs index 347885f62..e2ffda177 100644 --- a/examples/rmrk/resource/wasm/src/rmrk_resource.rs +++ b/examples/rmrk/resource/wasm/src/rmrk_resource.rs @@ -3,7 +3,7 @@ use sails_rs::{client::*, collections::*, prelude::*}; pub struct RmrkResourceProgram; impl RmrkResourceProgram { - pub const RMRK_RESOURCE_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_RMRK_RESOURCE: u8 = 1; } impl sails_rs::client::Program for RmrkResourceProgram {} pub trait RmrkResource { @@ -19,7 +19,7 @@ impl RmrkResource fn rmrk_resource( &self, ) -> sails_rs::client::Service { - self.service(RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID) + self.service(RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE) } } pub trait RmrkResourceCtors { @@ -39,7 +39,7 @@ impl RmrkResourceCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(New () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(New () -> (), 0); } pub mod rmrk_resource { @@ -174,6 +174,14 @@ pub mod rmrk_resource { #[codec(index = 1)] ResourceAdded { resource_id: u8 }, } + impl RmrkResourceEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::PartAdded { .. } => 0, + Self::ResourceAdded { .. } => 1, + } + } + } impl sails_rs::client::Event for RmrkResourceEvents {} impl sails_rs::client::Identifiable for RmrkResourceEvents { const INTERFACE_ID: sails_rs::InterfaceId = diff --git a/examples/rmrk/resource/wasm/tests/resources.rs b/examples/rmrk/resource/wasm/tests/resources.rs index 2f0365534..f217b6177 100644 --- a/examples/rmrk/resource/wasm/tests/resources.rs +++ b/examples/rmrk/resource/wasm/tests/resources.rs @@ -44,9 +44,9 @@ fn adding_resource_to_storage_by_admin_succeeds() { // Assert println!("{run_result:#?}"); let header = SailsMessageHeader::v1( - io::AddResourceEntry::INTERFACE_ID, + RmrkResourceImpl::INTERFACE_ID, io::AddResourceEntry::ENTRY_ID, - RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID, + RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE, ); let expected_response = [ header.encode(), @@ -55,10 +55,13 @@ fn adding_resource_to_storage_by_admin_succeeds() { .concat(); assert!(run_result.contains(&(ADMIN_ID, expected_response))); + let event = client::rmrk_resource::events::RmrkResourceEvents::ResourceAdded { + resource_id: RESOURCE_ID, + }; let event_header = SailsMessageHeader::v1( - io::AddResourceEntry::INTERFACE_ID, - 1, // EntryID for ResourceAdded (alphabetically 2nd) - RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID, + RmrkResourceImpl::INTERFACE_ID, + event.entry_id(), + RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE, ); let expected_event = [event_header.encode(), RESOURCE_ID.encode()].concat(); assert!(run_result.contains(&(0, expected_event))); @@ -95,9 +98,9 @@ async fn adding_resource_to_storage_by_admin_succeeds_async() { let reply = reply.unwrap(); let header = SailsMessageHeader::v1( - io::AddResourceEntry::INTERFACE_ID, + RmrkResourceImpl::INTERFACE_ID, io::AddResourceEntry::ENTRY_ID, - RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID, + RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE, ); let expected_reply = [ header.encode(), @@ -163,9 +166,9 @@ fn adding_existing_part_to_resource_by_admin_succeeds() { // Assert println!("{run_result:#?}"); let header = SailsMessageHeader::v1( - io::AddPartToResource::INTERFACE_ID, + RmrkResourceImpl::INTERFACE_ID, io::AddPartToResource::ENTRY_ID, - RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID, + RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE, ); let expected_response = [ header.encode(), @@ -262,9 +265,9 @@ fn adding_non_existing_part_to_resource_fails() { // Assert let header = SailsMessageHeader::v1( - io::AddPartToResource::INTERFACE_ID, + RmrkResourceImpl::INTERFACE_ID, io::AddPartToResource::ENTRY_ID, - RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID, + RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE, ); let expected_response = [ header.encode(), @@ -327,9 +330,9 @@ impl SystemFixture { ) -> BlockRunResult { let program = self.resource_program(); let header = SailsMessageHeader::v1( - io::AddResourceEntry::INTERFACE_ID, + RmrkResourceImpl::INTERFACE_ID, io::AddResourceEntry::ENTRY_ID, - RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID, + RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE, ); let encoded_request = [header.encode(), resource_id.encode(), resource.encode()].concat(); let _message_id = program.send_bytes(actor_id, encoded_request); @@ -344,9 +347,9 @@ impl SystemFixture { ) -> BlockRunResult { let program = self.resource_program(); let header = SailsMessageHeader::v1( - io::AddPartToResource::INTERFACE_ID, + RmrkResourceImpl::INTERFACE_ID, io::AddPartToResource::ENTRY_ID, - RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID, + RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE, ); let encoded_request = [header.encode(), resource_id.encode(), part_id.encode()].concat(); let _message_id = program.send_bytes(actor_id, encoded_request); @@ -360,9 +363,9 @@ impl SystemFixture { ) -> Option> { let program = self.resource_program(); let header = SailsMessageHeader::v1( - io::Resource::INTERFACE_ID, + RmrkResourceImpl::INTERFACE_ID, io::Resource::ENTRY_ID, - RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID, + RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE, ); let encoded_request = [header.encode(), resource_id.encode()].concat(); let _message_id = program.send_bytes(actor_id, encoded_request); @@ -384,9 +387,9 @@ impl SystemFixture { fn add_parts(&self, actor_id: u64, parts: &BTreeMap) -> BlockRunResult { let program = self.catalog_program(); let header = SailsMessageHeader::v1( - catalogs::rmrk_catalog::io::AddParts::INTERFACE_ID, + catalogs::rmrk_catalog::RmrkCatalogImpl::INTERFACE_ID, catalogs::rmrk_catalog::io::AddParts::ENTRY_ID, - catalogs::RmrkCatalogProgram::RMRK_CATALOG_ROUTE_ID, + catalogs::RmrkCatalogProgram::ROUTE_ID_RMRK_CATALOG, ); let encoded_request = [header.encode(), parts.encode()].concat(); let _message_id = program.send_bytes(actor_id, encoded_request); @@ -446,9 +449,9 @@ impl Fixture { resource: &Resource, ) -> Result, GtestError> { let header = SailsMessageHeader::v1( - io::AddResourceEntry::INTERFACE_ID, + RmrkResourceImpl::INTERFACE_ID, io::AddResourceEntry::ENTRY_ID, - RmrkResourceProgram::RMRK_RESOURCE_ROUTE_ID, + RmrkResourceProgram::ROUTE_ID_RMRK_RESOURCE, ); let encoded_request = [header.encode(), resource_id.encode(), resource.encode()].concat(); self.env @@ -509,9 +512,9 @@ impl Fixture { .get_program(self.catalog_program_id) .unwrap(); let header = SailsMessageHeader::v1( - catalogs::rmrk_catalog::io::AddParts::INTERFACE_ID, + catalogs::rmrk_catalog::RmrkCatalogImpl::INTERFACE_ID, catalogs::rmrk_catalog::io::AddParts::ENTRY_ID, - catalogs::RmrkCatalogProgram::RMRK_CATALOG_ROUTE_ID, + catalogs::RmrkCatalogProgram::ROUTE_ID_RMRK_CATALOG, ); let encoded_request = [header.encode(), parts.encode()].concat(); let _message_id = program.send_bytes(actor_id, encoded_request); diff --git a/rs/client-gen/src/ctor_generators.rs b/rs/client-gen/src/ctor_generators.rs index d7a94174c..d0a7e7031 100644 --- a/rs/client-gen/src/ctor_generators.rs +++ b/rs/client-gen/src/ctor_generators.rs @@ -87,7 +87,7 @@ impl<'ast> Visitor<'ast> for CtorGenerator<'ast> { let params_with_types_super = &fn_args_with_types_path(&func.params, "super"); let entry_id = self.entry_ids.get(func.name.as_str()).copied().unwrap_or(0); quote_in! { self.io_tokens => - $(self.sails_path)::io_struct_impl!($fn_name ($params_with_types_super) -> (), $entry_id, $(self.sails_path)::meta::InterfaceId::zero()); + $(self.sails_path)::io_struct_impl!($fn_name ($params_with_types_super) -> (), $entry_id); }; } } diff --git a/rs/client-gen/src/events_generator.rs b/rs/client-gen/src/events_generator.rs index a81f7a459..510c190df 100644 --- a/rs/client-gen/src/events_generator.rs +++ b/rs/client-gen/src/events_generator.rs @@ -49,9 +49,18 @@ impl<'ast> Visitor<'ast> for EventsModuleGenerator<'ast> { quote_in! { self.tokens => $['\r'] $("}") - }; - quote_in! { self.tokens => + $['\r'] + impl $events_name { + pub fn entry_id(&self) -> u16 { + match self { + $(for event in &service.events join ($['\r']) => + Self::$(&event.name) { .. } => $(self.entry_ids.get(event.name.as_str()).copied().unwrap_or(0)), + ) + } + } + } + impl $(self.sails_path)::client::Event for $events_name {} impl $(self.sails_path)::client::Identifiable for $events_name { diff --git a/rs/client-gen/src/root_generator.rs b/rs/client-gen/src/root_generator.rs index 23cc86034..9bbf92de6 100644 --- a/rs/client-gen/src/root_generator.rs +++ b/rs/client-gen/src/root_generator.rs @@ -156,7 +156,7 @@ impl<'ast> Visitor<'ast> for RootGenerator<'ast> { generate_doc_comments(&mut self.service_trait_tokens, &service_item.docs); - let route_id_const_name = format!("{}_ROUTE_ID", service_route.to_case(Case::UpperSnake)); + let route_id_const_name = format!("ROUTE_ID_{}", service_route.to_case(Case::UpperSnake)); quote_in!(self.program_meta_tokens => pub const $(&route_id_const_name): u8 = $(service_item.route_idx); diff --git a/rs/client-gen/tests/snapshots/generator__complex_type_generation_works.snap b/rs/client-gen/tests/snapshots/generator__complex_type_generation_works.snap index f25078d9a..c12bf5aca 100644 --- a/rs/client-gen/tests/snapshots/generator__complex_type_generation_works.snap +++ b/rs/client-gen/tests/snapshots/generator__complex_type_generation_works.snap @@ -10,8 +10,8 @@ extern crate std; use sails_rs::{client::*, collections::*, prelude::*}; pub struct ComplexTypesProgramProgram; impl ComplexTypesProgramProgram { - pub const MY_COMPLEX_SERVICE_ROUTE_ID: u8 = 1; - pub const ANOTHER_SERVICE_ROUTE_ID: u8 = 2; + pub const ROUTE_ID_MY_COMPLEX_SERVICE: u8 = 1; + pub const ROUTE_ID_ANOTHER_SERVICE: u8 = 2; } impl sails_rs::client::Program for ComplexTypesProgramProgram {} pub trait ComplexTypesProgram { @@ -30,12 +30,12 @@ impl ComplexTypesProgram fn my_complex_service( &self, ) -> sails_rs::client::Service { - self.service(ComplexTypesProgramProgram::MY_COMPLEX_SERVICE_ROUTE_ID) + self.service(ComplexTypesProgramProgram::ROUTE_ID_MY_COMPLEX_SERVICE) } fn another_service( &self, ) -> sails_rs::client::Service { - self.service(ComplexTypesProgramProgram::ANOTHER_SERVICE_ROUTE_ID) + self.service(ComplexTypesProgramProgram::ROUTE_ID_ANOTHER_SERVICE) } } pub trait ComplexTypesProgramCtors { @@ -61,7 +61,7 @@ impl ComplexTypesProgramCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(NewProgram (initial_count: u32, id_prefix: String) -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(NewProgram (initial_count: u32, id_prefix: String) -> (), 0); } pub mod my_complex_service { diff --git a/rs/client-gen/tests/snapshots/generator__events_works.snap b/rs/client-gen/tests/snapshots/generator__events_works.snap index 5e3af9722..36f6f0397 100644 --- a/rs/client-gen/tests/snapshots/generator__events_works.snap +++ b/rs/client-gen/tests/snapshots/generator__events_works.snap @@ -67,6 +67,16 @@ pub mod service_with_events { #[codec(index = 3)] Two { id: u8, reference: u64 }, } + impl ServiceWithEventsEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::One { .. } => 0, + Self::Reset { .. } => 1, + Self::Three { .. } => 2, + Self::Two { .. } => 3, + } + } + } impl sails_rs::client::Event for ServiceWithEventsEvents {} impl sails_rs::client::Identifiable for ServiceWithEventsEvents { const INTERFACE_ID: sails_rs::InterfaceId = diff --git a/rs/client-gen/tests/snapshots/generator__full_with_sails_path.snap b/rs/client-gen/tests/snapshots/generator__full_with_sails_path.snap index 3e5401ba6..e24077ee3 100644 --- a/rs/client-gen/tests/snapshots/generator__full_with_sails_path.snap +++ b/rs/client-gen/tests/snapshots/generator__full_with_sails_path.snap @@ -7,18 +7,18 @@ expression: code use my_crate::sails::{client::*, collections::*, prelude::*}; pub struct FullCoverageProgramProgram; impl FullCoverageProgramProgram { - pub const CANVAS_ROUTE_ID: u8 = 1; - pub const PING_PONG_ROUTE_ID: u8 = 2; - pub const COUNTER_ROUTE_ID: u8 = 3; - pub const REFERENCES_ROUTE_ID: u8 = 4; - pub const THIS_THAT_ROUTE_ID: u8 = 5; - pub const VALUE_FEE_ROUTE_ID: u8 = 6; - pub const RMRK_CATALOG_ROUTE_ID: u8 = 7; - pub const NON_ZERO_PARAMS_ROUTE_ID: u8 = 8; - pub const SERVICE_WITH_EVENTS_ROUTE_ID: u8 = 9; - pub const BASIC_ROUTE_ID: u8 = 10; - pub const ANOTHER_SERVICE_ROUTE_ID: u8 = 11; - pub const MY_COMPLEX_SERVICE_ROUTE_ID: u8 = 12; + pub const ROUTE_ID_CANVAS: u8 = 1; + pub const ROUTE_ID_PING_PONG: u8 = 2; + pub const ROUTE_ID_COUNTER: u8 = 3; + pub const ROUTE_ID_REFERENCES: u8 = 4; + pub const ROUTE_ID_THIS_THAT: u8 = 5; + pub const ROUTE_ID_VALUE_FEE: u8 = 6; + pub const ROUTE_ID_RMRK_CATALOG: u8 = 7; + pub const ROUTE_ID_NON_ZERO_PARAMS: u8 = 8; + pub const ROUTE_ID_SERVICE_WITH_EVENTS: u8 = 9; + pub const ROUTE_ID_BASIC: u8 = 10; + pub const ROUTE_ID_ANOTHER_SERVICE: u8 = 11; + pub const ROUTE_ID_MY_COMPLEX_SERVICE: u8 = 12; } impl my_crate::sails::client::Program for FullCoverageProgramProgram {} pub trait FullCoverageProgram { @@ -64,53 +64,53 @@ impl FullCoverageProgram { type Env = E; fn canvas(&self) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::CANVAS_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_CANVAS) } fn ping_pong(&self) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::PING_PONG_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_PING_PONG) } fn counter(&self) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::COUNTER_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_COUNTER) } fn references( &self, ) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::REFERENCES_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_REFERENCES) } fn this_that(&self) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::THIS_THAT_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_THIS_THAT) } fn value_fee(&self) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::VALUE_FEE_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_VALUE_FEE) } fn rmrk_catalog( &self, ) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::RMRK_CATALOG_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_RMRK_CATALOG) } fn non_zero_params( &self, ) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::NON_ZERO_PARAMS_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_NON_ZERO_PARAMS) } fn service_with_events( &self, ) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::SERVICE_WITH_EVENTS_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_SERVICE_WITH_EVENTS) } fn basic(&self) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::BASIC_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_BASIC) } fn another_service( &self, ) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::ANOTHER_SERVICE_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_ANOTHER_SERVICE) } fn my_complex_service( &self, ) -> my_crate::sails::client::Service { - self.service(FullCoverageProgramProgram::MY_COMPLEX_SERVICE_ROUTE_ID) + self.service(FullCoverageProgramProgram::ROUTE_ID_MY_COMPLEX_SERVICE) } } pub trait FullCoverageProgramCtors { @@ -148,8 +148,8 @@ impl FullCoverageProgramCtors pub mod io { use super::*; - my_crate::sails::io_struct_impl!(Default () -> (), 0, my_crate::sails::meta::InterfaceId::zero()); - my_crate::sails::io_struct_impl!(WithOwner (owner: ActorId, initial_config: super::ProgramConfig) -> (), 1, my_crate::sails::meta::InterfaceId::zero()); + my_crate::sails::io_struct_impl!(Default () -> (), 0); + my_crate::sails::io_struct_impl!(WithOwner (owner: ActorId, initial_config: super::ProgramConfig) -> (), 1); } #[derive(PartialEq, Clone, Debug, Encode, Decode, TypeInfo, ReflectHash)] #[codec(crate = my_crate::sails::scale_codec)] @@ -324,6 +324,15 @@ pub mod canvas { #[codec(index = 2)] StatusChanged(Point), } + impl CanvasEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::E1 { .. } => 0, + Self::Jubilee { .. } => 1, + Self::StatusChanged { .. } => 2, + } + } + } impl my_crate::sails::client::Event for CanvasEvents {} impl my_crate::sails::client::Identifiable for CanvasEvents { const INTERFACE_ID: my_crate::sails::InterfaceId = @@ -419,6 +428,14 @@ pub mod counter { #[codec(index = 1)] Subtracted(u32), } + impl CounterEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::Added { .. } => 0, + Self::Subtracted { .. } => 1, + } + } + } impl my_crate::sails::client::Event for CounterEvents {} impl my_crate::sails::client::Identifiable for CounterEvents { const INTERFACE_ID: my_crate::sails::InterfaceId = @@ -488,6 +505,14 @@ pub mod dog { #[codec(index = 1)] Walked { from: (i32, i32), to: (i32, i32) }, } + impl DogEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::Barked { .. } => 0, + Self::Walked { .. } => 1, + } + } + } impl my_crate::sails::client::Event for DogEvents {} impl my_crate::sails::client::Identifiable for DogEvents { const INTERFACE_ID: my_crate::sails::InterfaceId = @@ -726,6 +751,13 @@ pub mod value_fee { #[codec(index = 0)] Withheld(u128), } + impl ValueFeeEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::Withheld { .. } => 0, + } + } + } impl my_crate::sails::client::Event for ValueFeeEvents {} impl my_crate::sails::client::Identifiable for ValueFeeEvents { const INTERFACE_ID: my_crate::sails::InterfaceId = @@ -784,6 +816,14 @@ pub mod pausable { #[codec(index = 1)] Unpaused, } + impl PausableEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::Paused { .. } => 0, + Self::Unpaused { .. } => 1, + } + } + } impl my_crate::sails::client::Event for PausableEvents {} impl my_crate::sails::client::Identifiable for PausableEvents { const INTERFACE_ID: my_crate::sails::InterfaceId = @@ -948,6 +988,14 @@ pub mod service_with_events { #[codec(index = 1)] Event2 { field1: String, field2: u64 }, } + impl ServiceWithEventsEvents { + pub fn entry_id(&self) -> u16 { + match self { + Self::Event1 { .. } => 0, + Self::Event2 { .. } => 1, + } + } + } impl my_crate::sails::client::Event for ServiceWithEventsEvents {} impl my_crate::sails::client::Identifiable for ServiceWithEventsEvents { const INTERFACE_ID: my_crate::sails::InterfaceId = diff --git a/rs/client-gen/tests/snapshots/generator__rmrk_works.snap b/rs/client-gen/tests/snapshots/generator__rmrk_works.snap index de2ad6344..dfd4a46c6 100644 --- a/rs/client-gen/tests/snapshots/generator__rmrk_works.snap +++ b/rs/client-gen/tests/snapshots/generator__rmrk_works.snap @@ -10,7 +10,7 @@ extern crate std; use sails_rs::{client::*, collections::*, prelude::*}; pub struct RmrkCatalogProgram; impl RmrkCatalogProgram { - pub const RMRK_CATALOG_SERVICE_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_RMRK_CATALOG_SERVICE: u8 = 1; } impl sails_rs::client::Program for RmrkCatalogProgram {} pub trait RmrkCatalog { @@ -24,7 +24,7 @@ impl RmrkCatalog for sails_rs::client::Actor sails_rs::client::Service { - self.service(RmrkCatalogProgram::RMRK_CATALOG_SERVICE_ROUTE_ID) + self.service(RmrkCatalogProgram::ROUTE_ID_RMRK_CATALOG_SERVICE) } } pub trait RmrkCatalogCtors { @@ -44,7 +44,7 @@ impl RmrkCatalogCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(New () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(New () -> (), 0); } pub mod rmrk_catalog_service { diff --git a/rs/client-gen/tests/snapshots/generator__scope_resolution.snap b/rs/client-gen/tests/snapshots/generator__scope_resolution.snap index 45ff84088..a50ca8c65 100644 --- a/rs/client-gen/tests/snapshots/generator__scope_resolution.snap +++ b/rs/client-gen/tests/snapshots/generator__scope_resolution.snap @@ -10,7 +10,7 @@ extern crate std; use sails_rs::{client::*, collections::*, prelude::*}; pub struct MyProgramProgram; impl MyProgramProgram { - pub const MY_SERVICE_ROUTE_ID: u8 = 1; + pub const ROUTE_ID_MY_SERVICE: u8 = 1; } impl sails_rs::client::Program for MyProgramProgram {} pub trait MyProgram { @@ -20,7 +20,7 @@ pub trait MyProgram { impl MyProgram for sails_rs::client::Actor { type Env = E; fn my_service(&self) -> sails_rs::client::Service { - self.service(MyProgramProgram::MY_SERVICE_ROUTE_ID) + self.service(MyProgramProgram::ROUTE_ID_MY_SERVICE) } } pub trait MyProgramCtors { @@ -38,7 +38,7 @@ impl MyProgramCtors pub mod io { use super::*; - sails_rs::io_struct_impl!(Init () -> (), 0, sails_rs::meta::InterfaceId::zero()); + sails_rs::io_struct_impl!(Init () -> (), 0); } #[derive(PartialEq, Clone, Debug, Encode, Decode, TypeInfo, ReflectHash)] #[codec(crate = sails_rs::scale_codec)] diff --git a/rs/src/client/mod.rs b/rs/src/client/mod.rs index 58d77d2ea..47a2b0619 100644 --- a/rs/src/client/mod.rs +++ b/rs/src/client/mod.rs @@ -335,43 +335,13 @@ impl PendingCtor { self.params = Some(f(self.params.unwrap_or_default())); self } - - pub fn encode_params(mut self) -> Vec { - let args = self - .args - .take() - .unwrap_or_else(|| panic!("{PENDING_CTOR_INVALID_STATE}")); - T::encode_params(&args) - } } -pub trait CallCodec { +pub trait ServiceCall: Identifiable { const ENTRY_ID: u16; type Params: Encode; type Reply: Decode + 'static; - fn encode_params(value: &Self::Params) -> Vec { - value.encode() - } - - fn decode_reply(payload: impl AsRef<[u8]>) -> Result { - let mut value = payload.as_ref(); - if Self::is_empty_tuple::() { - return Decode::decode(&mut value); - } - let header = SailsMessageHeader::decode(&mut value)?; - if header.entry_id() != Self::ENTRY_ID { - return Err("Invalid reply entry_id".into()); - } - Decode::decode(&mut value) - } - - fn is_empty_tuple() -> bool { - TypeId::of::() == TypeId::of::<()>() - } -} - -pub trait ServiceCall: CallCodec + Identifiable { fn encode_params_with_header(route_idx: u8, value: &Self::Params) -> Vec { let header = SailsMessageHeader::new( crate::meta::Version::v1(), @@ -426,6 +396,10 @@ pub trait ServiceCall: CallCodec + Identifiable { buffer_writer.with_buffer(f) }) } + + fn is_empty_tuple() -> bool { + TypeId::of::() == TypeId::of::<()>() + } } #[macro_export] @@ -526,10 +500,6 @@ macro_rules! io_struct_impl { ) => { pub struct $name(()); impl $name { - /// Encodes the parameters only (without Sails header). - pub fn encode_params($( $param: $ty, )* ) -> Vec { - <$name as CallCodec>::encode_params(&( $( $param, )* )) - } /// Encodes the full call with the correct Sails header (Interface ID + Route Index + Entry ID). pub fn encode_call(route_idx: u8, $( $param: $ty, )* ) -> Vec { <$name as ServiceCall>::encode_params_with_header(route_idx, &( $( $param, )* )) @@ -542,28 +512,16 @@ macro_rules! io_struct_impl { impl Identifiable for $name { const INTERFACE_ID: InterfaceId = $interface_id; } - impl CallCodec for $name { + impl ServiceCall for $name { const ENTRY_ID: u16 = $entry_id; type Params = ( $( $ty, )* ); type Reply = $reply; } - impl ServiceCall for $name {} }; ( $name:ident ( $( $param:ident : $ty:ty ),* ) -> $reply:ty, $entry_id:expr ) => { - pub struct $name(()); - impl $name { - /// Encodes the parameters only (without Sails header). - pub fn encode_params($( $param: $ty, )* ) -> Vec { - <$name as CallCodec>::encode_params(&( $( $param, )* )) - } - } - impl CallCodec for $name { - const ENTRY_ID: u16 = $entry_id; - type Params = ( $( $ty, )* ); - type Reply = $reply; - } + $crate::io_struct_impl!($name ( $( $param : $ty ),* ) -> $reply, $entry_id, $crate::meta::InterfaceId::zero()); }; } @@ -720,12 +678,20 @@ mod tests { let decoded = Add::decode_reply(route_idx, &reply_with_header).unwrap(); assert_eq!(decoded, 42); - // Add::encode_params should return raw bytes without header even for service methods - let add_params_only = Add::encode_params(42); - assert_eq!(add_params_only, expected_add_payload); + // Value is "Ctor" mode, it uses InterfaceId::zero() + let value_encoded = Value::encode_call(0); + let expected_header_value = [ + 0x47, 0x4D, 1, 16, // magic, version, hlen + 0, 0, 0, 0, 0, 0, 0, 0, // interface_id (zero) + 1, 0, // entry_id (1 for Value) + 0, 0, // route_id 0 and reserved 0 + ]; + assert_eq!(value_encoded, expected_header_value); - // Value is "Ctor" mode, encode takes no route_idx and returns raw bytes - let value_encoded = Value::encode_params(); - assert_eq!(value_encoded, Vec::::new()); + // Decode reply for Value + let mut value_reply = expected_header_value.to_vec(); + value_reply.extend_from_slice(&[123, 0, 0, 0]); // payload 123u32 + let decoded_value = Value::decode_reply(0, &value_reply).unwrap(); + assert_eq!(decoded_value, 123); } }