Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
Implement json metadata for outer events
Browse files Browse the repository at this point in the history
Progress on: #535
  • Loading branch information
bkchr committed Sep 6, 2018
1 parent 2f593df commit 1538807
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 29 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions substrate/runtime-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ substrate-codec = { path = "../codec", default_features = false }
[dev-dependencies]
pretty_assertions = "0.5.1"
serde_json = { version = "1.0" }
substrate-codec-derive = { path = "../../substrate/codec/derive" }

[features]
default = ["std"]
Expand Down
99 changes: 99 additions & 0 deletions substrate/runtime-support/src/event.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,99 @@
#[macro_export]
macro_rules! impl_outer_event {
($(#[$attr:meta])* pub enum $name:ident for $runtime:ident { $( $module:ident ),* }) => {
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
$(#[$attr])*
#[allow(non_camel_case_types)]
pub enum $name {
system(system::Event),
$(
$module($module::Event<$runtime>),
)*
}
impl From<system::Event> for $name {
fn from(x: system::Event) -> Self {
$name::system(x)
}
}
$(
impl From<$module::Event<$runtime>> for $name {
fn from(x: $module::Event<$runtime>) -> Self {
$name::$module(x)
}
}
)*
__impl_outer_event_json_metadata!($runtime; $name; $( $module )*);
}
}

#[macro_export]
#[doc(hidden)]
macro_rules! __impl_outer_event_json_metadata {
(
$runtime:ident;
$event_name:ident;
$( $module:ident )*
) => {
impl $runtime {
pub fn outer_event_json_metadata() -> &'static str {
concat!(r#"{ "name": ""#, stringify!($event_name), r#"", "items": { "#,
r#""system": "system::Event""#,
$(concat!(", \"", stringify!($module), r#"": ""#,
stringify!($module), "::Event<", stringify!($runtime), r#">""#),)*
" } }")
}
}
}
}

#[cfg(test)]
mod tests {
use serde;
use serde_json;

mod system {
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Deserialize, Serialize)]
pub struct Event;
}

mod event_module {
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Deserialize, Serialize)]
pub struct Event<T> {
t: T,
}
}

mod event_module2 {
#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Deserialize, Serialize)]
pub struct Event<T> {
t: T,
}
}

#[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Deserialize, Serialize)]
pub struct TestRuntime;

impl_outer_event! {
pub enum TestEvent for TestRuntime {
event_module, event_module2
}
}

const EXPECTED_METADATA: &str = concat!(
r#"{ "name": "TestEvent", "items": { "#,
r#""system": "system::Event", "#,
r#""event_module": "event_module::Event<TestRuntime>", "#,
r#""event_module2": "event_module2::Event<TestRuntime>" "#,
r#"} }"#
);

#[test]
fn outer_event_json_metadata() {
let metadata = TestRuntime::outer_event_json_metadata();
assert_eq!(EXPECTED_METADATA, metadata);
let _: serde::de::IgnoredAny =
serde_json::from_str(metadata).expect("Is valid json syntax");
}
}
34 changes: 5 additions & 29 deletions substrate/runtime-support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,9 @@ extern crate pretty_assertions;
extern crate serde_derive;
#[cfg(test)]
extern crate serde_json;
#[cfg(test)]
#[macro_use]
extern crate substrate_codec_derive;

#[doc(hidden)]
pub extern crate substrate_codec as codec;
Expand All @@ -42,6 +45,8 @@ pub use self::storage::generator::Storage as GenericStorage;
pub mod dispatch;
pub mod storage;
mod hashable;
#[macro_use]
mod event;

pub use self::storage::{StorageVec, StorageList, StorageValue, StorageMap};
pub use self::hashable::Hashable;
Expand Down Expand Up @@ -94,35 +99,6 @@ macro_rules! assert_ok {
}
}

#[macro_export]
macro_rules! impl_outer_event {
($(#[$attr:meta])* pub enum $name:ident for $trait:ident { $( $module:ident ),* }) => {
// Workaround for https://github.com/rust-lang/rust/issues/26925 . Remove when sorted.
#[derive(Clone, PartialEq, Eq, Encode, Decode)]
#[cfg_attr(feature = "std", derive(Debug, Serialize, Deserialize))]
$(#[$attr])*
#[allow(non_camel_case_types)]
pub enum $name {
system(system::Event),
$(
$module($module::Event<$trait>),
)*
}
impl From<system::Event> for $name {
fn from(x: system::Event) -> Self {
$name::system(x)
}
}
$(
impl From<$module::Event<$trait>> for $name {
fn from(x: $module::Event<$trait>) -> Self {
$name::$module(x)
}
}
)*
}
}

#[macro_export]
macro_rules! impl_outer_log {

Expand Down

0 comments on commit 1538807

Please sign in to comment.