1use std::collections::HashMap;
2use std::future::{Future, IntoFuture};
3use std::pin::Pin;
4
5use dvm_api::{
6 DvmStop, FileStatus, InspectionWatches, PathDirectoryEntry, SandboxId, SeekSandbox, SeekTarget,
7 StepPoint, StopEvent, WatchPoint, WatchPoints,
8};
9use reqwest::Method;
10
11use crate::client::Client;
12use crate::error::{Error, Result};
13
14#[derive(Clone, Debug)]
15pub struct SandboxPoint {
16 client: Client,
17 sandbox: SandboxId,
18 stop: DvmStop,
19}
20
21#[must_use = "a seek happens once awaited"]
22pub struct SeekBuilder {
23 client: Client,
24 sandbox: SandboxId,
25 target: SeekTarget,
26 watches: Vec<WatchPoint>,
27}
28
29#[must_use = "a step happens once awaited"]
30pub struct StepBuilder {
31 point: SandboxPoint,
32 watches: Vec<WatchPoint>,
33}
34
35#[must_use = "stat runs once awaited"]
36pub struct StatBuilder {
37 point: SandboxPoint,
38 path: String,
39 follow_symlinks: bool,
40}
41
42impl SeekBuilder {
43 pub(crate) fn new(client: Client, sandbox: SandboxId, target: SeekTarget) -> Self {
44 Self {
45 client,
46 sandbox,
47 target,
48 watches: Vec::new(),
49 }
50 }
51
52 pub fn watch(mut self, watch: WatchPoint) -> Self {
53 self.watches.push(watch);
54 self
55 }
56
57 pub fn watches(mut self, watches: impl IntoIterator<Item = WatchPoint>) -> Self {
58 self.watches.extend(watches);
59 self
60 }
61
62 async fn run(self) -> Result<SandboxPoint> {
63 let body = SeekSandbox {
64 target: self.target,
65 watches: encode_watches(self.watches)?,
66 };
67 let stop: DvmStop = self
68 .client
69 .json_idempotent(
70 Method::POST,
71 &format!("/sandboxes/{}/points/seek", self.sandbox),
72 &[],
73 Some(&body),
74 )
75 .await?;
76 Ok(SandboxPoint::new(self.client, self.sandbox, stop))
77 }
78}
79
80impl IntoFuture for SeekBuilder {
81 type Output = Result<SandboxPoint>;
82 type IntoFuture = Pin<Box<dyn Future<Output = Result<SandboxPoint>> + Send>>;
83
84 fn into_future(self) -> Self::IntoFuture {
85 Box::pin(self.run())
86 }
87}
88
89impl StepBuilder {
90 pub fn watch(mut self, watch: WatchPoint) -> Self {
91 self.watches.push(watch);
92 self
93 }
94
95 pub fn watches(mut self, watches: impl IntoIterator<Item = WatchPoint>) -> Self {
96 self.watches.extend(watches);
97 self
98 }
99
100 async fn run(self) -> Result<Option<SandboxPoint>> {
101 let body = StepPoint {
102 thread: self.point.stop.thread,
103 event: self.point.stop.event,
104 watches: encode_watches(self.watches)?,
105 };
106 let stop: Option<DvmStop> = self
107 .point
108 .client
109 .json_idempotent(
110 Method::POST,
111 &format!("/sandboxes/{}/points/step", self.point.sandbox),
112 &[],
113 Some(&body),
114 )
115 .await?;
116 Ok(stop.map(|stop| SandboxPoint::new(self.point.client, self.point.sandbox, stop)))
117 }
118}
119
120impl IntoFuture for StepBuilder {
121 type Output = Result<Option<SandboxPoint>>;
122 type IntoFuture = Pin<Box<dyn Future<Output = Result<Option<SandboxPoint>>> + Send>>;
123
124 fn into_future(self) -> Self::IntoFuture {
125 Box::pin(self.run())
126 }
127}
128
129impl StatBuilder {
130 pub fn follow_symlinks(mut self, follow: bool) -> Self {
131 self.follow_symlinks = follow;
132 self
133 }
134
135 async fn run(self) -> Result<FileStatus> {
136 let mut query = self.point.fs_query(self.path);
137 query.push(("follow_symlinks", self.follow_symlinks.to_string()));
138 self.point
139 .client
140 .json(
141 Method::GET,
142 &format!("/sandboxes/{}/fs/stat", self.point.sandbox),
143 &query,
144 None::<&()>,
145 )
146 .await
147 }
148}
149
150impl IntoFuture for StatBuilder {
151 type Output = Result<FileStatus>;
152 type IntoFuture = Pin<Box<dyn Future<Output = Result<FileStatus>> + Send>>;
153
154 fn into_future(self) -> Self::IntoFuture {
155 Box::pin(self.run())
156 }
157}
158
159impl SandboxPoint {
160 fn new(client: Client, sandbox: SandboxId, stop: DvmStop) -> Self {
161 Self {
162 client,
163 sandbox,
164 stop,
165 }
166 }
167
168 pub fn sandbox_id(&self) -> SandboxId {
169 self.sandbox
170 }
171
172 pub fn ns(&self) -> Option<u64> {
173 self.stop.recording_ns
174 }
175
176 pub fn thread(&self) -> u64 {
177 self.stop.thread
178 }
179
180 pub fn process(&self) -> u64 {
181 self.stop.process
182 }
183
184 pub fn operations(&self) -> u64 {
185 self.stop.operations
186 }
187
188 pub fn event(&self) -> u64 {
189 self.stop.event
190 }
191
192 pub fn at(&self) -> &StopEvent {
193 &self.stop.at
194 }
195
196 pub fn stats(&self) -> &HashMap<String, u64> {
197 &self.stop.stats
198 }
199
200 pub fn step(&self) -> StepBuilder {
201 StepBuilder {
202 point: self.clone(),
203 watches: Vec::new(),
204 }
205 }
206
207 pub fn stat(&self, path: impl Into<String>) -> StatBuilder {
208 StatBuilder {
209 point: self.clone(),
210 path: path.into(),
211 follow_symlinks: true,
212 }
213 }
214
215 pub async fn list_dir(&self, path: impl AsRef<str>) -> Result<Vec<PathDirectoryEntry>> {
216 let query = self.fs_query(path.as_ref().to_owned());
217 self.client
218 .json(
219 Method::GET,
220 &format!("/sandboxes/{}/fs/list", self.sandbox),
221 &query,
222 None::<&()>,
223 )
224 .await
225 }
226
227 pub async fn read_file(&self, path: impl AsRef<str>) -> Result<Vec<u8>> {
228 let query = self.fs_query(path.as_ref().to_owned());
229 self.client
230 .get_bytes(&format!("/sandboxes/{}/fs/read", self.sandbox), &query)
231 .await
232 }
233
234 fn fs_query(&self, path: String) -> Vec<(&'static str, String)> {
235 vec![
236 ("thread", self.stop.thread.to_string()),
237 ("event", self.stop.event.to_string()),
238 ("path", path),
239 ]
240 }
241}
242
243fn encode_watches(watches: Vec<WatchPoint>) -> Result<InspectionWatches> {
244 if watches.is_empty() {
245 Ok(InspectionWatches::default())
246 } else {
247 WatchPoints::try_from(watches)
248 .map(InspectionWatches::from)
249 .map_err(|reason| Error::InvalidRequest { reason })
250 }
251}