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

/// Parameters of `JsonPipe.send`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JsonPipeSendParams {
    #[serde(rename = "message")]
    pub message: serde_json::Value,
}

/// Payload of the `JsonPipe.message` event.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JsonPipeMessageEventParams {
    #[serde(rename = "message")]
    pub message: serde_json::Value,
}

/// Payload of the `JsonPipe.closed` event.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JsonPipeClosedEventParams {
    #[serde(rename = "reason", default, skip_serializing_if = "Option::is_none")]
    pub reason: Option<String>,
}

/// Events a `JsonPipe` channel can emit.
#[derive(Debug, Clone, PartialEq)]
pub enum JsonPipeEvent {
    /// `JsonPipe.message`
    Message(JsonPipeMessageEventParams),
    /// `JsonPipe.closed`
    Closed(JsonPipeClosedEventParams),
    /// 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 JsonPipeEvent {
    fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
        match method {
            "message" => Ok(Self::Message(serde_json::from_value(params)?)),
            "closed" => Ok(Self::Closed(serde_json::from_value(params)?)),
            _ => Ok(Self::Unknown { method: method.to_string(), params }),
        }
    }
}

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

impl ChannelType for JsonPipe {
    const INTERFACE: &'static str = "JsonPipe";
    type Initializer = JsonPipeInitializer;
    type Event = JsonPipeEvent;

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

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

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

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

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