mirror of
https://github.com/pezkuwichain/pezkuwi-subxt.git
synced 2026-04-26 21:37:56 +00:00
Improve mock_impl_runtime_apis! (#7370)
* Improve `mock_impl_runtime_apis!` This adds a new attribute for functions being implemented in the `mock_impl_runtime_apis!` macro, the `advanced` attribute. When this attribute is given the user gets access to the `at` parameter and is able to return a `Result`, instead of letting the macro generate this stuff. * Use the `at_param_name` directly * Prevent clashing of `params`
This commit is contained in:
@@ -0,0 +1,20 @@
|
||||
use substrate_test_runtime_client::runtime::Block;
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
pub trait Api {
|
||||
fn test();
|
||||
}
|
||||
}
|
||||
|
||||
struct MockApi;
|
||||
|
||||
sp_api::mock_impl_runtime_apis! {
|
||||
impl Api<Block> for MockApi {
|
||||
#[advanced]
|
||||
fn test(&self, _: BlockId<Block>) -> Result<sp_core::NativeOrEncoded<()>, String> {
|
||||
Ok(().into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,13 @@
|
||||
error: `BlockId` needs to be taken by reference and not by value!
|
||||
--> $DIR/mock_advanced_block_id_by_value.rs:11:1
|
||||
|
|
||||
11 | / sp_api::mock_impl_runtime_apis! {
|
||||
12 | | impl Api<Block> for MockApi {
|
||||
13 | | #[advanced]
|
||||
14 | | fn test(&self, _: BlockId<Block>) -> Result<sp_core::NativeOrEncoded<()>, String> {
|
||||
... |
|
||||
17 | | }
|
||||
18 | | }
|
||||
| |_^
|
||||
|
|
||||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
@@ -0,0 +1,20 @@
|
||||
use substrate_test_runtime_client::runtime::Block;
|
||||
|
||||
sp_api::decl_runtime_apis! {
|
||||
pub trait Api {
|
||||
fn test();
|
||||
}
|
||||
}
|
||||
|
||||
struct MockApi;
|
||||
|
||||
sp_api::mock_impl_runtime_apis! {
|
||||
impl Api<Block> for MockApi {
|
||||
#[advanced]
|
||||
fn test(&self) -> Result<sp_core::NativeOrEncoded<()>, String> {
|
||||
Ok(().into())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {}
|
||||
@@ -0,0 +1,5 @@
|
||||
error: If using the `advanced` attribute, it is required that the function takes at least one argument, the `BlockId`.
|
||||
--> $DIR/mock_advanced_missing_blockid.rs:14:3
|
||||
|
|
||||
14 | fn test(&self) -> Result<sp_core::NativeOrEncoded<()>, String> {
|
||||
| ^^
|
||||
@@ -4,22 +4,22 @@ error: Error type can not change between runtime apis
|
||||
23 | type Error = u64;
|
||||
| ^^^^
|
||||
|
||||
error: First error type was declared here.
|
||||
--> $DIR/mock_only_one_error_type.rs:17:16
|
||||
|
|
||||
17 | type Error = u32;
|
||||
| ^^^
|
||||
|
||||
error[E0277]: the trait bound `u32: std::convert::From<std::string::String>` is not satisfied
|
||||
--> $DIR/mock_only_one_error_type.rs:15:1
|
||||
--> $DIR/mock_only_one_error_type.rs:17:16
|
||||
|
|
||||
15 | / sp_api::mock_impl_runtime_apis! {
|
||||
16 | | impl Api<Block> for MockApi {
|
||||
17 | | type Error = u32;
|
||||
18 | |
|
||||
... |
|
||||
26 | | }
|
||||
27 | | }
|
||||
| |_^ the trait `std::convert::From<std::string::String>` is not implemented for `u32`
|
||||
|
|
||||
::: $WORKSPACE/primitives/api/src/lib.rs:350:35
|
||||
17 | type Error = u32;
|
||||
| ^^^ the trait `std::convert::From<std::string::String>` is not implemented for `u32`
|
||||
|
|
||||
350 | type Error: std::fmt::Debug + From<String>;
|
||||
| ------------ required by this bound in `sp_api_hidden_includes_DECL_RUNTIME_APIS::sp_api::ApiErrorExt`
|
||||
::: $WORKSPACE/primitives/api/src/lib.rs
|
||||
|
|
||||
| type Error: std::fmt::Debug + From<String>;
|
||||
| ------------ required by this bound in `sp_api_hidden_includes_DECL_RUNTIME_APIS::sp_api::ApiErrorExt`
|
||||
|
|
||||
= help: the following implementations were found:
|
||||
<u32 as std::convert::From<bool>>
|
||||
@@ -27,4 +27,3 @@ error[E0277]: the trait bound `u32: std::convert::From<std::string::String>` is
|
||||
<u32 as std::convert::From<h2::frame::reason::Reason>>
|
||||
<u32 as std::convert::From<h2::frame::reason::Reason>>
|
||||
and 18 others
|
||||
= note: this error originates in a macro (in Nightly builds, run with -Z macro-backtrace for more info)
|
||||
|
||||
Reference in New Issue
Block a user