use std::{future::Future, future::IntoFuture, pin::Pin, time::Duration};
use super::{Action, ActionOptions, ActionReceipt, Destination, Expectation};
use crate::{Page, Result};
type ActionFuture<T> = Pin<Box<dyn Future<Output = Result<T>> + Send>>;
#[must_use = "actions do nothing until awaited"]
pub struct ActionCall {
page: Page,
action: Action,
options: ActionOptions,
}
impl ActionCall {
pub(crate) fn new(page: &Page, action: Action) -> Self {
Self {
page: page.clone(),
action,
options: page.action_options(),
}
}
pub fn expecting(mut self, expect: Expectation) -> Self {
self.options.expect = Some(expect);
self
}
pub fn timeout(mut self, timeout: Duration) -> Self {
self.options.timeout_ms = super::types::duration_to_ms(timeout);
self
}
pub fn destination(mut self, destination: Destination) -> Self {
self.options.destination = destination;
self
}
}
impl IntoFuture for ActionCall {
type Output = Result<ActionReceipt>;
type IntoFuture = ActionFuture<ActionReceipt>;
fn into_future(self) -> Self::IntoFuture {
Box::pin(async move { self.page.explore(self.action, self.options).await })
}
}