From 24de0c37c1fb6c862f701e597edaac6eac8ec9c4 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Mon, 22 Apr 2024 13:39:42 -0500 Subject: [PATCH] refactor(data): Make it easier to tell when new fields are added to Data --- crates/snapbox/src/data/mod.rs | 46 +++++++++--------------- crates/snapbox/src/data/tests.rs | 12 +++---- crates/snapbox/src/filters/mod.rs | 58 +++++++++++++++---------------- crates/snapbox/src/report/diff.rs | 4 +-- 4 files changed, 54 insertions(+), 66 deletions(-) diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index 826a0e07..b7b5a51d 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -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>) -> 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) -> Self { - DataInner::Text(raw.into()).into() + Self::with_inner(DataInner::Text(raw.into())) } #[cfg(feature = "json")] pub fn json(raw: impl Into) -> Self { - DataInner::Json(raw.into()).into() + Self::with_inner(DataInner::Json(raw.into())) } #[cfg(feature = "json")] pub fn jsonlines(raw: impl Into>) -> 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, intended: DataFormat) -> Self { - DataError { + Self::with_inner(DataInner::Error(DataError { error: raw.into(), intended, - } - .into() + })) } /// Empty test data @@ -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()); @@ -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 { @@ -438,24 +444,6 @@ impl Data { } } -impl From for Data { - fn from(inner: DataInner) -> Self { - Data { - inner, - source: None, - } - } -} - -impl From 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 { diff --git a/crates/snapbox/src/data/tests.rs b/crates/snapbox/src/data/tests.rs index 7d8cc7c5..79139bf3 100644 --- a/crates/snapbox/src/data/tests.rs +++ b/crates/snapbox/src/data/tests.rs @@ -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 relevant @@ -15,7 +15,7 @@ irrelevant irrelevant" .to_owned(), )); - let right = Data::from(DataInner::TermSvg( + let right = Data::with_inner(DataInner::TermSvg( " irrelevant relevant @@ -26,7 +26,7 @@ irrelevant" )); assert_eq!(left, right); - let left = Data::from(DataInner::TermSvg( + let left = Data::with_inner(DataInner::TermSvg( " irrelevant 1 relevant @@ -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 relevant @@ -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 relevant 1 @@ -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 relevant 2 diff --git a/crates/snapbox/src/filters/mod.rs b/crates/snapbox/src/filters/mod.rs index 39eeb895..b62c87f5 100644 --- a/crates/snapbox/src/filters/mod.rs +++ b/crates/snapbox/src/filters/mod.rs @@ -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 } } } @@ -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")] @@ -151,7 +152,7 @@ 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) => { @@ -159,20 +160,19 @@ impl Filter for FilterRedactions<'_> { 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 } } } diff --git a/crates/snapbox/src/report/diff.rs b/crates/snapbox/src/report/diff.rs index 6e8bd1cf..30b63005 100644 --- a/crates/snapbox/src/report/diff.rs +++ b/crates/snapbox/src/report/diff.rs @@ -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,