Repository: oasisprotocol/oasis-sdk
Pull request #70 (2021-03-30 15:24:08)

Apply macros to keyvalue example
This PR does what it says on the tin. It also removes `sdk::error::Error::msg` because it's a non-standard way of writing `std::error::Error::to_string`.

codecov[bot]: # [Codecov](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70?src=pr&el=h1) Report
> Merging [#70](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70?src=pr&el=desc) (274fcc1) into [main](https://codecov.io/gh/oasisprotocol/oasis-sdk/commit/88cc7106686eeb0cd172b7ad3bdacb5cfae9de06?el=desc) (88cc710) will **decrease** coverage by `2.83%`.
> The diff coverage is `n/a`.

> :exclamation: Current head 274fcc1 differs from pull request most recent head b18b858. Consider uploading reports for the commit b18b858 to get more accurate results
[![Impacted file tree graph](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/graphs/tree.svg?width=650&height=150&src=pr&token=uscfoYjSZn)](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70?src=pr&el=tree)

```diff
@@            Coverage Diff             @@
##             main      #70      +/-   ##
==========================================
- Coverage   56.43%   53.60%   -2.84%     
==========================================
  Files          39        5      -34     
  Lines        1460      125    -1335     
==========================================
- Hits          824       67     -757     
+ Misses        622       44     -578     
  Partials       14       14              
```


| [Impacted Files](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70?src=pr&el=tree) | Coverage Δ | |
|---|---|---|
| [runtime-sdk-macros/src/version\_from\_cargo.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGstbWFjcm9zL3NyYy92ZXJzaW9uX2Zyb21fY2FyZ28ucnM=) | | |
| [runtime-sdk/src/types/transaction.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGsvc3JjL3R5cGVzL3RyYW5zYWN0aW9uLnJz) | | |
| [runtime-sdk/src/storage/prefix.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGsvc3JjL3N0b3JhZ2UvcHJlZml4LnJz) | | |
| [runtime-sdk/src/modules/accounts/types.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGsvc3JjL21vZHVsZXMvYWNjb3VudHMvdHlwZXMucnM=) | | |
| [runtime-sdk-macros/src/generators.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGstbWFjcm9zL3NyYy9nZW5lcmF0b3JzLnJz) | | |
| [runtime-sdk/src/modules/core/types.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGsvc3JjL21vZHVsZXMvY29yZS90eXBlcy5ycw==) | | |
| [runtime-sdk/src/crypto/signature/ed25519.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGsvc3JjL2NyeXB0by9zaWduYXR1cmUvZWQyNTUxOS5ycw==) | | |
| [runtime-sdk/src/storage/overlay.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGsvc3JjL3N0b3JhZ2Uvb3ZlcmxheS5ycw==) | | |
| [runtime-sdk/src/types/token.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGsvc3JjL3R5cGVzL3Rva2VuLnJz) | | |
| [runtime-sdk-macros/src/lib.rs](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree#diff-cnVudGltZS1zZGstbWFjcm9zL3NyYy9saWIucnM=) | | |
| ... and [19 more](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70/diff?src=pr&el=tree-more) | |

------

[Continue to review full report at Codecov](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70?src=pr&el=continue).
> **Legend** - [Click here to learn more](https://docs.codecov.io/docs/codecov-delta)
> `Δ = absolute <relative> (impact)`, `ø = not affected`, `? = missing data`
> Powered by [Codecov](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70?src=pr&el=footer). Last update [88cc710...b18b858](https://codecov.io/gh/oasisprotocol/oasis-sdk/pull/70?src=pr&el=lastupdated). Read the [comment docs](https://docs.codecov.io/docs/pull-request-comments).


Patch:
diff --git a/runtime-sdk-macros/src/error_derive.rs b/runtime-sdk-macros/src/error_derive.rs
index 113b73a59c..f1854ea473 100644
--- a/runtime-sdk-macros/src/error_derive.rs
+++ b/runtime-sdk-macros/src/error_derive.rs
@@ -78,7 +78,11 @@ pub fn derive_error(input: DeriveInput) -> TokenStream {
     };
 
     gen::wrap_in_const(quote! {
-        impl #sdk_crate::error::Error for #error_ty_ident {
+        use #sdk_crate::{
+            self as sdk, core::types::Error as RuntimeError, error::Error as _,
+        };
+
+        impl sdk::error::Error for #error_ty_ident {
             fn module(&self) -> &str {
                 #module_name
             }
@@ -87,6 +91,12 @@ pub fn derive_error(input: DeriveInput) -> TokenStream {
                 #code_converter
             }
         }
+
+        impl From<#error_ty_ident> for RuntimeError {
+            fn from(err: #error_ty_ident) -> RuntimeError {
+                RuntimeError::new(err.module(), err.code(), &err.to_string())
+            }
+        }
     })
 }
 
@@ -96,7 +106,10 @@ mod tests {
     fn generate_error_impl() {
         let expected: syn::Stmt = syn::parse_quote!(
             const _: () = {
-                impl ::oasis_runtime_sdk::error::Error for Error {
+                use oasis_runtime_sdk::{
+                    self as sdk, core::types::Error as RuntimeError, error::Error as _,
+                };
+                impl sdk::error::Error for Error {
                     fn module(&self) -> &str {
                         <module::TheModule as ::oasis_runtime_sdk::module::Module>::NAME
                     }
@@ -109,6 +122,11 @@ mod tests {
                         }
                     }
                 }
+                impl From<Error> for RuntimeError {
+                    fn from(err: Error) -> RuntimeError {
+                        RuntimeError::new(err.module(), err.code(), &err.to_string())
+                    }
+                }
             };
         );
 
diff --git a/runtime-sdk/src/error.rs b/runtime-sdk/src/error.rs
index 7b3332ae74..9f6c3a8a11 100644
--- a/runtime-sdk/src/error.rs
+++ b/runtime-sdk/src/error.rs
@@ -34,11 +34,6 @@ pub trait Error: std::error::Error {
     /// Error code uniquely identifying the error.
     fn code(&self) -> u32;
 
-    /// Error message.
-    fn msg(&self) -> String {
-        self.to_string()
-    }
-
     /// Converts the error into a call result.
     fn to_call_result(&self) -> CallResult {
         CallResult::Failed {
diff --git a/runtime-sdk/src/modules/accounts/mod.rs b/runtime-sdk/src/modules/accounts/mod.rs
index ba33b421ed..51af47f60b 100644
--- a/runtime-sdk/src/modules/accounts/mod.rs
+++ b/runtime-sdk/src/modules/accounts/mod.rs
@@ -46,12 +46,6 @@ pub enum Error {
     Forbidden,
 }
 
-impl From<Error> for error::RuntimeError {
-    fn from(err: Error) -> error::RuntimeError {
-        error::RuntimeError::new(err.module(), err.code(), &err.msg())
-    }
-}
-
 /// Events emitted by the accounts module.
 #[derive(Debug, Serialize, Deserialize, oasis_runtime_sdk_macros::Event)]
 #[serde(untagged)]
diff --git a/runtime-sdk/src/modules/core/mod.rs b/runtime-sdk/src/modules/core/mod.rs
index 6eb71a68fc..c72745f1a5 100644
--- a/runtime-sdk/src/modules/core/mod.rs
+++ b/runtime-sdk/src/modules/core/mod.rs
@@ -1,10 +1,7 @@
 //! Core definitions module.
 use thiserror::Error;
 
-use crate::{
-    error::{self, Error as _},
-    types::transaction,
-};
+use crate::types::transaction;
 
 pub mod types;
 
@@ -36,12 +33,6 @@ pub enum Error {
     InsufficientFeeBalance,
 }
 
-impl From<Error> for error::RuntimeError {
-    fn from(err: Error) -> error::RuntimeError {
-        error::RuntimeError::new(err.module(), err.code(), &err.msg())
-    }
-}
-
 /// State schema constants.
 pub mod state {
     /// Runtime metadata.


Test patch:
diff --git a/tests/runtimes/simple-keyvalue/src/keyvalue.rs b/tests/runtimes/simple-keyvalue/src/keyvalue.rs
index b0916fd966..31c5c6ccfb 100644
--- a/tests/runtimes/simple-keyvalue/src/keyvalue.rs
+++ b/tests/runtimes/simple-keyvalue/src/keyvalue.rs
@@ -16,53 +16,19 @@ pub mod types;
 const MODULE_NAME: &str = "keyvalue";
 
 /// Errors emitted by the keyvalue module.
-#[derive(Error, Debug)]
+#[derive(Error, Debug, sdk::Error)]
+#[sdk_error(module = "Module")]
 pub enum Error {
     #[error("invalid argument")]
+    #[sdk_error(code = 1)]
     InvalidArgument,
 }
 
-impl sdk::error::Error for Error {
-    fn module(&self) -> &str {
-        MODULE_NAME
-    }
-
-    fn code(&self) -> u32 {
-        match self {
-            Error::InvalidArgument => 1,
-        }
-    }
-}
-
-impl From<Error> for sdk::error::RuntimeError {
-    fn from(err: Error) -> sdk::error::RuntimeError {
-        sdk::error::RuntimeError::new(err.module(), err.code(), &err.msg())
-    }
-}
-
 /// Events emitted by the keyvalue module (none so far).
-#[derive(Debug, Serialize, Deserialize)]
+#[derive(Debug, Serialize, Deserialize, sdk::Event)]
 #[serde(untagged)]
-pub enum Event {
-    // XXX: What if we don't want to have any events?  The SDK seems to require an implementation anyways :/
-    DummyEvent = 1,
-}
-
-impl sdk::event::Event for Event {
-    fn module(&self) -> &str {
-        MODULE_NAME
-    }
-
-    fn code(&self) -> u32 {
-        match self {
-            Event::DummyEvent { .. } => 1,
-        }
-    }
-
-    fn value(&self) -> cbor::Value {
-        cbor::to_value(self)
-    }
-}
+#[sdk_event(module = "Module")]
+pub enum Event {}
 
 /// Parameters for the keyvalue module (none so far).
 #[derive(Clone, Debug, Default, Serialize, Deserialize)]