warpgate_api/
host_funcs.rs

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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
use crate::virtual_path::VirtualPath;
use crate::{api_struct, api_unit_enum, AnyResult};
use rustc_hash::FxHashMap;
use serde::de::DeserializeOwned;

api_unit_enum!(
    /// Target where host logs should be written to.
    pub enum HostLogTarget {
        Stderr,
        Stdout,
        #[default]
        Tracing,
    }
);

api_struct!(
    /// Input passed to the `host_log` host function.
    pub struct HostLogInput {
        #[serde(default)]
        pub data: FxHashMap<String, serde_json::Value>,

        pub message: String,

        #[serde(default)]
        pub target: HostLogTarget,
    }
);

impl HostLogInput {
    /// Create a new host log with the provided message.
    pub fn new(message: impl AsRef<str>) -> Self {
        Self {
            message: message.as_ref().to_owned(),
            ..Default::default()
        }
    }
}

impl From<&str> for HostLogInput {
    fn from(message: &str) -> Self {
        HostLogInput::new(message)
    }
}

impl From<String> for HostLogInput {
    fn from(message: String) -> Self {
        HostLogInput::new(message)
    }
}

api_struct!(
    /// Input passed to the `exec_command` host function.
    pub struct ExecCommandInput {
        /// The command or script to execute.
        pub command: String,

        /// Arguments to pass to the command.
        pub args: Vec<String>,

        /// Environment variables to pass to the command.
        #[serde(default)]
        pub env: FxHashMap<String, String>,

        /// Mark the command as executable before executing.
        #[doc(hidden)]
        pub set_executable: bool,

        /// Stream the output instead of capturing it.
        #[serde(default)]
        pub stream: bool,

        /// Override the current working directory.
        #[serde(default)]
        pub working_dir: Option<VirtualPath>,
    }
);

impl ExecCommandInput {
    /// Create a new command that pipes and captures the output.
    pub fn pipe<C, I, V>(command: C, args: I) -> ExecCommandInput
    where
        C: AsRef<str>,
        I: IntoIterator<Item = V>,
        V: AsRef<str>,
    {
        ExecCommandInput {
            command: command.as_ref().to_string(),
            args: args.into_iter().map(|a| a.as_ref().to_owned()).collect(),
            ..ExecCommandInput::default()
        }
    }

    /// Create a new command that inherits and streams the output.
    pub fn inherit<C, I, V>(command: C, args: I) -> ExecCommandInput
    where
        C: AsRef<str>,
        I: IntoIterator<Item = V>,
        V: AsRef<str>,
    {
        let mut input = Self::pipe(command, args);
        input.stream = true;
        input
    }
}

api_struct!(
    /// Output returned from the `exec_command` host function.
    #[serde(default)]
    pub struct ExecCommandOutput {
        pub command: String,
        pub exit_code: i32,
        pub stderr: String,
        pub stdout: String,
    }
);

impl ExecCommandOutput {
    pub fn get_output(&self) -> String {
        let mut out = String::new();
        out.push_str(self.stdout.trim());

        if !self.stderr.is_empty() {
            if !out.is_empty() {
                out.push(' ');
            }

            out.push_str(self.stderr.trim());
        }

        out
    }
}

api_struct!(
    /// Input passed to the `send_request` host function.
    pub struct SendRequestInput {
        /// The URL to send to.
        pub url: String,
    }
);

impl SendRequestInput {
    /// Create a new send request with the provided url.
    pub fn new(url: impl AsRef<str>) -> Self {
        Self {
            url: url.as_ref().to_owned(),
        }
    }
}

impl From<&str> for SendRequestInput {
    fn from(url: &str) -> Self {
        SendRequestInput::new(url)
    }
}

impl From<String> for SendRequestInput {
    fn from(url: String) -> Self {
        SendRequestInput::new(url)
    }
}

api_struct!(
    /// Output returned from the `send_request` host function.
    pub struct SendRequestOutput {
        pub body: Vec<u8>,
        pub body_length: u64,
        pub body_offset: u64,
        pub status: u16,
    }
);

impl SendRequestOutput {
    /// Consume the response body and return as JSON.
    pub fn json<T: DeserializeOwned>(self) -> AnyResult<T> {
        Ok(serde_json::from_slice(&self.body)?)
    }

    /// Consume the response body and return as raw text.
    pub fn text(self) -> AnyResult<String> {
        Ok(String::from_utf8(self.body)?)
    }
}