Skip to content

Commit

Permalink
Merge pull request #110 from Muscraft/data-format-support
Browse files Browse the repository at this point in the history
refactor(snapbox)!: Change `as_bytes` to `to_bytes`
  • Loading branch information
epage authored Jul 18, 2022
2 parents 0d4447b + 4543940 commit 6b98513
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 8 deletions.
7 changes: 3 additions & 4 deletions crates/snapbox/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -325,7 +325,7 @@ impl Command {
// before we read. Here we do this by dropping the Command object.
drop(self.cmd);

let (stdout, stderr) = process_io(&mut child, self.stdin.as_ref().map(|d| d.as_bytes()))?;
let (stdout, stderr) = process_io(&mut child, self.stdin.as_ref().map(|d| d.to_bytes()))?;

let status = wait(child, self.timeout)?;
let _stdout = stdout
Expand All @@ -350,7 +350,7 @@ impl Command {
self.cmd.stderr(std::process::Stdio::piped());
let mut child = self.cmd.spawn()?;

let (stdout, stderr) = process_io(&mut child, self.stdin.as_ref().map(|d| d.as_bytes()))?;
let (stdout, stderr) = process_io(&mut child, self.stdin.as_ref().map(|d| d.to_bytes()))?;

let status = wait(child, self.timeout)?;
let stdout = stdout
Expand All @@ -370,11 +370,10 @@ impl Command {

fn process_io(
child: &mut std::process::Child,
input: Option<&[u8]>,
input: Option<Vec<u8>>,
) -> std::io::Result<(Stream, Stream)> {
use std::io::Write;

let input = input.map(|b| b.to_owned());
let stdin = input.and_then(|i| {
child
.stdin
Expand Down
8 changes: 4 additions & 4 deletions crates/snapbox/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ impl Data {
format!("Failed to create parent dir for {}: {}", path.display(), e)
})?;
}
std::fs::write(path, self.as_bytes())
std::fs::write(path, self.to_bytes())
.map_err(|e| format!("Failed to write {}: {}", path.display(), e).into())
}

Expand Down Expand Up @@ -139,10 +139,10 @@ impl Data {
}
}

pub fn as_bytes(&self) -> &[u8] {
pub fn to_bytes(&self) -> Vec<u8> {
match &self.inner {
DataInner::Binary(data) => data,
DataInner::Text(data) => data.as_bytes(),
DataInner::Binary(data) => data.clone(),
DataInner::Text(data) => data.clone().into_bytes(),
}
}
}
Expand Down

0 comments on commit 6b98513

Please sign in to comment.