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,
document: Option<u64>,
}
impl ActionCall {
pub(crate) fn new(page: &Page, action: Action) -> Self {
Self {
page: page.clone(),
action,
options: page.action_options(),
document: None,
}
}
pub(crate) fn pinned(mut self, document: u64) -> Self {
self.document = Some(document);
self
}
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 {
match self.document {
Some(document) => {
self.page
.run_pinned(self.action, self.options, true, document)
.await
}
None => self.page.explore(self.action, self.options).await,
}
})
}
}