eoka 0.17.0

Stealth browser automation for Rust. Puppeteer/Playwright alternative with anti-bot bypass.
Documentation
//! Per-call builders behind the shorthand input methods such as `Page::click`.

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>>;

/// One shorthand input action, run when awaited.
///
/// Without an expectation, a successful `.await` proves only that the input was
/// dispatched to an actionable target: the receipt's outcome is
/// [`OutcomeUnknown`](crate::ActionOutcome::OutcomeUnknown). Use
/// [`expecting`](Self::expecting) to prove the page reacted, or
/// [`Page::act`] to require that proof. Actions are never replayed after dispatch.
///
/// `press_key` has no target, so it succeeds even if nothing handles the key.
/// `type_text` fails unless an editable element has focus. Input that opens a
/// JavaScript dialog blocks until the deadline, and is then reported as an error.
#[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(),
        }
    }

    /// Require this condition to become true after dispatch, for this call only.
    /// It must not already hold, so that it proves a new result.
    pub fn expecting(mut self, expect: Expectation) -> Self {
        self.options.expect = Some(expect);
        self
    }

    /// Set the total deadline, including queueing and verification. It must be
    /// between 1ms and 24h; a sub-millisecond remainder rounds up to a whole
    /// millisecond, and zero is rejected with `invalid_timeout`.
    pub fn timeout(mut self, timeout: Duration) -> Self {
        self.options.timeout_ms = super::types::duration_to_ms(timeout);
        self
    }

    /// Set where the expectation may be observed. Does not change the input target.
    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 })
    }
}