browser_commander/playwright/protocol/
json_pipe.rs1#![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#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
15pub struct JsonPipeInitializer {}
16
17#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
19pub struct JsonPipeSendParams {
20 #[serde(rename = "message")]
21 pub message: serde_json::Value,
22}
23
24#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
26pub struct JsonPipeMessageEventParams {
27 #[serde(rename = "message")]
28 pub message: serde_json::Value,
29}
30
31#[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#[derive(Debug, Clone, PartialEq)]
40pub enum JsonPipeEvent {
41 Message(JsonPipeMessageEventParams),
43 Closed(JsonPipeClosedEventParams),
45 Unknown {
47 method: String,
49 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#[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 pub async fn send(&self, params: JsonPipeSendParams) -> Result<(), ProtocolError> {
91 self.channel.send_no_result("send", ¶ms).await
92 }
93
94 pub async fn close(&self) -> Result<(), ProtocolError> {
96 self.channel.send_no_result("close", &serde_json::json!({})).await
97 }
98}