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

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DebuggerRunToParamsLocation {
    #[serde(rename = "file")]
    pub file: String,
    #[serde(rename = "line", default, skip_serializing_if = "Option::is_none")]
    pub line: Option<i64>,
    #[serde(rename = "column", default, skip_serializing_if = "Option::is_none")]
    pub column: Option<i64>,
}

/// Parameters of `Debugger.runTo` (Run to location).
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DebuggerRunToParams {
    #[serde(rename = "location")]
    pub location: DebuggerRunToParamsLocation,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DebuggerPausedStateChangedEventParamsPausedDetailsLocation {
    #[serde(rename = "file")]
    pub file: String,
    #[serde(rename = "line", default, skip_serializing_if = "Option::is_none")]
    pub line: Option<i64>,
    #[serde(rename = "column", default, skip_serializing_if = "Option::is_none")]
    pub column: Option<i64>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DebuggerPausedStateChangedEventParamsPausedDetails {
    #[serde(rename = "location")]
    pub location: DebuggerPausedStateChangedEventParamsPausedDetailsLocation,
    #[serde(rename = "title")]
    pub title: String,
    #[serde(rename = "stack", default, skip_serializing_if = "Option::is_none")]
    pub stack: Option<String>,
}

/// Payload of the `Debugger.pausedStateChanged` event.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DebuggerPausedStateChangedEventParams {
    #[serde(rename = "pausedDetails", default, skip_serializing_if = "Option::is_none")]
    pub paused_details: Option<DebuggerPausedStateChangedEventParamsPausedDetails>,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DebuggerApiCallsUpdatedEventParamsApiCallsLocation {
    #[serde(rename = "file")]
    pub file: String,
    #[serde(rename = "line", default, skip_serializing_if = "Option::is_none")]
    pub line: Option<i64>,
    #[serde(rename = "column", default, skip_serializing_if = "Option::is_none")]
    pub column: Option<i64>,
}

#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum DebuggerApiCallsUpdatedEventParamsApiCallsStatus {
    #[default]
    #[serde(rename = "running")]
    Running,
    #[serde(rename = "success")]
    Success,
    #[serde(rename = "error")]
    Error,
}

#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DebuggerApiCallsUpdatedEventParamsApiCalls {
    #[serde(rename = "id")]
    pub id: String,
    #[serde(rename = "title")]
    pub title: String,
    #[serde(rename = "location", default, skip_serializing_if = "Option::is_none")]
    pub location: Option<DebuggerApiCallsUpdatedEventParamsApiCallsLocation>,
    #[serde(rename = "newLogEntries")]
    pub new_log_entries: Vec<String>,
    #[serde(rename = "actionPoint", default, skip_serializing_if = "Option::is_none")]
    pub action_point: Option<Point>,
    #[serde(rename = "status")]
    pub status: DebuggerApiCallsUpdatedEventParamsApiCallsStatus,
    #[serde(rename = "error", default, skip_serializing_if = "Option::is_none")]
    pub error: Option<String>,
}

/// Payload of the `Debugger.apiCallsUpdated` event.
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct DebuggerApiCallsUpdatedEventParams {
    #[serde(rename = "apiCalls")]
    pub api_calls: Vec<DebuggerApiCallsUpdatedEventParamsApiCalls>,
}

/// Events a `Debugger` channel can emit.
#[derive(Debug, Clone, PartialEq)]
pub enum DebuggerEvent {
    /// `Debugger.pausedStateChanged`
    PausedStateChanged(DebuggerPausedStateChangedEventParams),
    /// `Debugger.apiCallsUpdated`
    ApiCallsUpdated(DebuggerApiCallsUpdatedEventParams),
    /// 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 DebuggerEvent {
    fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
        match method {
            "pausedStateChanged" => Ok(Self::PausedStateChanged(serde_json::from_value(params)?)),
            "apiCallsUpdated" => Ok(Self::ApiCallsUpdated(serde_json::from_value(params)?)),
            _ => Ok(Self::Unknown { method: method.to_string(), params }),
        }
    }
}

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

impl ChannelType for Debugger {
    const INTERFACE: &'static str = "Debugger";
    type Initializer = DebuggerInitializer;
    type Event = DebuggerEvent;

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

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

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

impl Debugger {
    /// `Debugger.requestPause` ("Pause on next call").
    pub async fn request_pause(&self) -> Result<(), ProtocolError> {
        self.channel.send_no_result("requestPause", &serde_json::json!({})).await
    }

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

    /// `Debugger.next` ("Step to next call").
    pub async fn next(&self) -> Result<(), ProtocolError> {
        self.channel.send_no_result("next", &serde_json::json!({})).await
    }

    /// `Debugger.runTo` ("Run to location").
    pub async fn run_to(&self, params: DebuggerRunToParams) -> Result<(), ProtocolError> {
        self.channel.send_no_result("runTo", &params).await
    }

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