browser_commander/playwright/protocol/
stream.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 StreamInitializer {}
16
17#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
19pub struct StreamReadParams {
20 #[serde(rename = "size", default, skip_serializing_if = "Option::is_none")]
21 pub size: Option<i64>,
22}
23
24#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
26pub struct StreamReadResult {
27 #[serde(rename = "binary")]
28 pub binary: Binary,
29}
30
31#[derive(Debug, Clone, PartialEq)]
33pub enum StreamEvent {
34 Unknown {
36 method: String,
38 params: serde_json::Value,
40 },
41}
42
43impl ProtocolEvent for StreamEvent {
44 fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
45 match method {
46 _ => Ok(Self::Unknown { method: method.to_string(), params }),
47 }
48 }
49}
50
51#[derive(Debug, Clone)]
53pub struct Stream {
54 channel: Channel,
55}
56
57impl ChannelType for Stream {
58 const INTERFACE: &'static str = "Stream";
59 type Initializer = StreamInitializer;
60 type Event = StreamEvent;
61
62 fn accepts(interface: &str) -> bool {
63 matches!(interface, "Stream")
64 }
65
66 fn from_channel(channel: Channel) -> Self {
67 Self { channel }
68 }
69
70 fn channel(&self) -> &Channel {
71 &self.channel
72 }
73}
74
75impl Stream {
76 pub async fn read(&self, params: StreamReadParams) -> Result<StreamReadResult, ProtocolError> {
78 self.channel.send("read", ¶ms).await
79 }
80
81 pub async fn close(&self) -> Result<(), ProtocolError> {
83 self.channel.send_no_result("close", &serde_json::json!({})).await
84 }
85}