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

/// Parameters of `CDPSession.send` (Send CDP command).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct CDPSessionSendParams {
    #[serde(rename = "method")]
    pub method: String,
    #[serde(rename = "params", default, skip_serializing_if = "Option::is_none")]
    pub params: Option<serde_json::Value>,
}

/// Result of `CDPSession.send` (Send CDP command).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct CDPSessionSendResult {
    #[serde(rename = "result")]
    pub result: serde_json::Value,
}

/// Payload of the `CDPSession.event` event.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct CDPSessionEventEventParams {
    #[serde(rename = "method")]
    pub method: String,
    #[serde(rename = "params", default, skip_serializing_if = "Option::is_none")]
    pub params: Option<serde_json::Value>,
}

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

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

impl ChannelType for CDPSession {
    const INTERFACE: &'static str = "CDPSession";
    type Initializer = CDPSessionInitializer;
    type Event = CDPSessionEvent;

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

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

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

impl CDPSession {
    /// `CDPSession.send` ("Send CDP command").
    pub async fn send(&self, params: CDPSessionSendParams) -> Result<CDPSessionSendResult, ProtocolError> {
        self.channel.send("send", &params).await
    }

    /// `CDPSession.detach` ("Detach CDP session").
    pub async fn detach(&self) -> Result<(), ProtocolError> {
        self.channel.send_no_result("detach", &serde_json::json!({})).await
    }
}