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    pub fn builder(handle: StreamHandle<'a>) -> CloseParamsBuilder<'a> {
24        CloseParamsBuilder {
25            handle: handle,
26        }
27    }
28    pub fn handle(&self) -> &StreamHandle<'a> { &self.handle }
29}
30
31
32pub struct CloseParamsBuilder<'a> {
33    handle: StreamHandle<'a>,
34}
35
36impl<'a> CloseParamsBuilder<'a> {
37    pub fn build(self) -> CloseParams<'a> {
38        CloseParams {
39            handle: self.handle,
40        }
41    }
42}
43
44impl<'a> CloseParams<'a> { pub const METHOD: &'static str = "IO.close"; }
45
46impl<'a> crate::CdpCommand<'a> for CloseParams<'a> {
47    const METHOD: &'static str = "IO.close";
48    type Response = crate::EmptyReturns;
49}
50
51/// Read a chunk of the stream
52
53#[derive(Debug, Clone, Serialize, Deserialize, Default)]
54#[serde(rename_all = "camelCase")]
55pub struct ReadParams<'a> {
56    /// Handle of the stream to read.
57    handle: StreamHandle<'a>,
58    /// Seek to the specified offset before reading (if not specified, proceed with offset
59    /// following the last read). Some types of streams may only support sequential reads.
60    #[serde(skip_serializing_if = "Option::is_none")]
61    offset: Option<i32>,
62    /// Maximum number of bytes to read (left upon the agent discretion if not specified).
63    #[serde(skip_serializing_if = "Option::is_none")]
64    size: Option<u64>,
65}
66
67impl<'a> ReadParams<'a> {
68    pub fn builder(handle: StreamHandle<'a>) -> ReadParamsBuilder<'a> {
69        ReadParamsBuilder {
70            handle: handle,
71            offset: None,
72            size: None,
73        }
74    }
75    pub fn handle(&self) -> &StreamHandle<'a> { &self.handle }
76    pub fn offset(&self) -> Option<i32> { self.offset }
77    pub fn size(&self) -> Option<u64> { self.size }
78}
79
80
81pub struct ReadParamsBuilder<'a> {
82    handle: StreamHandle<'a>,
83    offset: Option<i32>,
84    size: Option<u64>,
85}
86
87impl<'a> ReadParamsBuilder<'a> {
88    /// Seek to the specified offset before reading (if not specified, proceed with offset
89    /// following the last read). Some types of streams may only support sequential reads.
90    pub fn offset(mut self, offset: i32) -> Self { self.offset = Some(offset); self }
91    /// Maximum number of bytes to read (left upon the agent discretion if not specified).
92    pub fn size(mut self, size: u64) -> Self { self.size = Some(size); self }
93    pub fn build(self) -> ReadParams<'a> {
94        ReadParams {
95            handle: self.handle,
96            offset: self.offset,
97            size: self.size,
98        }
99    }
100}
101
102/// Read a chunk of the stream
103
104#[derive(Debug, Clone, Serialize, Deserialize, Default)]
105#[serde(rename_all = "camelCase")]
106pub struct ReadReturns<'a> {
107    /// Set if the data is base64-encoded
108    #[serde(skip_serializing_if = "Option::is_none")]
109    base64Encoded: Option<bool>,
110    /// Data that were read.
111    data: Cow<'a, str>,
112    /// Set if the end-of-file condition occurred while reading.
113    eof: bool,
114}
115
116impl<'a> ReadReturns<'a> {
117    pub fn builder(data: impl Into<Cow<'a, str>>, eof: bool) -> ReadReturnsBuilder<'a> {
118        ReadReturnsBuilder {
119            base64Encoded: None,
120            data: data.into(),
121            eof: eof,
122        }
123    }
124    pub fn base64Encoded(&self) -> Option<bool> { self.base64Encoded }
125    pub fn data(&self) -> &str { self.data.as_ref() }
126    pub fn eof(&self) -> bool { self.eof }
127}
128
129
130pub struct ReadReturnsBuilder<'a> {
131    base64Encoded: Option<bool>,
132    data: Cow<'a, str>,
133    eof: bool,
134}
135
136impl<'a> ReadReturnsBuilder<'a> {
137    /// Set if the data is base64-encoded
138    pub fn base64Encoded(mut self, base64Encoded: bool) -> Self { self.base64Encoded = Some(base64Encoded); self }
139    pub fn build(self) -> ReadReturns<'a> {
140        ReadReturns {
141            base64Encoded: self.base64Encoded,
142            data: self.data,
143            eof: self.eof,
144        }
145    }
146}
147
148impl<'a> ReadParams<'a> { pub const METHOD: &'static str = "IO.read"; }
149
150impl<'a> crate::CdpCommand<'a> for ReadParams<'a> {
151    const METHOD: &'static str = "IO.read";
152    type Response = ReadReturns<'a>;
153}
154
155/// Return UUID of Blob object specified by a remote object id.
156
157#[derive(Debug, Clone, Serialize, Deserialize, Default)]
158#[serde(rename_all = "camelCase")]
159pub struct ResolveBlobParams<'a> {
160    /// Object id of a Blob object wrapper.
161    objectId: crate::runtime::RemoteObjectId<'a>,
162}
163
164impl<'a> ResolveBlobParams<'a> {
165    pub fn builder(objectId: crate::runtime::RemoteObjectId<'a>) -> ResolveBlobParamsBuilder<'a> {
166        ResolveBlobParamsBuilder {
167            objectId: objectId,
168        }
169    }
170    pub fn objectId(&self) -> &crate::runtime::RemoteObjectId<'a> { &self.objectId }
171}
172
173
174pub struct ResolveBlobParamsBuilder<'a> {
175    objectId: crate::runtime::RemoteObjectId<'a>,
176}
177
178impl<'a> ResolveBlobParamsBuilder<'a> {
179    pub fn build(self) -> ResolveBlobParams<'a> {
180        ResolveBlobParams {
181            objectId: self.objectId,
182        }
183    }
184}
185
186/// Return UUID of Blob object specified by a remote object id.
187
188#[derive(Debug, Clone, Serialize, Deserialize, Default)]
189#[serde(rename_all = "camelCase")]
190pub struct ResolveBlobReturns<'a> {
191    /// UUID of the specified Blob.
192    uuid: Cow<'a, str>,
193}
194
195impl<'a> ResolveBlobReturns<'a> {
196    pub fn builder(uuid: impl Into<Cow<'a, str>>) -> ResolveBlobReturnsBuilder<'a> {
197        ResolveBlobReturnsBuilder {
198            uuid: uuid.into(),
199        }
200    }
201    pub fn uuid(&self) -> &str { self.uuid.as_ref() }
202}
203
204
205pub struct ResolveBlobReturnsBuilder<'a> {
206    uuid: Cow<'a, str>,
207}
208
209impl<'a> ResolveBlobReturnsBuilder<'a> {
210    pub fn build(self) -> ResolveBlobReturns<'a> {
211        ResolveBlobReturns {
212            uuid: self.uuid,
213        }
214    }
215}
216
217impl<'a> ResolveBlobParams<'a> { pub const METHOD: &'static str = "IO.resolveBlob"; }
218
219impl<'a> crate::CdpCommand<'a> for ResolveBlobParams<'a> {
220    const METHOD: &'static str = "IO.resolveBlob";
221    type Response = ResolveBlobReturns<'a>;
222}