Skip to content

Commit

Permalink
fix(data): From From<D> impls in favor of IntoData
Browse files Browse the repository at this point in the history
This reduces duplicated effort
  • Loading branch information
epage committed Apr 22, 2024
1 parent fb14410 commit 8daebd3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 50 deletions.
4 changes: 2 additions & 2 deletions crates/snapbox/src/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -630,7 +630,7 @@ impl OutputAssert {

#[track_caller]
fn stdout_eq_inner(self, expected: crate::Data) -> Self {
let actual = crate::Data::from(self.output.stdout.as_slice());
let actual = self.output.stdout.as_slice().into_data();
if let Err(err) = self.config.try_eq(expected, actual, Some(&"stdout")) {
err.panic();
}
Expand Down Expand Up @@ -680,7 +680,7 @@ impl OutputAssert {

#[track_caller]
fn stderr_eq_inner(self, expected: crate::Data) -> Self {
let actual = crate::Data::from(self.output.stderr.as_slice());
let actual = self.output.stderr.as_slice().into_data();
if let Err(err) = self.config.try_eq(expected, actual, Some(&"stderr")) {
err.panic();
}
Expand Down
49 changes: 3 additions & 46 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ impl IntoData for Vec<u8> {

impl IntoData for &'_ [u8] {
fn into_data(self) -> Data {
self.to_owned().into()
self.to_owned().into_data()
}
}

Expand All @@ -103,13 +103,13 @@ impl IntoData for String {

impl IntoData for &'_ String {
fn into_data(self) -> Data {
self.to_owned().into()
self.to_owned().into_data()
}
}

impl IntoData for &'_ str {
fn into_data(self) -> Data {
self.to_owned().into()
self.to_owned().into_data()
}
}

Expand Down Expand Up @@ -671,49 +671,6 @@ impl Default for Data {
}
}

impl<'d> From<&'d Data> for Data {
fn from(other: &'d Data) -> Self {
other.clone()
}
}

impl From<Vec<u8>> for Data {
fn from(other: Vec<u8>) -> Self {
Self::binary(other)
}
}

impl<'b> From<&'b [u8]> for Data {
fn from(other: &'b [u8]) -> Self {
other.to_owned().into()
}
}

impl From<String> for Data {
fn from(other: String) -> Self {
Self::text(other)
}
}

impl<'s> From<&'s String> for Data {
fn from(other: &'s String) -> Self {
other.clone().into()
}
}

impl<'s> From<&'s str> for Data {
fn from(other: &'s str) -> Self {
other.to_owned().into()
}
}

impl From<Inline> for super::Data {
fn from(inline: Inline) -> Self {
let trimmed = inline.trimmed();
super::Data::text(trimmed).with_source(inline)
}
}

#[cfg(feature = "detect-encoding")]
fn is_binary(data: &[u8]) -> bool {
match content_inspector::inspect(data) {
Expand Down
5 changes: 3 additions & 2 deletions src/runner.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use rayon::prelude::*;
use snapbox::data::DataFormat;
use snapbox::filters::{Filter as _, FilterNewlines, FilterPaths, FilterRedactions};
use snapbox::path::FileType;
use snapbox::IntoData;

#[derive(Debug)]
pub(crate) struct Runner {
Expand Down Expand Up @@ -580,12 +581,12 @@ impl Output {
self.spawn.status = SpawnStatus::Ok;
self.stdout = Some(Stream {
stream: Stdio::Stdout,
content: output.stdout.into(),
content: output.stdout.into_data(),
status: StreamStatus::Ok,
});
self.stderr = Some(Stream {
stream: Stdio::Stderr,
content: output.stderr.into(),
content: output.stderr.into_data(),
status: StreamStatus::Ok,
});
self
Expand Down

0 comments on commit 8daebd3

Please sign in to comment.