Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 17 additions & 29 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -111,32 +111,38 @@ pub(crate) enum DataInner {
}

impl Data {
pub(crate) fn with_inner(inner: DataInner) -> Self {
Self {
inner,
source: None,
}
}

/// Mark the data as binary (no post-processing)
pub fn binary(raw: impl Into<Vec<u8>>) -> Self {
DataInner::Binary(raw.into()).into()
Self::with_inner(DataInner::Binary(raw.into()))
}

/// Mark the data as text (post-processing)
pub fn text(raw: impl Into<String>) -> Self {
DataInner::Text(raw.into()).into()
Self::with_inner(DataInner::Text(raw.into()))
}

#[cfg(feature = "json")]
pub fn json(raw: impl Into<serde_json::Value>) -> Self {
DataInner::Json(raw.into()).into()
Self::with_inner(DataInner::Json(raw.into()))
}

#[cfg(feature = "json")]
pub fn jsonlines(raw: impl Into<Vec<serde_json::Value>>) -> Self {
DataInner::JsonLines(serde_json::Value::Array(raw.into())).into()
Self::with_inner(DataInner::JsonLines(serde_json::Value::Array(raw.into())))
}

fn error(raw: impl Into<crate::Error>, intended: DataFormat) -> Self {
DataError {
Self::with_inner(DataInner::Error(DataError {
error: raw.into(),
intended,
}
.into()
}))
}

/// Empty test data
Expand Down Expand Up @@ -288,13 +294,13 @@ impl Data {
#[cfg(feature = "term-svg")]
(DataInner::Text(inner), DataFormat::TermSvg) => DataInner::TermSvg(inner),
(inner, DataFormat::Binary) => {
let remake: Self = inner.into();
let remake = Self::with_inner(inner);
DataInner::Binary(remake.to_bytes().expect("error case handled"))
}
// This variant is already covered unless structured data is enabled
#[cfg(feature = "structured-data")]
(inner, DataFormat::Text) => {
if let Some(str) = Data::from(inner).render() {
if let Some(str) = Self::with_inner(inner).render() {
DataInner::Text(str)
} else {
return Err(format!("cannot convert {original:?} to {format:?}").into());
Expand Down Expand Up @@ -365,13 +371,13 @@ impl Data {
DataInner::TermSvg(anstyle_svg::Term::new().render_svg(&inner))
}
(inner, DataFormat::Binary) => {
let remake: Self = inner.into();
let remake = Self::with_inner(inner);
DataInner::Binary(remake.to_bytes().expect("error case handled"))
}
// This variant is already covered unless structured data is enabled
#[cfg(feature = "structured-data")]
(inner, DataFormat::Text) => {
let remake: Self = inner.into();
let remake = Self::with_inner(inner);
if let Some(str) = remake.render() {
DataInner::Text(str)
} else {
Expand Down Expand Up @@ -438,24 +444,6 @@ impl Data {
}
}

impl From<DataInner> for Data {
fn from(inner: DataInner) -> Self {
Data {
inner,
source: None,
}
}
}

impl From<DataError> for Data {
fn from(inner: DataError) -> Self {
Data {
inner: DataInner::Error(inner),
source: None,
}
}
}

impl std::fmt::Display for Data {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self.inner {
Expand Down
12 changes: 6 additions & 6 deletions crates/snapbox/src/data/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use super::*;
#[test]
#[cfg(feature = "term-svg")]
fn term_svg_eq() {
let left = Data::from(DataInner::TermSvg(
let left = Data::with_inner(DataInner::TermSvg(
"
irrelevant
<text>relevant
Expand All @@ -15,7 +15,7 @@ irrelevant
irrelevant"
.to_owned(),
));
let right = Data::from(DataInner::TermSvg(
let right = Data::with_inner(DataInner::TermSvg(
"
irrelevant
<text>relevant
Expand All @@ -26,7 +26,7 @@ irrelevant"
));
assert_eq!(left, right);

let left = Data::from(DataInner::TermSvg(
let left = Data::with_inner(DataInner::TermSvg(
"
irrelevant 1
<text>relevant
Expand All @@ -35,7 +35,7 @@ irrelevant 1
irrelevant 1"
.to_owned(),
));
let right = Data::from(DataInner::TermSvg(
let right = Data::with_inner(DataInner::TermSvg(
"
irrelevant 2
<text>relevant
Expand All @@ -50,7 +50,7 @@ irrelevant 2"
#[test]
#[cfg(feature = "term-svg")]
fn term_svg_ne() {
let left = Data::from(DataInner::TermSvg(
let left = Data::with_inner(DataInner::TermSvg(
"
irrelevant 1
<text>relevant 1
Expand All @@ -59,7 +59,7 @@ irrelevant 1
irrelevant 1"
.to_owned(),
));
let right = Data::from(DataInner::TermSvg(
let right = Data::with_inner(DataInner::TermSvg(
"
irrelevant 2
<text>relevant 2
Expand Down
58 changes: 29 additions & 29 deletions crates/snapbox/src/filters/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,66 +55,66 @@ pub trait Filter {
pub struct FilterNewlines;
impl Filter for FilterNewlines {
fn filter(&self, data: Data) -> Data {
let mut new = match data.inner {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
let source = data.source;
let inner = match data.inner {
DataInner::Error(err) => DataInner::Error(err),
DataInner::Binary(bin) => DataInner::Binary(bin),
DataInner::Text(text) => {
let lines = crate::filters::normalize_lines(&text);
Data::text(lines)
DataInner::Text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::filters::normalize_lines);
Data::json(value)
DataInner::Json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
normalize_value(&mut value, crate::filters::normalize_lines);
DataInner::JsonLines(value).into()
DataInner::JsonLines(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::filters::normalize_lines(&text);
DataInner::TermSvg(lines).into()
DataInner::TermSvg(lines)
}
};
new.source = data.source;
new
Data { inner, source }
}
}

pub struct FilterPaths;
impl Filter for FilterPaths {
fn filter(&self, data: Data) -> Data {
let mut new = match data.inner {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
let source = data.source;
let inner = match data.inner {
DataInner::Error(err) => DataInner::Error(err),
DataInner::Binary(bin) => DataInner::Binary(bin),
DataInner::Text(text) => {
let lines = crate::filters::normalize_paths(&text);
Data::text(lines)
DataInner::Text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::filters::normalize_paths);
Data::json(value)
DataInner::Json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
normalize_value(&mut value, crate::filters::normalize_paths);
DataInner::JsonLines(value).into()
DataInner::JsonLines(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::filters::normalize_paths(&text);
DataInner::TermSvg(lines).into()
DataInner::TermSvg(lines)
}
};
new.source = data.source;
new
Data { inner, source }
}
}

Expand All @@ -134,15 +134,16 @@ impl<'a> FilterRedactions<'a> {

impl Filter for FilterRedactions<'_> {
fn filter(&self, data: Data) -> Data {
let mut new = match data.inner {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
let source = data.source;
let inner = match data.inner {
DataInner::Error(err) => DataInner::Error(err),
DataInner::Binary(bin) => DataInner::Binary(bin),
DataInner::Text(text) => {
if let Some(pattern) = self.pattern.render() {
let lines = self.substitutions.normalize(&text, &pattern);
Data::text(lines)
DataInner::Text(lines)
} else {
DataInner::Text(text).into()
DataInner::Text(text)
}
}
#[cfg(feature = "json")]
Expand All @@ -151,28 +152,27 @@ impl Filter for FilterRedactions<'_> {
if let DataInner::Json(exp) = &self.pattern.inner {
normalize_value_matches(&mut value, exp, self.substitutions);
}
Data::json(value)
DataInner::Json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
if let DataInner::Json(exp) = &self.pattern.inner {
normalize_value_matches(&mut value, exp, self.substitutions);
}
DataInner::JsonLines(value).into()
DataInner::JsonLines(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
if let Some(pattern) = self.pattern.render() {
let lines = self.substitutions.normalize(&text, &pattern);
DataInner::TermSvg(lines).into()
DataInner::TermSvg(lines)
} else {
DataInner::TermSvg(text).into()
DataInner::TermSvg(text)
}
}
};
new.source = data.source;
new
Data { inner, source }
}
}

Expand Down
4 changes: 2 additions & 2 deletions crates/snapbox/src/report/diff.rs
Original file line number Diff line number Diff line change
Expand Up @@ -486,8 +486,8 @@ Hello World
let mut actual_diff = String::new();
write_diff(
&mut actual_diff,
&crate::data::DataInner::TermSvg(expected.to_owned()).into(),
&crate::data::DataInner::TermSvg(actual.to_owned()).into(),
&crate::Data::with_inner(crate::data::DataInner::TermSvg(expected.to_owned())),
&crate::Data::with_inner(crate::data::DataInner::TermSvg(actual.to_owned())),
Some(&expected_name),
Some(&actual_name),
palette,
Expand Down