1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
use async_trait::async_trait;
use time::OffsetDateTime;
use url::Url;

use anyhow::anyhow;
use std::path::PathBuf;

use super::Environment;
use crate::util::{to_file_path, to_file_uri};

#[derive(Clone)]
pub struct NativeEnvironment {
    handle: tokio::runtime::Handle,
}

impl NativeEnvironment {
    #[must_use]
    pub fn new() -> Self {
        Self {
            handle: tokio::runtime::Handle::current(),
        }
    }
}

impl Default for NativeEnvironment {
    fn default() -> Self {
        Self::new()
    }
}

#[async_trait(?Send)]
impl Environment for NativeEnvironment {
    type Stdin = tokio::io::Stdin;
    type Stdout = tokio::io::Stdout;
    type Stderr = tokio::io::Stderr;

    fn now(&self) -> time::OffsetDateTime {
        OffsetDateTime::now_utc()
    }

    fn spawn<F>(&self, fut: F)
    where
        F: futures::Future + Send + 'static,
        F::Output: Send,
    {
        self.handle.spawn(fut);
    }

    fn spawn_local<F>(&self, fut: F)
    where
        F: futures::Future + 'static,
    {
        tokio::task::spawn_local(fut);
    }

    fn env_var(&self, name: &str) -> Option<String> {
        std::env::var(name).ok()
    }

    fn atty_stderr(&self) -> bool {
        atty::is(atty::Stream::Stderr)
    }

    fn stdin(&self) -> Self::Stdin {
        tokio::io::stdin()
    }

    fn stdout(&self) -> Self::Stdout {
        tokio::io::stdout()
    }

    fn stderr(&self) -> Self::Stderr {
        tokio::io::stderr()
    }

    async fn read_file(&self, path: &Url) -> Result<Vec<u8>, anyhow::Error> {
        let path = to_file_path(path).ok_or_else(|| anyhow!("failed to read file at ${path}"))?;
        Ok(tokio::fs::read(PathBuf::from(path)).await?)
    }

    async fn write_file(&self, path: &Url, bytes: &[u8]) -> Result<(), anyhow::Error> {
        let path = to_file_path(path).ok_or_else(|| anyhow!("failed to read file at ${path}"))?;
        Ok(tokio::fs::write(PathBuf::from(path), bytes).await?)
    }

    #[cfg(feature = "fetch")]
    async fn fetch_file(&self, url: &Url) -> Result<Vec<u8>, anyhow::Error> {
        let client = reqwest::Client::builder()
            .timeout(std::time::Duration::from_secs(30))
            .build()
            .unwrap();
        let data = client
            .get(url.clone())
            .send()
            .await?
            .bytes()
            .await?
            .to_vec();
        Ok(data)
    }

    #[cfg(not(feature = "fetch"))]
    async fn fetch_file(&self, url: &Url) -> Result<Vec<u8>, anyhow::Error> {
        anyhow::bail!("failed to fetch `{url}`, fetch is not supported")
    }

    fn root_uri(&self) -> Option<Url> {
        let cwd = std::env::current_dir().ok()?;
        to_file_uri(&cwd.display().to_string(), &None)
    }
}