browser-commander 0.17.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 `JSHandle`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleInitializer {
    #[serde(rename = "preview")]
    pub preview: String,
}

/// Parameters of `JSHandle.evaluateExpression` (Evaluate).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleEvaluateExpressionParams {
    #[serde(rename = "expression")]
    pub expression: String,
    #[serde(rename = "isFunction", default, skip_serializing_if = "Option::is_none")]
    pub is_function: Option<bool>,
    #[serde(rename = "arg")]
    pub arg: SerializedArgument,
}

/// Result of `JSHandle.evaluateExpression` (Evaluate).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleEvaluateExpressionResult {
    #[serde(rename = "value")]
    pub value: SerializedValue,
}

/// Parameters of `JSHandle.evaluateExpressionHandle` (Evaluate).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleEvaluateExpressionHandleParams {
    #[serde(rename = "expression")]
    pub expression: String,
    #[serde(rename = "isFunction", default, skip_serializing_if = "Option::is_none")]
    pub is_function: Option<bool>,
    #[serde(rename = "arg")]
    pub arg: SerializedArgument,
}

/// Result of `JSHandle.evaluateExpressionHandle` (Evaluate).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleEvaluateExpressionHandleResult {
    #[serde(rename = "handle")]
    pub handle: Ref<JSHandle>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleGetPropertyListResultProperties {
    #[serde(rename = "name")]
    pub name: String,
    #[serde(rename = "value")]
    pub value: Ref<JSHandle>,
}

/// Result of `JSHandle.getPropertyList` (Get property list).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleGetPropertyListResult {
    #[serde(rename = "properties")]
    pub properties: Vec<JSHandleGetPropertyListResultProperties>,
}

/// Parameters of `JSHandle.getProperty` (Get JS property).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleGetPropertyParams {
    #[serde(rename = "name")]
    pub name: String,
}

/// Result of `JSHandle.getProperty` (Get JS property).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleGetPropertyResult {
    #[serde(rename = "handle")]
    pub handle: Ref<JSHandle>,
}

/// Result of `JSHandle.jsonValue` (Get JSON value).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandleJsonValueResult {
    #[serde(rename = "value")]
    pub value: SerializedValue,
}

/// Payload of the `JSHandle.previewUpdated` event.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JSHandlePreviewUpdatedEventParams {
    #[serde(rename = "preview")]
    pub preview: String,
}

/// Events a `JSHandle` channel can emit.
#[derive(Debug, Clone, PartialEq)]
pub enum JSHandleEvent {
    /// `JSHandle.previewUpdated`
    PreviewUpdated(JSHandlePreviewUpdatedEventParams),
    /// 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 JSHandleEvent {
    fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
        match method {
            "previewUpdated" => Ok(Self::PreviewUpdated(serde_json::from_value(params)?)),
            _ => Ok(Self::Unknown { method: method.to_string(), params }),
        }
    }
}

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

impl ChannelType for JSHandle {
    const INTERFACE: &'static str = "JSHandle";
    type Initializer = JSHandleInitializer;
    type Event = JSHandleEvent;

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

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

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

impl JSHandle {
    /// `JSHandle.dispose` (internal).
    pub async fn dispose(&self) -> Result<(), ProtocolError> {
        self.channel.send_no_result("dispose", &serde_json::json!({})).await
    }

    /// `JSHandle.evaluateExpression` ("Evaluate").
    pub async fn evaluate_expression(&self, params: JSHandleEvaluateExpressionParams) -> Result<JSHandleEvaluateExpressionResult, ProtocolError> {
        self.channel.send("evaluateExpression", &params).await
    }

    /// `JSHandle.evaluateExpressionHandle` ("Evaluate").
    pub async fn evaluate_expression_handle(&self, params: JSHandleEvaluateExpressionHandleParams) -> Result<JSHandleEvaluateExpressionHandleResult, ProtocolError> {
        self.channel.send("evaluateExpressionHandle", &params).await
    }

    /// `JSHandle.getPropertyList` ("Get property list").
    pub async fn get_property_list(&self) -> Result<JSHandleGetPropertyListResult, ProtocolError> {
        self.channel.send("getPropertyList", &serde_json::json!({})).await
    }

    /// `JSHandle.getProperty` ("Get JS property").
    pub async fn get_property(&self, params: JSHandleGetPropertyParams) -> Result<JSHandleGetPropertyResult, ProtocolError> {
        self.channel.send("getProperty", &params).await
    }

    /// `JSHandle.jsonValue` ("Get JSON value").
    pub async fn json_value(&self) -> Result<JSHandleJsonValueResult, ProtocolError> {
        self.channel.send("jsonValue", &serde_json::json!({})).await
    }
}