browser-commander 0.18.0

Universal browser automation library that supports multiple browser engines with a unified API
Documentation
// @generated by scripts/generate-playwright-protocol.mjs from
// rust/protocol/playwright/*.yml. Do not edit by hand.

#![allow(clippy::all, missing_docs, unused_imports)]

use serde::{Deserialize, Serialize};

use super::*;
use crate::playwright::connection::{
    Binary, Channel, ChannelType, ObjectRef, ProtocolError, ProtocolEvent, Ref,
};

/// Initializer the driver sends when it creates a `Dialog`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DialogInitializer {
    #[serde(rename = "page", default, skip_serializing_if = "Option::is_none")]
    pub page: Option<Ref<Page>>,
    #[serde(rename = "type")]
    pub r#type: String,
    #[serde(rename = "message")]
    pub message: String,
    #[serde(rename = "defaultValue")]
    pub default_value: String,
}

/// Parameters of `Dialog.accept` (Accept dialog).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DialogAcceptParams {
    #[serde(rename = "promptText", default, skip_serializing_if = "Option::is_none")]
    pub prompt_text: Option<String>,
}

/// Events a `Dialog` channel can emit.
#[derive(Debug, Clone, PartialEq)]
pub enum DialogEvent {
    /// An event this binding does not know (a newer driver).
    Unknown {
        /// Event name.
        method: String,
        /// Raw event parameters.
        params: serde_json::Value,
    },
}

impl ProtocolEvent for DialogEvent {
    fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
        match method {
            _ => Ok(Self::Unknown { method: method.to_string(), params }),
        }
    }
}

/// Channel for the `Dialog` interface (`playwright.yml`).
#[derive(Debug, Clone)]
pub struct Dialog {
    channel: Channel,
}

impl ChannelType for Dialog {
    const INTERFACE: &'static str = "Dialog";
    type Initializer = DialogInitializer;
    type Event = DialogEvent;

    fn accepts(interface: &str) -> bool {
        matches!(interface, "Dialog")
    }

    fn from_channel(channel: Channel) -> Self {
        Self { channel }
    }

    fn channel(&self) -> &Channel {
        &self.channel
    }
}

impl Dialog {
    /// `Dialog.accept` ("Accept dialog").
    pub async fn accept(&self, params: DialogAcceptParams) -> Result<(), ProtocolError> {
        self.channel.send_no_result("accept", &params).await
    }

    /// `Dialog.dismiss` ("Dismiss dialog").
    pub async fn dismiss(&self) -> Result<(), ProtocolError> {
        self.channel.send_no_result("dismiss", &serde_json::json!({})).await
    }
}