Skip to main content

browser_protocol/io/
mod.rs

1//! Input/Output operations for streams produced by DevTools.
2
3
4use serde::{Serialize, Deserialize};
5use serde_json::Value as JsonValue;
6use std::borrow::Cow;
7
8/// This is either obtained from another method or specified as 'blob:\<uuid\>' where
9/// '\<uuid\>' is an UUID of a Blob.
10
11pub type StreamHandle<'a> = Cow<'a, str>;
12
13/// Close the stream, discard any temporary backing storage.
14
15#[derive(Debug, Clone, Serialize, Deserialize, Default)]
16#[serde(rename_all = "camelCase")]
17pub struct CloseParams<'a> {
18    /// Handle of the stream to close.
19    handle: StreamHandle<'a>,
20}
21
22impl<'a> CloseParams<'a> {
23    /// Creates a builder for this type with the required parameters:
24    /// * `handle`: Handle of the stream to close.
25    pub fn builder(handle: impl Into<StreamHandle<'a>>) -> CloseParamsBuilder<'a> {
26        CloseParamsBuilder {
27            handle: handle.into(),
28        }
29    }
30    /// Handle of the stream to close.
31    pub fn handle(&self) -> &StreamHandle<'a> { &self.handle }
32}
33
34
35pub struct CloseParamsBuilder<'a> {
36    handle: StreamHandle<'a>,
37}
38
39impl<'a> CloseParamsBuilder<'a> {
40    pub fn build(self) -> CloseParams<'a> {
41        CloseParams {
42            handle: self.handle,
43        }
44    }
45}
46
47impl<'a> CloseParams<'a> { pub const METHOD: &'static str = "IO.close"; }
48
49impl<'a> crate::CdpCommand<'a> for CloseParams<'a> {
50    const METHOD: &'static str = "IO.close";
51    type Response = crate::EmptyReturns;
52}
53
54/// Read a chunk of the stream
55
56#[derive(Debug, Clone, Serialize, Deserialize, Default)]
57#[serde(rename_all = "camelCase")]
58pub struct ReadParams<'a> {
59    /// Handle of the stream to read.
60    handle: StreamHandle<'a>,
61    /// Seek to the specified offset before reading (if not specified, proceed with offset
62    /// following the last read). Some types of streams may only support sequential reads.
63    #[serde(skip_serializing_if = "Option::is_none")]
64    offset: Option<i32>,
65    /// Maximum number of bytes to read (left upon the agent discretion if not specified).
66    #[serde(skip_serializing_if = "Option::is_none")]
67    size: Option<u64>,
68}
69
70impl<'a> ReadParams<'a> {
71    /// Creates a builder for this type with the required parameters:
72    /// * `handle`: Handle of the stream to read.
73    pub fn builder(handle: impl Into<StreamHandle<'a>>) -> ReadParamsBuilder<'a> {
74        ReadParamsBuilder {
75            handle: handle.into(),
76            offset: None,
77            size: None,
78        }
79    }
80    /// Handle of the stream to read.
81    pub fn handle(&self) -> &StreamHandle<'a> { &self.handle }
82    /// Seek to the specified offset before reading (if not specified, proceed with offset
83    /// following the last read). Some types of streams may only support sequential reads.
84    pub fn offset(&self) -> Option<i32> { self.offset }
85    /// Maximum number of bytes to read (left upon the agent discretion if not specified).
86    pub fn size(&self) -> Option<u64> { self.size }
87}
88
89
90pub struct ReadParamsBuilder<'a> {
91    handle: StreamHandle<'a>,
92    offset: Option<i32>,
93    size: Option<u64>,
94}
95
96impl<'a> ReadParamsBuilder<'a> {
97    /// Seek to the specified offset before reading (if not specified, proceed with offset
98    /// following the last read). Some types of streams may only support sequential reads.
99    pub fn offset(mut self, offset: i32) -> Self { self.offset = Some(offset); self }
100    /// Maximum number of bytes to read (left upon the agent discretion if not specified).
101    pub fn size(mut self, size: u64) -> Self { self.size = Some(size); self }
102    pub fn build(self) -> ReadParams<'a> {
103        ReadParams {
104            handle: self.handle,
105            offset: self.offset,
106            size: self.size,
107        }
108    }
109}
110
111/// Read a chunk of the stream
112
113#[derive(Debug, Clone, Serialize, Deserialize, Default)]
114#[serde(rename_all = "camelCase")]
115pub struct ReadReturns<'a> {
116    /// Set if the data is base64-encoded
117    #[serde(skip_serializing_if = "Option::is_none", rename = "base64Encoded")]
118    base64_encoded: Option<bool>,
119    /// Data that were read.
120    data: Cow<'a, str>,
121    /// Set if the end-of-file condition occurred while reading.
122    eof: bool,
123}
124
125impl<'a> ReadReturns<'a> {
126    /// Creates a builder for this type with the required parameters:
127    /// * `data`: Data that were read.
128    /// * `eof`: Set if the end-of-file condition occurred while reading.
129    pub fn builder(data: impl Into<Cow<'a, str>>, eof: bool) -> ReadReturnsBuilder<'a> {
130        ReadReturnsBuilder {
131            base64_encoded: None,
132            data: data.into(),
133            eof: eof,
134        }
135    }
136    /// Set if the data is base64-encoded
137    pub fn base64_encoded(&self) -> Option<bool> { self.base64_encoded }
138    /// Data that were read.
139    pub fn data(&self) -> &str { self.data.as_ref() }
140    /// Set if the end-of-file condition occurred while reading.
141    pub fn eof(&self) -> bool { self.eof }
142}
143
144
145pub struct ReadReturnsBuilder<'a> {
146    base64_encoded: Option<bool>,
147    data: Cow<'a, str>,
148    eof: bool,
149}
150
151impl<'a> ReadReturnsBuilder<'a> {
152    /// Set if the data is base64-encoded
153    pub fn base64_encoded(mut self, base64_encoded: bool) -> Self { self.base64_encoded = Some(base64_encoded); self }
154    pub fn build(self) -> ReadReturns<'a> {
155        ReadReturns {
156            base64_encoded: self.base64_encoded,
157            data: self.data,
158            eof: self.eof,
159        }
160    }
161}
162
163impl<'a> ReadParams<'a> { pub const METHOD: &'static str = "IO.read"; }
164
165impl<'a> crate::CdpCommand<'a> for ReadParams<'a> {
166    const METHOD: &'static str = "IO.read";
167    type Response = ReadReturns<'a>;
168}
169
170/// Return UUID of Blob object specified by a remote object id.
171
172#[derive(Debug, Clone, Serialize, Deserialize, Default)]
173#[serde(rename_all = "camelCase")]
174pub struct ResolveBlobParams<'a> {
175    /// Object id of a Blob object wrapper.
176    #[serde(rename = "objectId")]
177    object_id: crate::runtime::RemoteObjectId<'a>,
178}
179
180impl<'a> ResolveBlobParams<'a> {
181    /// Creates a builder for this type with the required parameters:
182    /// * `object_id`: Object id of a Blob object wrapper.
183    pub fn builder(object_id: crate::runtime::RemoteObjectId<'a>) -> ResolveBlobParamsBuilder<'a> {
184        ResolveBlobParamsBuilder {
185            object_id: object_id,
186        }
187    }
188    /// Object id of a Blob object wrapper.
189    pub fn object_id(&self) -> &crate::runtime::RemoteObjectId<'a> { &self.object_id }
190}
191
192
193pub struct ResolveBlobParamsBuilder<'a> {
194    object_id: crate::runtime::RemoteObjectId<'a>,
195}
196
197impl<'a> ResolveBlobParamsBuilder<'a> {
198    pub fn build(self) -> ResolveBlobParams<'a> {
199        ResolveBlobParams {
200            object_id: self.object_id,
201        }
202    }
203}
204
205/// Return UUID of Blob object specified by a remote object id.
206
207#[derive(Debug, Clone, Serialize, Deserialize, Default)]
208#[serde(rename_all = "camelCase")]
209pub struct ResolveBlobReturns<'a> {
210    /// UUID of the specified Blob.
211    uuid: Cow<'a, str>,
212}
213
214impl<'a> ResolveBlobReturns<'a> {
215    /// Creates a builder for this type with the required parameters:
216    /// * `uuid`: UUID of the specified Blob.
217    pub fn builder(uuid: impl Into<Cow<'a, str>>) -> ResolveBlobReturnsBuilder<'a> {
218        ResolveBlobReturnsBuilder {
219            uuid: uuid.into(),
220        }
221    }
222    /// UUID of the specified Blob.
223    pub fn uuid(&self) -> &str { self.uuid.as_ref() }
224}
225
226
227pub struct ResolveBlobReturnsBuilder<'a> {
228    uuid: Cow<'a, str>,
229}
230
231impl<'a> ResolveBlobReturnsBuilder<'a> {
232    pub fn build(self) -> ResolveBlobReturns<'a> {
233        ResolveBlobReturns {
234            uuid: self.uuid,
235        }
236    }
237}
238
239impl<'a> ResolveBlobParams<'a> { pub const METHOD: &'static str = "IO.resolveBlob"; }
240
241impl<'a> crate::CdpCommand<'a> for ResolveBlobParams<'a> {
242    const METHOD: &'static str = "IO.resolveBlob";
243    type Response = ResolveBlobReturns<'a>;
244}