Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feature] #4350: Expose event set bitfields in schema #4381

Merged
merged 3 commits into from
Mar 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading