playwright/imp/
stream.rs

1use crate::imp::{core::*, prelude::*};
2use std::{
3    fs::File,
4    io::{BufWriter, Write}
5};
6
7#[derive(Debug)]
8pub(crate) struct Stream {
9    channel: ChannelOwner
10}
11
12impl Stream {
13    pub(crate) fn new(channel: ChannelOwner) -> Self { Self { channel } }
14
15    pub(crate) async fn save_as<P: AsRef<Path>>(&self, path: P) -> ArcResult<()> {
16        let file = File::create(path).map_err(Error::from)?;
17        let mut writer = BufWriter::new(file);
18        loop {
19            let v = send_message!(self, "read", Map::new());
20            let b64 = only_str(&v)?;
21            if b64.is_empty() {
22                break;
23            } else {
24                let bytes = base64::decode(b64).map_err(Error::InvalidBase64)?;
25                writer.write(&bytes).map_err(Error::from)?;
26            }
27        }
28        Ok(())
29    }
30
31    // with open(path, mode="wb") as file:
32    //    while True:
33    //        binary = await self._channel.send("read")
34    //        if not binary:
35    //            break
36    //        file.write(base64.b64decode(binary))
37}
38
39impl RemoteObject for Stream {
40    fn channel(&self) -> &ChannelOwner { &self.channel }
41    fn channel_mut(&mut self) -> &mut ChannelOwner { &mut self.channel }
42}