1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
use std::convert::TryFrom;
use cap_sdk_core::transaction::{Event, IndefiniteEvent};
use ic_kit::Principal;
use super::{IntoEvent, TryFromEvent};
/// A Cap event with typed `details`.
///
/// This type implements [`TryFrom<Event>`]. This allows easy conversion
/// from an [`Event`] into this type, provided the [`Event`]'s details
/// are the correct type. Internally this uses [`TryFromDetails`], which
/// is why this type is bounded on `TryFromDetails + IntoDetails + Sized`.
///
/// Any structures that take [`Event`] use `impl Into<Event>`, so this structure
/// can be used interchangably with [`Event`].
///
/// # Examples
///
/// ### Creating a [`TypedEvent`] from an [`Event`]
///
/// ```rust
//# use std::convert::TryFrom;
//# use cap_sdk_core::transaction::{DetailValue, Event, EventStatus};
//# use ic_kit::Principal;
//# use crate::{IntoDetails, TryFromDetails, TypedEvent};
/// pub struct TransactionDetails {
/// foo: String,
/// bar: u64,
/// }
//#
//# impl TryFromDetails for TransactionDetails {
//# fn try_from_details(
//# details: &Vec<(String, cap_sdk_core::transaction::DetailValue)>,
//# ) -> Result<Self, ()> {
//# Ok(Self {
//# foo: String::from(
//# "foo",
//# ),
//# bar: 42,
//# })
//# }
//# }
//# impl IntoDetails for TransactionDetails {
//# fn into_details(self) -> Vec<(String, cap_sdk_core::transaction::DetailValue)> {
//# vec![]
//# }
//# }
///
/// // This is an example of the type of event that would be retrieved from
/// // a call to Cap. It has the required details to be cast into a typed
/// // event with our transaction details type.
/// let event = Event {
/// time: 0,
/// caller: Principal::anonymous(),
/// status: EventStatus::Completed,
/// operation: String::from("transfer"),
/// details: vec![
/// ("foo".to_owned(), DetailValue::Text("foo".to_owned())),
/// ("bar".to_owned(), DetailValue::U64(64))
/// ],
/// };
///
/// if let Ok(_typed_event) = TypedEvent::<TransactionDetails>::try_from(event) {
/// // ...
/// } else {
/// panic!("Failed to cast event to typed event.")
/// }
///
/// ```
#[derive(Clone)]
pub struct TypedEvent<T>
where
T: TryFromEvent + IntoEvent + Sized + Clone,
{
/// The timestamp in ms.
pub time: u64,
/// The caller that initiated the call on the token contract.
pub caller: Principal,
/// Details of the transaction.
pub details: T,
}
impl<T: IntoEvent + TryFromEvent + Clone> TypedEvent<T> {
/// The operation of the event
pub fn operation(&self) -> &'static str {
self.details.operation().unwrap_or("")
}
}
impl<T: TryFromEvent + IntoEvent + Clone> Into<Event> for TypedEvent<T> {
fn into(self) -> Event {
Event {
time: self.time,
caller: self.caller,
operation: self.details.operation().unwrap_or("").to_owned(),
details: self.details.details(),
}
}
}
impl<T: TryFromEvent + IntoEvent + Clone> TryFrom<Event> for TypedEvent<T> {
type Error = T::Error;
fn try_from(value: Event) -> Result<Self, Self::Error> {
Ok(Self {
time: value.time,
caller: value.caller,
details: T::try_from_event(value)?,
})
}
}
/// A typed indefinite event.
///
/// You can construct an [`IndefiniteEvent`] using a builder, then cast it to
/// a [`TypedIndefiniteEvent`] with [`TryInto`][std::convert::TryInto].
#[derive(Clone)]
pub struct TypedIndefiniteEvent<T>
where
T: TryFromEvent + IntoEvent + Sized + Clone,
{
/// The caller that initiated the call on the token contract.
pub caller: Principal,
/// Details of the transaction.
pub details: T,
}
impl<T: IntoEvent + TryFromEvent + Clone> TypedIndefiniteEvent<T> {
/// The operation of the event.
pub fn operation(&self) -> &'static str {
self.details.operation().unwrap_or("")
}
}
impl<T: TryFromEvent + IntoEvent + Clone> Into<IndefiniteEvent> for TypedIndefiniteEvent<T> {
fn into(self) -> IndefiniteEvent {
IndefiniteEvent {
caller: self.caller,
operation: self.details.operation().unwrap_or("").to_owned(),
details: self.details.details(),
}
}
}
impl<T: TryFromEvent + IntoEvent + Clone> TryFrom<IndefiniteEvent> for TypedIndefiniteEvent<T> {
type Error = T::Error;
fn try_from(value: IndefiniteEvent) -> Result<Self, Self::Error> {
Ok(Self {
caller: value.caller,
details: T::try_from_event(value)?,
})
}
}