Skip to main content

rustenium_bidi_definitions/script/
command_builders.rs

1use super::commands::*;
2impl AddPreloadScript {
3    pub fn builder() -> AddPreloadScriptBuilder {
4        <AddPreloadScriptBuilder as Default>::default()
5    }
6}
7#[derive(Default, Clone)]
8pub struct AddPreloadScriptBuilder {
9    function_declaration: Option<String>,
10    arguments: Option<Vec<super::types::ChannelValue>>,
11    contexts: Option<Vec<crate::browsing_context::types::BrowsingContext>>,
12    user_contexts: Option<Vec<crate::browser::types::UserContext>>,
13    sandbox: Option<String>,
14}
15impl AddPreloadScriptBuilder {
16    pub fn function_declaration(mut self, function_declaration: impl Into<String>) -> Self {
17        self.function_declaration = Some(function_declaration.into());
18        self
19    }
20    pub fn argument(mut self, argument: impl Into<super::types::ChannelValue>) -> Self {
21        let v = self.arguments.get_or_insert(Vec::new());
22        v.push(argument.into());
23        self
24    }
25    pub fn arguments<I, S>(mut self, arguments: I) -> Self
26    where
27        I: IntoIterator<Item = S>,
28        S: Into<super::types::ChannelValue>,
29    {
30        let v = self.arguments.get_or_insert(Vec::new());
31        for val in arguments {
32            v.push(val.into());
33        }
34        self
35    }
36    pub fn context(
37        mut self,
38        context: impl Into<crate::browsing_context::types::BrowsingContext>,
39    ) -> Self {
40        let v = self.contexts.get_or_insert(Vec::new());
41        v.push(context.into());
42        self
43    }
44    pub fn contexts<I, S>(mut self, contexts: I) -> Self
45    where
46        I: IntoIterator<Item = S>,
47        S: Into<crate::browsing_context::types::BrowsingContext>,
48    {
49        let v = self.contexts.get_or_insert(Vec::new());
50        for val in contexts {
51            v.push(val.into());
52        }
53        self
54    }
55    pub fn user_context(
56        mut self,
57        user_context: impl Into<crate::browser::types::UserContext>,
58    ) -> Self {
59        let v = self.user_contexts.get_or_insert(Vec::new());
60        v.push(user_context.into());
61        self
62    }
63    pub fn user_contexts<I, S>(mut self, user_contexts: I) -> Self
64    where
65        I: IntoIterator<Item = S>,
66        S: Into<crate::browser::types::UserContext>,
67    {
68        let v = self.user_contexts.get_or_insert(Vec::new());
69        for val in user_contexts {
70            v.push(val.into());
71        }
72        self
73    }
74    pub fn sandbox(mut self, sandbox: impl Into<String>) -> Self {
75        self.sandbox = Some(sandbox.into());
76        self
77    }
78    pub fn build(self) -> Result<AddPreloadScript, String> {
79        Ok(AddPreloadScript {
80            method: AddPreloadScriptMethod::AddPreloadScript,
81            params: AddPreloadScriptParams {
82                function_declaration: self.function_declaration.ok_or_else(|| {
83                    format!(
84                        "Field `{}` is mandatory.",
85                        std::stringify!(function_declaration)
86                    )
87                })?,
88                arguments: self.arguments,
89                contexts: self.contexts,
90                user_contexts: self.user_contexts,
91                sandbox: self.sandbox,
92            },
93        })
94    }
95}
96impl Disown {
97    pub fn builder() -> DisownBuilder {
98        <DisownBuilder as Default>::default()
99    }
100}
101#[derive(Default, Clone)]
102pub struct DisownBuilder {
103    handles: Option<Vec<super::types::Handle>>,
104    target: Option<super::types::Target>,
105}
106impl DisownBuilder {
107    pub fn handle(mut self, handle: impl Into<super::types::Handle>) -> Self {
108        let v = self.handles.get_or_insert(Vec::new());
109        v.push(handle.into());
110        self
111    }
112    pub fn handles<I, S>(mut self, handles: I) -> Self
113    where
114        I: IntoIterator<Item = S>,
115        S: Into<super::types::Handle>,
116    {
117        let v = self.handles.get_or_insert(Vec::new());
118        for val in handles {
119            v.push(val.into());
120        }
121        self
122    }
123    pub fn target(mut self, target: impl Into<super::types::Target>) -> Self {
124        self.target = Some(target.into());
125        self
126    }
127    pub fn build(self) -> Result<Disown, String> {
128        Ok(Disown {
129            method: DisownMethod::Disown,
130            params: DisownParams {
131                handles: self
132                    .handles
133                    .ok_or_else(|| format!("Field `{}` is mandatory.", std::stringify!(handles)))?,
134                target: self
135                    .target
136                    .ok_or_else(|| format!("Field `{}` is mandatory.", std::stringify!(target)))?,
137            },
138        })
139    }
140}
141impl CallFunction {
142    pub fn builder() -> CallFunctionBuilder {
143        <CallFunctionBuilder as Default>::default()
144    }
145}
146#[derive(Default, Clone)]
147pub struct CallFunctionBuilder {
148    function_declaration: Option<String>,
149    await_promise: Option<bool>,
150    target: Option<super::types::Target>,
151    arguments: Option<Vec<super::types::LocalValue>>,
152    result_ownership: Option<super::types::ResultOwnership>,
153    serialization_options: Option<super::types::SerializationOptions>,
154    this: Option<super::types::LocalValue>,
155    user_activation: Option<bool>,
156}
157impl CallFunctionBuilder {
158    pub fn function_declaration(mut self, function_declaration: impl Into<String>) -> Self {
159        self.function_declaration = Some(function_declaration.into());
160        self
161    }
162    pub fn await_promise(mut self, await_promise: impl Into<bool>) -> Self {
163        self.await_promise = Some(await_promise.into());
164        self
165    }
166    pub fn target(mut self, target: impl Into<super::types::Target>) -> Self {
167        self.target = Some(target.into());
168        self
169    }
170    pub fn argument(mut self, argument: impl Into<super::types::LocalValue>) -> Self {
171        let v = self.arguments.get_or_insert(Vec::new());
172        v.push(argument.into());
173        self
174    }
175    pub fn arguments<I, S>(mut self, arguments: I) -> Self
176    where
177        I: IntoIterator<Item = S>,
178        S: Into<super::types::LocalValue>,
179    {
180        let v = self.arguments.get_or_insert(Vec::new());
181        for val in arguments {
182            v.push(val.into());
183        }
184        self
185    }
186    pub fn result_ownership(
187        mut self,
188        result_ownership: impl Into<super::types::ResultOwnership>,
189    ) -> Self {
190        self.result_ownership = Some(result_ownership.into());
191        self
192    }
193    pub fn serialization_options(
194        mut self,
195        serialization_options: impl Into<super::types::SerializationOptions>,
196    ) -> Self {
197        self.serialization_options = Some(serialization_options.into());
198        self
199    }
200    pub fn this(mut self, this: impl Into<super::types::LocalValue>) -> Self {
201        self.this = Some(this.into());
202        self
203    }
204    pub fn user_activation(mut self, user_activation: impl Into<bool>) -> Self {
205        self.user_activation = Some(user_activation.into());
206        self
207    }
208    pub fn build(self) -> Result<CallFunction, String> {
209        Ok(CallFunction {
210            method: CallFunctionMethod::CallFunction,
211            params: CallFunctionParams {
212                function_declaration: self.function_declaration.ok_or_else(|| {
213                    format!(
214                        "Field `{}` is mandatory.",
215                        std::stringify!(function_declaration)
216                    )
217                })?,
218                await_promise: self.await_promise.ok_or_else(|| {
219                    format!("Field `{}` is mandatory.", std::stringify!(await_promise))
220                })?,
221                target: self
222                    .target
223                    .ok_or_else(|| format!("Field `{}` is mandatory.", std::stringify!(target)))?,
224                arguments: self.arguments,
225                result_ownership: self.result_ownership,
226                serialization_options: self.serialization_options,
227                this: self.this,
228                user_activation: self.user_activation,
229            },
230        })
231    }
232}
233impl Evaluate {
234    pub fn builder() -> EvaluateBuilder {
235        <EvaluateBuilder as Default>::default()
236    }
237}
238#[derive(Default, Clone)]
239pub struct EvaluateBuilder {
240    expression: Option<String>,
241    target: Option<super::types::Target>,
242    await_promise: Option<bool>,
243    result_ownership: Option<super::types::ResultOwnership>,
244    serialization_options: Option<super::types::SerializationOptions>,
245    user_activation: Option<bool>,
246}
247impl EvaluateBuilder {
248    pub fn expression(mut self, expression: impl Into<String>) -> Self {
249        self.expression = Some(expression.into());
250        self
251    }
252    pub fn target(mut self, target: impl Into<super::types::Target>) -> Self {
253        self.target = Some(target.into());
254        self
255    }
256    pub fn await_promise(mut self, await_promise: impl Into<bool>) -> Self {
257        self.await_promise = Some(await_promise.into());
258        self
259    }
260    pub fn result_ownership(
261        mut self,
262        result_ownership: impl Into<super::types::ResultOwnership>,
263    ) -> Self {
264        self.result_ownership = Some(result_ownership.into());
265        self
266    }
267    pub fn serialization_options(
268        mut self,
269        serialization_options: impl Into<super::types::SerializationOptions>,
270    ) -> Self {
271        self.serialization_options = Some(serialization_options.into());
272        self
273    }
274    pub fn user_activation(mut self, user_activation: impl Into<bool>) -> Self {
275        self.user_activation = Some(user_activation.into());
276        self
277    }
278    pub fn build(self) -> Result<Evaluate, String> {
279        Ok(Evaluate {
280            method: EvaluateMethod::Evaluate,
281            params: EvaluateParams {
282                expression: self.expression.ok_or_else(|| {
283                    format!("Field `{}` is mandatory.", std::stringify!(expression))
284                })?,
285                target: self
286                    .target
287                    .ok_or_else(|| format!("Field `{}` is mandatory.", std::stringify!(target)))?,
288                await_promise: self.await_promise.ok_or_else(|| {
289                    format!("Field `{}` is mandatory.", std::stringify!(await_promise))
290                })?,
291                result_ownership: self.result_ownership,
292                serialization_options: self.serialization_options,
293                user_activation: self.user_activation,
294            },
295        })
296    }
297}
298impl GetRealms {
299    pub fn builder() -> GetRealmsBuilder {
300        <GetRealmsBuilder as Default>::default()
301    }
302}
303#[derive(Default, Clone)]
304pub struct GetRealmsBuilder {
305    context: Option<crate::browsing_context::types::BrowsingContext>,
306    r#type: Option<super::types::RealmType>,
307}
308impl GetRealmsBuilder {
309    pub fn context(
310        mut self,
311        context: impl Into<crate::browsing_context::types::BrowsingContext>,
312    ) -> Self {
313        self.context = Some(context.into());
314        self
315    }
316    pub fn r#type(mut self, r#type: impl Into<super::types::RealmType>) -> Self {
317        self.r#type = Some(r#type.into());
318        self
319    }
320    pub fn build(self) -> GetRealms {
321        GetRealms {
322            method: GetRealmsMethod::GetRealms,
323            params: GetRealmsParams {
324                context: self.context,
325                r#type: self.r#type,
326            },
327        }
328    }
329}
330impl RemovePreloadScript {
331    pub fn builder() -> RemovePreloadScriptBuilder {
332        <RemovePreloadScriptBuilder as Default>::default()
333    }
334}
335#[derive(Default, Clone)]
336pub struct RemovePreloadScriptBuilder {
337    script: Option<super::types::PreloadScript>,
338}
339impl RemovePreloadScriptBuilder {
340    pub fn script(mut self, script: impl Into<super::types::PreloadScript>) -> Self {
341        self.script = Some(script.into());
342        self
343    }
344    pub fn build(self) -> Result<RemovePreloadScript, String> {
345        Ok(RemovePreloadScript {
346            method: RemovePreloadScriptMethod::RemovePreloadScript,
347            params: RemovePreloadScriptParams {
348                script: self
349                    .script
350                    .ok_or_else(|| format!("Field `{}` is mandatory.", std::stringify!(script)))?,
351            },
352        })
353    }
354}