use crate::{
config::{Config, HashFor},
error::EventsError,
Metadata,
};
use derive_where::derive_where;
use pezkuwi_subxt_core::events::{EventDetails as CoreEventDetails, Events as CoreEvents};
use scale_decode::{DecodeAsFields, DecodeAsType};
pub use pezkuwi_subxt_core::events::{EventMetadataDetails, Phase, StaticEvent};
#[derive_where(Clone, Debug)]
pub struct Events<T> {
inner: CoreEvents<T>,
}
impl<T: Config> Events<T> {
pub fn decode_from(event_bytes: Vec<u8>, metadata: Metadata) -> Self {
Self { inner: CoreEvents::decode_from(event_bytes, metadata) }
}
pub fn len(&self) -> u32 {
self.inner.len()
}
pub fn is_empty(&self) -> bool {
self.inner.is_empty()
}
pub fn bytes(&self) -> &[u8] {
self.inner.bytes()
}
pub fn iter(
&self,
) -> impl Iterator<Item = Result<EventDetails<T>, EventsError>> + Send + Sync + 'static {
self.inner
.iter()
.map(|item| item.map(|e| EventDetails { inner: e }).map_err(Into::into))
}
pub fn find<Ev: StaticEvent>(&self) -> impl Iterator<Item = Result<Ev, EventsError>> {
self.inner.find::<Ev>().map(|item| item.map_err(Into::into))
}
pub fn find_first<Ev: StaticEvent>(&self) -> Result<Option<Ev>, EventsError> {
self.inner.find_first::<Ev>().map_err(Into::into)
}
pub fn find_last<Ev: StaticEvent>(&self) -> Result<Option<Ev>, EventsError> {
self.inner.find_last::<Ev>().map_err(Into::into)
}
pub fn has<Ev: StaticEvent>(&self) -> Result<bool, EventsError> {
self.inner.has::<Ev>().map_err(Into::into)
}
}
#[derive(Debug, Clone)]
pub struct EventDetails<T: Config> {
inner: CoreEventDetails<T>,
}
impl<T: Config> EventDetails<T> {
pub fn phase(&self) -> Phase {
self.inner.phase()
}
pub fn index(&self) -> u32 {
self.inner.index()
}
pub fn pallet_index(&self) -> u8 {
self.inner.pallet_index()
}
pub fn variant_index(&self) -> u8 {
self.inner.variant_index()
}
pub fn pallet_name(&self) -> &str {
self.inner.pallet_name()
}
pub fn pezpallet_name(&self) -> &str {
self.pallet_name()
}
pub fn variant_name(&self) -> &str {
self.inner.variant_name()
}
pub fn event_metadata(&self) -> EventMetadataDetails<'_> {
self.inner.event_metadata()
}
pub fn bytes(&self) -> &[u8] {
self.inner.bytes()
}
pub fn field_bytes(&self) -> &[u8] {
self.inner.field_bytes()
}
pub fn decode_as_fields<E: DecodeAsFields>(&self) -> Result<E, EventsError> {
self.inner.decode_as_fields().map_err(Into::into)
}
pub fn as_event<E: StaticEvent>(&self) -> Result<Option<E>, EventsError> {
self.inner.as_event::<E>().map_err(Into::into)
}
pub fn as_root_event<E: DecodeAsType>(&self) -> Result<E, EventsError> {
self.inner.as_root_event::<E>().map_err(Into::into)
}
pub fn topics(&self) -> &[HashFor<T>] {
self.inner.topics()
}
}