Skip to content

Commit

Permalink
[feature] #4350: Expose event set bitfields in schema (#4381)
Browse files Browse the repository at this point in the history
* [feature] #4350: Add BitmapMeta variant to schema
* [feature] #4350: Expose the EventSet bitfields in schema

Signed-off-by: Nikita Strygin <[email protected]>
  • Loading branch information
DCNick3 authored Mar 26, 2024
1 parent 0c49284 commit 1f0e21d
Show file tree
Hide file tree
Showing 6 changed files with 328 additions and 19 deletions.
41 changes: 36 additions & 5 deletions data_model/derive/src/event_set.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,26 @@ impl ToTokens for EventSetEnum {
variants,
} = self;

let flag_raw_values = variants
.iter()
.zip(0u32..)
.map(|(_, i)| quote!(1 << #i))
.collect::<Vec<_>>();

// definitions of consts for each event
let flag_defs = variants.iter().zip(0u32..).map(
let flag_defs = variants.iter().zip(flag_raw_values.iter()).map(
|(
EventSetVariant {
flag_ident,
event_ident,
..
},
i,
raw_value,
)| {
let doc = format!(" Matches [`{event_enum_ident}::{event_ident}`]");
quote! {
#[doc = #doc]
#vis const #flag_ident: Self = Self(1 << #i);
#vis const #flag_ident: Self = Self(#raw_value);
}
},
);
Expand Down Expand Up @@ -197,8 +203,7 @@ impl ToTokens for EventSetEnum {
// but it's the easiest way to make sure those traits are implemented
parity_scale_codec::Decode,
parity_scale_codec::Encode,
// TODO: we probably want to represent the bit values for each variant in the schema
iroha_schema::IntoSchema,
iroha_schema::TypeId,
)]
#[repr(transparent)]
#[doc = #doc]
Expand Down Expand Up @@ -380,6 +385,32 @@ impl ToTokens for EventSetEnum {
deserializer.deserialize_seq(Visitor)
}
}


impl iroha_schema::IntoSchema for #set_ident {
fn type_name() -> iroha_schema::Ident {
<Self as iroha_schema::TypeId>::id()
}

fn update_schema_map(metamap: &mut iroha_schema::MetaMap) {
if !metamap.contains_key::<Self>() {
if !metamap.contains_key::<u32>() {
<u32 as iroha_schema::IntoSchema>::update_schema_map(metamap);
}
metamap.insert::<Self>(iroha_schema::Metadata::Bitmap(iroha_schema::BitmapMeta {
repr: core::any::TypeId::of::<u32>(),
masks: vec![
#(
iroha_schema::BitmapMask {
name: String::from(#flag_names),
mask: #flag_raw_values,
},
)*
],
}));
}
}
}
})
}
}
Expand Down
9 changes: 7 additions & 2 deletions data_model/src/events/data/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -543,14 +543,19 @@ mod executor {
use iroha_data_model_derive::model;

pub use self::model::*;
// this is used in no_std
#[allow(unused)]
use super::*;

#[model]
pub mod model {
#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec::Vec};

use iroha_data_model_derive::EventSet;

// this is used in no_std
#[allow(unused)]
use super::*;

#[derive(
Debug,
Copy,
Expand Down
2 changes: 1 addition & 1 deletion data_model/src/events/data/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Data events.

#[cfg(not(feature = "std"))]
use alloc::{format, string::String, vec::Vec};
use alloc::{format, string::String, vec, vec::Vec};

pub use events::DataEvent;
pub use filters::DataEventFilter;
Expand Down
Loading

0 comments on commit 1f0e21d

Please sign in to comment.