use cap_sdk_core::transaction::{DetailValue, IndefiniteEvent};
use ic_kit::Principal;
use super::IntoEvent;
#[derive(Default, Clone)]
pub struct IndefiniteEventBuilder {
caller: Option<Principal>,
operation: Option<String>,
details: Vec<(String, DetailValue)>,
operation_from_event: bool,
}
impl IndefiniteEventBuilder {
pub fn new() -> Self {
Default::default()
}
#[inline(always)]
pub fn caller(mut self, caller: Principal) -> Self {
self.caller = Some(caller);
self
}
#[inline(always)]
pub fn operation(mut self, operation: impl Into<String>) -> Self {
if self.operation_from_event {
panic!("Tried to set operation after it was set from an `IntoEvent` type")
}
self.operation = Some(operation.into());
self
}
#[inline(always)]
pub fn details(mut self, details: impl IntoEvent) -> Self {
self.details.append(&mut details.details());
if self.operation_from_event {
panic!("Cannot combine two `IntoEvent` types with unique operations.")
}
if let Some(operation) = details.operation() {
self.operation_from_event = true;
self.operation = Some(operation.to_owned());
}
self
}
#[inline(always)]
pub fn build(self) -> Result<IndefiniteEvent, ()> {
Ok(IndefiniteEvent {
caller: self.caller.unwrap(),
operation: self.operation.unwrap(),
details: self.details,
})
}
}