Skip to content

Commit

Permalink
refactor(data): Compiler-guided Data fields updates
Browse files Browse the repository at this point in the history
Cherry pick 24de0c3 (assert-rs#289)
  • Loading branch information
epage committed May 15, 2024
1 parent 4e2e793 commit 6177a6e
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 62 deletions.
44 changes: 16 additions & 28 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,27 +114,33 @@ 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()))
}

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 @@ -284,13 +290,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 @@ -351,13 +357,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 @@ -414,24 +420,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
52 changes: 26 additions & 26 deletions crates/snapbox/src/data/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,54 +8,54 @@ pub trait Normalize {
pub struct NormalizeNewlines;
impl Normalize for NormalizeNewlines {
fn normalize(&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::utils::normalize_lines(&text);
Data::text(lines)
DataInner::Text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::utils::normalize_lines);
Data::json(value)
DataInner::Json(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::utils::normalize_lines(&text);
DataInner::TermSvg(lines).into()
DataInner::TermSvg(lines)
}
};
new.source = data.source;
new
Data { inner, source }
}
}

pub struct NormalizePaths;
impl Normalize for NormalizePaths {
fn normalize(&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::utils::normalize_paths(&text);
Data::text(lines)
DataInner::Text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::utils::normalize_paths);
Data::json(value)
DataInner::Json(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::utils::normalize_paths(&text);
DataInner::TermSvg(lines).into()
DataInner::TermSvg(lines)
}
};
new.source = data.source;
new
Data { inner, source }
}
}

Expand All @@ -75,15 +75,16 @@ impl<'a> NormalizeMatches<'a> {

impl Normalize for NormalizeMatches<'_> {
fn normalize(&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 @@ -92,20 +93,19 @@ impl Normalize for NormalizeMatches<'_> {
if let DataInner::Json(exp) = &self.pattern.inner {
normalize_value_matches(&mut value, exp, self.substitutions);
}
Data::json(value)
DataInner::Json(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
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
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

0 comments on commit 6177a6e

Please sign in to comment.