diff --git a/Cargo.lock b/Cargo.lock index 37ca7481b6ef4..aa9131c76a32d 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2954,6 +2954,7 @@ dependencies = [ "serde_derive 1.0.70 (registry+https://github.com/rust-lang/crates.io-index)", "serde_json 1.0.24 (registry+https://github.com/rust-lang/crates.io-index)", "substrate-codec 0.1.0", + "substrate-codec-derive 0.1.0", "substrate-primitives 0.1.0", "substrate-runtime-io 0.1.0", "substrate-runtime-std 0.1.0", diff --git a/substrate/runtime-support/Cargo.toml b/substrate/runtime-support/Cargo.toml index d0c21056af722..ee8e524cf7133 100644 --- a/substrate/runtime-support/Cargo.toml +++ b/substrate/runtime-support/Cargo.toml @@ -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"] diff --git a/substrate/runtime-support/src/event.rs b/substrate/runtime-support/src/event.rs new file mode 100644 index 0000000000000..9d73ef966e3f8 --- /dev/null +++ b/substrate/runtime-support/src/event.rs @@ -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 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, + } + } + + mod event_module2 { + #[derive(Debug, Clone, PartialEq, Eq, Encode, Decode, Deserialize, Serialize)] + pub struct Event { + 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", "#, + r#""event_module2": "event_module2::Event" "#, + 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"); + } +} diff --git a/substrate/runtime-support/src/lib.rs b/substrate/runtime-support/src/lib.rs index 7bb4d94c3393e..d431d1a89aff9 100644 --- a/substrate/runtime-support/src/lib.rs +++ b/substrate/runtime-support/src/lib.rs @@ -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; @@ -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; @@ -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 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 {