browser_commander/playwright/protocol/
worker.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 WorkerInitializer {
16 #[serde(rename = "url")]
17 pub url: String,
18}
19
20#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
22pub struct WorkerDisconnectParams {
23 #[serde(rename = "reason", default, skip_serializing_if = "Option::is_none")]
24 pub reason: Option<String>,
25}
26
27#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
29pub struct WorkerEvaluateExpressionParams {
30 #[serde(rename = "expression")]
31 pub expression: String,
32 #[serde(rename = "isFunction", default, skip_serializing_if = "Option::is_none")]
33 pub is_function: Option<bool>,
34 #[serde(rename = "arg")]
35 pub arg: SerializedArgument,
36}
37
38#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
40pub struct WorkerEvaluateExpressionResult {
41 #[serde(rename = "value")]
42 pub value: SerializedValue,
43}
44
45#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
47pub struct WorkerEvaluateExpressionHandleParams {
48 #[serde(rename = "expression")]
49 pub expression: String,
50 #[serde(rename = "isFunction", default, skip_serializing_if = "Option::is_none")]
51 pub is_function: Option<bool>,
52 #[serde(rename = "arg")]
53 pub arg: SerializedArgument,
54}
55
56#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
58pub struct WorkerEvaluateExpressionHandleResult {
59 #[serde(rename = "handle")]
60 pub handle: Ref<JSHandle>,
61}
62
63#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash, Serialize, Deserialize)]
64pub enum WorkerUpdateSubscriptionParamsEvent {
65 #[default]
66 #[serde(rename = "console")]
67 Console,
68}
69
70#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
72pub struct WorkerUpdateSubscriptionParams {
73 #[serde(rename = "event")]
74 pub event: WorkerUpdateSubscriptionParamsEvent,
75 #[serde(rename = "enabled")]
76 pub enabled: bool,
77}
78
79#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
81pub struct WorkerConsoleEventParams {
82 #[serde(flatten)]
84 pub console_message: ConsoleMessage,
85}
86
87#[derive(Debug, Clone, PartialEq)]
89pub enum WorkerEvent {
90 Console(WorkerConsoleEventParams),
92 Close,
94 Unknown {
96 method: String,
98 params: serde_json::Value,
100 },
101}
102
103impl ProtocolEvent for WorkerEvent {
104 fn parse(method: &str, params: serde_json::Value) -> Result<Self, serde_json::Error> {
105 match method {
106 "console" => Ok(Self::Console(serde_json::from_value(params)?)),
107 "close" => Ok(Self::Close),
108 _ => Ok(Self::Unknown { method: method.to_string(), params }),
109 }
110 }
111}
112
113#[derive(Debug, Clone)]
115pub struct Worker {
116 channel: Channel,
117}
118
119impl ChannelType for Worker {
120 const INTERFACE: &'static str = "Worker";
121 type Initializer = WorkerInitializer;
122 type Event = WorkerEvent;
123
124 fn accepts(interface: &str) -> bool {
125 matches!(interface, "Worker")
126 }
127
128 fn from_channel(channel: Channel) -> Self {
129 Self { channel }
130 }
131
132 fn channel(&self) -> &Channel {
133 &self.channel
134 }
135}
136
137impl Worker {
138 pub async fn disconnect(&self, params: WorkerDisconnectParams) -> Result<(), ProtocolError> {
140 self.channel.send_no_result("disconnect", ¶ms).await
141 }
142
143 pub async fn evaluate_expression(&self, params: WorkerEvaluateExpressionParams) -> Result<WorkerEvaluateExpressionResult, ProtocolError> {
145 self.channel.send("evaluateExpression", ¶ms).await
146 }
147
148 pub async fn evaluate_expression_handle(&self, params: WorkerEvaluateExpressionHandleParams) -> Result<WorkerEvaluateExpressionHandleResult, ProtocolError> {
150 self.channel.send("evaluateExpressionHandle", ¶ms).await
151 }
152
153 pub async fn update_subscription(&self, params: WorkerUpdateSubscriptionParams) -> Result<(), ProtocolError> {
155 self.channel.send_no_result("updateSubscription", ¶ms).await
156 }
157}