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 `Request`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct RequestInitializer {
    #[serde(rename = "frame", default, skip_serializing_if = "Option::is_none")]
    pub frame: Option<Ref<Frame>>,
    #[serde(rename = "serviceWorker", default, skip_serializing_if = "Option::is_none")]
    pub service_worker: Option<Ref<Worker>>,
    #[serde(rename = "url")]
    pub url: String,
    #[serde(rename = "resourceType")]
    pub resource_type: String,
    #[serde(rename = "method")]
    pub method: String,
    #[serde(rename = "postData", default, skip_serializing_if = "Option::is_none")]
    pub post_data: Option<Binary>,
    #[serde(rename = "headers")]
    pub headers: Vec<NameValue>,
    #[serde(rename = "isNavigationRequest")]
    pub is_navigation_request: bool,
    #[serde(rename = "redirectedFrom", default, skip_serializing_if = "Option::is_none")]
    pub redirected_from: Option<Ref<Request>>,
}

/// Result of `Request.response`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct RequestResponseResult {
    #[serde(rename = "response", default, skip_serializing_if = "Option::is_none")]
    pub response: Option<Ref<Response>>,
}

/// Result of `Request.rawRequestHeaders`.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct RequestRawRequestHeadersResult {
    #[serde(rename = "headers")]
    pub headers: Vec<NameValue>,
}

/// Events a `Request` channel can emit.
#[derive(Debug, Clone, PartialEq)]
pub enum RequestEvent {
    /// 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 RequestEvent {
    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 `Request` interface (`network.yml`).
#[derive(Debug, Clone)]
pub struct Request {
    channel: Channel,
}

impl ChannelType for Request {
    const INTERFACE: &'static str = "Request";
    type Initializer = RequestInitializer;
    type Event = RequestEvent;

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

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

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

impl Request {
    /// `Request.response` (internal).
    pub async fn response(&self) -> Result<RequestResponseResult, ProtocolError> {
        self.channel.send("response", &serde_json::json!({})).await
    }

    /// `Request.rawRequestHeaders` (internal).
    pub async fn raw_request_headers(&self) -> Result<RequestRawRequestHeadersResult, ProtocolError> {
        self.channel.send("rawRequestHeaders", &serde_json::json!({})).await
    }
}