Skip to main content

browser_commander/playwright/protocol/
json_pipe.rs

1// @generated by scripts/generate-playwright-protocol.mjs from
2// rust/protocol/playwright/*.yml. Do not edit by hand.
3
4#![allow(clippy::all, missing_docs, unused_imports)]
5
6use serde::{Deserialize, Serialize};
7
8use super::*;
9use crate::playwright::connection::{
10    Binary, Channel, ChannelType, ObjectRef, ProtocolError, ProtocolEvent, Ref,
11};
12
13/// Initializer the driver sends when it creates a `JsonPipe`.
14#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
15pub struct JsonPipeInitializer {}
16
17/// Parameters of `JsonPipe.send`.
18#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
19pub struct JsonPipeSendParams {
20    #[serde(rename = "message")]
21    pub message: serde_json::Value,
22}
23
24/// Payload of the `JsonPipe.message` event.
25#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
26pub struct JsonPipeMessageEventParams {
27    #[serde(rename = "message")]
28    pub message: serde_json::Value,
29}
30
31/// Payload of the `JsonPipe.closed` event.
32#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
33pub struct JsonPipeClosedEventParams {
34    #[serde(rename = "reason", default, skip_serializing_if = "Option::is_none")]
35    pub reason: Option<String>,
36}
37
38/// Events a `JsonPipe` channel can emit.
39#[derive(Debug, Clone, PartialEq)]
40pub enum JsonPipeEvent {
41    /// `JsonPipe.message`
42    Message(JsonPipeMessageEventParams),
43    /// `JsonPipe.closed`
44    Closed(JsonPipeClosedEventParams),
45    /// An event this binding does not know (a newer driver).
46    Unknown {
47        /// Event name.
48        method: String,
49        /// Raw event parameters.
50        params: serde_json::Value,
51    },
52}
53
54impl ProtocolEvent for JsonPipeEvent {
55    fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
56        match method {
57            "message" => Ok(Self::Message(serde_json::from_value(params)?)),
58            "closed" => Ok(Self::Closed(serde_json::from_value(params)?)),
59            _ => Ok(Self::Unknown { method: method.to_string(), params }),
60        }
61    }
62}
63
64/// Channel for the `JsonPipe` interface (`playwright.yml`).
65#[derive(Debug, Clone)]
66pub struct JsonPipe {
67    channel: Channel,
68}
69
70impl ChannelType for JsonPipe {
71    const INTERFACE: &'static str = "JsonPipe";
72    type Initializer = JsonPipeInitializer;
73    type Event = JsonPipeEvent;
74
75    fn accepts(interface: &str) -> bool {
76        matches!(interface, "JsonPipe")
77    }
78
79    fn from_channel(channel: Channel) -> Self {
80        Self { channel }
81    }
82
83    fn channel(&self) -> &Channel {
84        &self.channel
85    }
86}
87
88impl JsonPipe {
89    /// `JsonPipe.send` (internal).
90    pub async fn send(&self, params: JsonPipeSendParams) -> Result<(), ProtocolError> {
91        self.channel.send_no_result("send", &params).await
92    }
93
94    /// `JsonPipe.close` (internal).
95    pub async fn close(&self) -> Result<(), ProtocolError> {
96        self.channel.send_no_result("close", &serde_json::json!({})).await
97    }
98}