Skip to content

Commit

Permalink
update as_bytes to to_bytes and as_str to try_string
Browse files Browse the repository at this point in the history
  • Loading branch information
Muscraft committed Jul 18, 2022
1 parent 0d4447b commit a989b4e
Show file tree
Hide file tree
Showing 7 changed files with 34 additions and 46 deletions.
10 changes: 7 additions & 3 deletions crates/snapbox/src/assert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl Assert {
// On `expected` being an error, make a best guess
if expected
.as_ref()
.map(|d| d.as_str().is_some())
.map(|d| d.try_string().is_ok())
.unwrap_or(true)
{
actual = actual.try_text().map_text(crate::utils::normalize_lines);
Expand All @@ -197,11 +197,15 @@ impl Assert {
) -> (crate::Result<crate::Data>, crate::Data) {
let expected = expected.map(|d| d.map_text(crate::utils::normalize_lines));
// On `expected` being an error, make a best guess
if let Some(expected) = expected.as_ref().map(|d| d.as_str()).unwrap_or(Some("")) {
if let Ok(expected) = expected
.as_ref()
.map(|d| d.try_string())
.unwrap_or(Ok(String::new()))
{
actual = actual
.try_text()
.map_text(crate::utils::normalize_text)
.map_text(|t| self.substitutions.normalize(t, expected));
.map_text(|t| self.substitutions.normalize(t, &expected));
}

(expected, actual)
Expand Down
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
32 changes: 10 additions & 22 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 @@ -112,37 +112,25 @@ impl Data {
///
/// Note: this will **not** do a binary-content check
pub fn make_text(&mut self) -> Result<(), std::str::Utf8Error> {
*self = Self::text(std::mem::take(self).into_string()?);
*self = Self::text(std::mem::take(self).try_string()?);
Ok(())
}

/// Coerce to a string
///
/// Note: this will **not** do a binary-content check
pub fn into_string(self) -> Result<String, std::str::Utf8Error> {
match self.inner {
/// Tries to convert the underlying data into a String
pub fn try_string(&self) -> Result<String, std::str::Utf8Error> {
match &self.inner {
DataInner::Binary(data) => {
let data = String::from_utf8(data).map_err(|e| e.utf8_error())?;
let data = String::from_utf8(data.clone()).map_err(|e| e.utf8_error())?;
Ok(data)
}
DataInner::Text(data) => Ok(data),
}
}

/// Return the underlying `str`
///
/// Note: this will not inspect binary data for being a valid `str`.
pub fn as_str(&self) -> Option<&str> {
match &self.inner {
DataInner::Binary(_) => None,
DataInner::Text(data) => Some(data.as_str()),
DataInner::Text(data) => Ok(data.to_owned()),
}
}

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
6 changes: 3 additions & 3 deletions crates/snapbox/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ impl PathDiff {
let expected = crate::Data::read_from(&expected_path, None)
.map(|d| d.map_text(crate::utils::normalize_lines))
.map_err(Self::Failure)?;
if expected.as_str().is_some() {
if expected.try_string().is_ok() {
actual = actual.try_text().map_text(crate::utils::normalize_lines);
}

Expand Down Expand Up @@ -258,11 +258,11 @@ impl PathDiff {
let expected = crate::Data::read_from(&expected_path, None)
.map(|d| d.map_text(crate::utils::normalize_lines))
.map_err(Self::Failure)?;
if let Some(expected) = expected.as_str() {
if let Ok(expected) = expected.try_string() {
actual = actual
.try_text()
.map_text(crate::utils::normalize_text)
.map_text(|t| substitutions.normalize(t, expected));
.map_text(|t| substitutions.normalize(t, &expected));
}

if expected != actual {
Expand Down
6 changes: 3 additions & 3 deletions crates/snapbox/src/report/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ pub fn write_diff(
#[allow(unused_mut)]
let mut rendered = false;
#[cfg(feature = "diff")]
if let (Some(expected), Some(actual)) = (expected.as_str(), actual.as_str()) {
if let (Ok(expected), Ok(actual)) = (expected.try_string(), actual.try_string()) {
write_diff_inner(
writer,
expected,
actual,
&expected,
&actual,
expected_name,
actual_name,
palette,
Expand Down
4 changes: 2 additions & 2 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -419,8 +419,8 @@ impl Case {
}

if let Some(expected_content) = expected_content {
if let Some(e) = expected_content.as_str() {
stream.content = stream.content.map_text(|t| substitutions.normalize(t, e));
if let Ok(e) = expected_content.try_string() {
stream.content = stream.content.map_text(|t| substitutions.normalize(t, &e));
}
if stream.content != *expected_content {
stream.status = StreamStatus::Expected(expected_content.clone());
Expand Down
15 changes: 6 additions & 9 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ impl TryCmd {
} else if ext == std::ffi::OsStr::new("trycmd") || ext == std::ffi::OsStr::new("md") {
let raw = crate::Data::read_from(path, Some(snapbox::DataFormat::Text))?
.map_text(snapbox::utils::normalize_lines)
.into_string()
.try_string()
.unwrap();
Self::parse_trycmd(&raw)?
} else {
Expand Down Expand Up @@ -144,10 +144,7 @@ impl TryCmd {
.expected_stdout_source
.clone()
.expect("always present for .trycmd");
let mut stdout = stdout
.as_str()
.expect("already converted to Text")
.to_owned();
let mut stdout = stdout.try_string().expect("already converted to Text");
// Add back trailing newline removed when parsing
stdout.push('\n');
let mut raw = crate::Data::read_from(path, Some(snapbox::DataFormat::Text))?
Expand Down Expand Up @@ -323,7 +320,7 @@ fn overwrite_toml_output(
let output_path = path.with_extension(output_ext);
if output_path.exists() {
output.write_to(&output_path)?;
} else if let Some(output) = output.as_str() {
} else if let Ok(output) = output.try_string() {
let raw = std::fs::read_to_string(path)
.map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
let mut doc = raw
Expand Down Expand Up @@ -360,9 +357,9 @@ fn replace_lines(
let mut output_lines = String::new();

let s = data
.as_str()
.ok_or("Binary file can't have lines replaced")?;
for (line_num, line) in snapbox::utils::LinesWithTerminator::new(s)
.try_string()
.expect("Binary file can't have lines replaced");
for (line_num, line) in snapbox::utils::LinesWithTerminator::new(&s)
.enumerate()
.map(|(i, l)| (i + 1, l))
{
Expand Down

0 comments on commit a989b4e

Please sign in to comment.