#![allow(clippy::all, missing_docs, unused_imports)]
use serde::{Deserialize, Serialize};
use super::*;
use crate::playwright::connection::{
Binary, Channel, ChannelType, ObjectRef, ProtocolError, ProtocolEvent, Ref,
};
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JsonPipeInitializer {}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JsonPipeSendParams {
#[serde(rename = "message")]
pub message: serde_json::Value,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JsonPipeMessageEventParams {
#[serde(rename = "message")]
pub message: serde_json::Value,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct JsonPipeClosedEventParams {
#[serde(rename = "reason", default, skip_serializing_if = "Option::is_none")]
pub reason: Option<String>,
}
#[derive(Debug, Clone, PartialEq)]
pub enum JsonPipeEvent {
Message(JsonPipeMessageEventParams),
Closed(JsonPipeClosedEventParams),
Unknown {
method: String,
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 }),
}
}
}
#[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 {
pub async fn send(&self, params: JsonPipeSendParams) -> Result<(), ProtocolError> {
self.channel.send_no_result("send", ¶ms).await
}
pub async fn close(&self) -> Result<(), ProtocolError> {
self.channel.send_no_result("close", &serde_json::json!({})).await
}
}