Skip to content
This repository was archived by the owner on Feb 1, 2024. It is now read-only.

Commit cfc45a7

Browse files
author
Achim Schneider
committed
update Snafu with breaking changes
1 parent 8013423 commit cfc45a7

File tree

3 files changed

+32
-31
lines changed

3 files changed

+32
-31
lines changed

Cargo.lock

Lines changed: 5 additions & 4 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

crates/sandbox/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ readme = "README.md"
99
[dependencies]
1010
serde = "1.0.143"
1111
tempdir = "0.3.7"
12-
snafu = "0.6.10"
12+
snafu = "0.7.4"
1313
log = "0.4.17"
1414
tokio = { version = "1.19.2", features = [
1515
"macros",

crates/sandbox/src/lib.rs

Lines changed: 26 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -73,43 +73,43 @@ pub struct Sandbox {
7373
#[derive(Debug, Snafu)]
7474
pub enum Error {
7575
#[snafu(display("Unable to create temporary directory: {}", source))]
76-
UnableToCreateTempDir { source: io::Error },
76+
UnableToCreateTempDirSnafu { source: io::Error },
7777

7878
#[snafu(display("Unable to create output directory: {}", source))]
79-
UnableToCreateOutputDir { source: io::Error },
79+
UnableToCreateOutputDirSnafu { source: io::Error },
8080

8181
#[snafu(display("Unable to set permissions for output directory: {}", source))]
82-
UnableToSetOutputPermissions { source: io::Error },
82+
UnableToSetOutputPermissionsSnafu { source: io::Error },
8383

8484
#[snafu(display("Unable to create source file: {}", source))]
85-
UnableToCreateSourceFile { source: io::Error },
85+
UnableToCreateSourceFileSnafu { source: io::Error },
8686

8787
#[snafu(display("Unable to set permissions for source file: {}", source))]
88-
UnableToSetSourcePermissions { source: io::Error },
88+
UnableToSetSourcePermissionsSnafu { source: io::Error },
8989

9090
#[snafu(display("Output was not valid UTF-8: {}", source))]
9191
OutputNotUtf8 { source: string::FromUtf8Error },
9292

9393
#[snafu(display("Unable to read output file: {}", source))]
94-
UnableToReadOutput { source: io::Error },
94+
UnableToReadOutputSnafu { source: io::Error },
9595

9696
#[snafu(display("Unable to start the compiler: {}", source))]
97-
UnableToStartCompiler { source: io::Error },
97+
UnableToStartCompilerSnafu { source: io::Error },
9898

9999
#[snafu(display("Unable to find the compiler ID"))]
100-
MissingCompilerId,
100+
MissingCompilerIdSnafu,
101101

102102
#[snafu(display("Unable to wait for the compiler: {}", source))]
103-
UnableToWaitForCompiler { source: io::Error },
103+
UnableToWaitForCompilerSnafu { source: io::Error },
104104

105105
#[snafu(display("Unable to get output from the compiler: {}", source))]
106-
UnableToGetOutputFromCompiler { source: io::Error },
106+
UnableToGetOutputFromCompilerSnafu { source: io::Error },
107107

108108
#[snafu(display("Unable to remove the compiler: {}", source))]
109-
UnableToRemoveCompiler { source: io::Error },
109+
UnableToRemoveCompilerSnafu { source: io::Error },
110110

111111
#[snafu(display("Compiler execution took longer than {} ms", timeout.as_millis()))]
112-
CompilerExecutionTimedOut {
112+
CompilerExecutionTimedOutSnafu {
113113
source: tokio::time::error::Elapsed,
114114
timeout: Duration,
115115
},
@@ -172,13 +172,13 @@ const DOCKER_PROCESS_TIMEOUT_HARD: Duration = Duration::from_secs(60);
172172

173173
impl Sandbox {
174174
pub fn new() -> Result<Self> {
175-
let scratch = TempDir::new("playground").context(UnableToCreateTempDir)?;
175+
let scratch = TempDir::new("playground").context(UnableToCreateTempDirSnafuSnafu)?;
176176
let input_file = scratch.path().join("input.rs");
177177
let output_dir = scratch.path().join("output");
178-
fs::create_dir(&output_dir).context(UnableToCreateOutputDir)?;
178+
fs::create_dir(&output_dir).context(UnableToCreateOutputDirSnafuSnafu)?;
179179

180180
fs::set_permissions(&output_dir, wide_open_permissions())
181-
.context(UnableToSetOutputPermissions)?;
181+
.context(UnableToSetOutputPermissionsSnafuSnafu)?;
182182

183183
Ok(Sandbox {
184184
scratch,
@@ -196,7 +196,7 @@ impl Sandbox {
196196

197197
let output = run_command_with_timeout(command)?;
198198
let file = fs::read_dir(&self.output_dir)
199-
.context(UnableToReadOutput)?
199+
.context(UnableToReadOutputSnafuSnafu)?
200200
.flatten()
201201
.map(|entry| entry.path())
202202
.find(|path| path.extension() == Some(OsStr::new("contract")));
@@ -262,9 +262,9 @@ impl Sandbox {
262262
}
263263

264264
fn write_source_code(&self, code: &str) -> Result<()> {
265-
fs::write(&self.input_file, code).context(UnableToCreateSourceFile)?;
265+
fs::write(&self.input_file, code).context(UnableToCreateSourceFileSnafuSnafu)?;
266266
fs::set_permissions(&self.input_file, wide_open_permissions())
267-
.context(UnableToSetSourcePermissions)?;
267+
.context(UnableToSetSourcePermissionsSnafuSnafu)?;
268268

269269
println!(
270270
"Wrote {} bytes of source to {}",
@@ -283,7 +283,7 @@ fn read(path: &Path) -> Result<Option<Vec<u8>>> {
283283
let f = match File::open(path) {
284284
Ok(f) => f,
285285
Err(ref e) if e.kind() == ErrorKind::NotFound => return Ok(None),
286-
e => e.context(UnableToReadOutput)?,
286+
e => e.context(UnableToReadOutputSnafuSnafu)?,
287287
};
288288
let mut f = BufReader::new(f);
289289
let metadata = fs::metadata(path).expect("unable to read metadata");
@@ -299,7 +299,7 @@ async fn run_command_with_timeout(mut command: Command) -> Result<std::process::
299299

300300
let timeout = DOCKER_PROCESS_TIMEOUT_HARD;
301301
println!("executing command!");
302-
let output = command.output().await.context(UnableToStartCompiler)?;
302+
let output = command.output().await.context(UnableToStartCompilerSnafuSnafu)?;
303303
println!("Done! {:?}", output);
304304
// Exit early, in case we don't have the container
305305
// if !output.status.success() {
@@ -308,7 +308,7 @@ async fn run_command_with_timeout(mut command: Command) -> Result<std::process::
308308
// let response = &output.stdout;
309309
let stdout = String::from_utf8_lossy(&output.stdout);
310310

311-
let id = stdout.lines().next().context(MissingCompilerId)?.trim();
311+
let id = stdout.lines().next().context(MissingCompilerIdSnafuSnafu)?.trim();
312312
let stderr = &output.stderr;
313313

314314
// ----------
@@ -328,7 +328,7 @@ async fn run_command_with_timeout(mut command: Command) -> Result<std::process::
328328
.unwrap_or(i32::MAX);
329329
Ok(ExitStatusExt::from_raw(code))
330330
}
331-
Ok(e) => return e.context(UnableToWaitForCompiler), // Failed to run
331+
Ok(e) => return e.context(UnableToWaitForCompilerSnafuSnafu), // Failed to run
332332
Err(e) => Err(e), // Timed out
333333
};
334334

@@ -338,7 +338,7 @@ async fn run_command_with_timeout(mut command: Command) -> Result<std::process::
338338
let mut output = command
339339
.output()
340340
.await
341-
.context(UnableToGetOutputFromCompiler)?;
341+
.context(UnableToGetOutputFromCompilerSnafuSnafu)?;
342342

343343
// ----------
344344

@@ -347,9 +347,9 @@ async fn run_command_with_timeout(mut command: Command) -> Result<std::process::
347347
"--force", id
348348
);
349349
command.stdout(std::process::Stdio::null());
350-
command.status().await.context(UnableToRemoveCompiler)?;
350+
command.status().await.context(UnableToRemoveCompilerSnafuSnafu)?;
351351

352-
let code = timed_out.context(CompilerExecutionTimedOut { timeout })?;
352+
let code = timed_out.context(CompilerExecutionTimedOutSnafuSnafu { timeout })?;
353353

354354
output.status = code;
355355
output.stderr = stderr.to_owned();
@@ -368,7 +368,7 @@ fn wide_open_permissions() -> std::fs::Permissions {
368368
}
369369

370370
fn vec_to_str(v: Vec<u8>) -> Result<String> {
371-
String::from_utf8(v).context(OutputNotUtf8)
371+
String::from_utf8(v).context(OutputNotUtf8Snafu)
372372
}
373373

374374
#[cfg(test)]

0 commit comments

Comments
 (0)