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 `AndroidSocket`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct AndroidSocketInitializer {}

/// Parameters of `AndroidSocket.write`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct AndroidSocketWriteParams {
    #[serde(rename = "data")]
    pub data: Binary,
}

/// Payload of the `AndroidSocket.data` event.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct AndroidSocketDataEventParams {
    #[serde(rename = "data")]
    pub data: Binary,
}

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

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

impl ChannelType for AndroidSocket {
    const INTERFACE: &'static str = "AndroidSocket";
    type Initializer = AndroidSocketInitializer;
    type Event = AndroidSocketEvent;

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

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

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

impl AndroidSocket {
    /// `AndroidSocket.write` (internal).
    pub async fn write(&self, params: AndroidSocketWriteParams) -> Result<(), ProtocolError> {
        self.channel.send_no_result("write", &params).await
    }

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