playwright/imp/
artifact.rs

1use crate::imp::{core::*, prelude::*};
2
3#[derive(Debug)]
4pub(crate) struct Artifact {
5    channel: ChannelOwner,
6    pub(crate) absolute_path: String,
7    var: Mutex<Variable>
8}
9
10#[derive(Debug, Default)]
11pub(crate) struct Variable {
12    is_remote: bool
13}
14
15impl Artifact {
16    pub(crate) fn try_new(channel: ChannelOwner) -> Result<Self, Error> {
17        let Initializer { absolute_path } = serde_json::from_value(channel.initializer.clone())?;
18        Ok(Self {
19            channel,
20            absolute_path,
21            var: Mutex::default()
22        })
23    }
24
25    pub(crate) async fn path_after_finished(&self) -> ArcResult<Option<PathBuf>> {
26        if self.is_remote() {
27            return Err(Error::RemoteArtifact.into());
28        }
29        let v = send_message!(self, "pathAfterFinished", Map::new());
30        let p: Option<PathBuf> = maybe_only_str(&*v)?.map(|s| s.into());
31        Ok(p)
32    }
33
34    pub(crate) async fn delete(&self) -> ArcResult<()> {
35        let _ = send_message!(self, "delete", Map::new());
36        Ok(())
37    }
38
39    pub(crate) async fn save_as<P: AsRef<Path>>(&self, path: P) -> ArcResult<()> {
40        let path = path.as_ref();
41        let dir = path
42            .parent()
43            .ok_or_else(|| Error::ResolvePath(path.into()))?;
44        let res = send_message!(self, "saveAsStream", Map::new());
45        let guid = only_guid(&res)?;
46        let stream = get_object!(self.context()?.lock().unwrap(), guid, Stream)?;
47        std::fs::create_dir_all(dir).map_err(Error::from)?;
48        upgrade(&stream)?.save_as(path).await?;
49        Ok(())
50    }
51
52    pub(crate) async fn failure(&self) -> ArcResult<Option<String>> {
53        let v = send_message!(self, "failure", Map::new());
54        let msg = maybe_only_str(&v)?;
55        Ok(msg.map(ToOwned::to_owned))
56    }
57}
58
59// mutable
60impl Artifact {
61    fn set_is_remote(&self, x: bool) { self.var.lock().unwrap().is_remote = x; }
62
63    fn is_remote(&self) -> bool { self.var.lock().unwrap().is_remote }
64}
65
66impl RemoteObject for Artifact {
67    fn channel(&self) -> &ChannelOwner { &self.channel }
68    fn channel_mut(&mut self) -> &mut ChannelOwner { &mut self.channel }
69}
70
71#[derive(Debug, Deserialize)]
72#[serde(rename_all = "camelCase")]
73struct Initializer {
74    absolute_path: String
75}