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
56 changes: 28 additions & 28 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -321,89 +321,89 @@ impl Data {
///
/// This is generally used on `actual` data to make it match `expected`
pub fn coerce_to(self, format: DataFormat) -> Self {
let mut data = match (self.inner, format) {
(DataInner::Error(inner), _) => inner.into(),
(inner, DataFormat::Error) => inner.into(),
(DataInner::Binary(inner), DataFormat::Binary) => Self::binary(inner),
(DataInner::Text(inner), DataFormat::Text) => Self::text(inner),
let source = self.source;
let inner = match (self.inner, format) {
(DataInner::Error(inner), _) => DataInner::Error(inner),
(inner, DataFormat::Error) => inner,
(DataInner::Binary(inner), DataFormat::Binary) => DataInner::Binary(inner),
(DataInner::Text(inner), DataFormat::Text) => DataInner::Text(inner),
#[cfg(feature = "json")]
(DataInner::Json(inner), DataFormat::Json) => Self::json(inner),
(DataInner::Json(inner), DataFormat::Json) => DataInner::Json(inner),
#[cfg(feature = "json")]
(DataInner::JsonLines(inner), DataFormat::JsonLines) => {
DataInner::JsonLines(inner).into()
}
(DataInner::JsonLines(inner), DataFormat::JsonLines) => DataInner::JsonLines(inner),
#[cfg(feature = "term-svg")]
(DataInner::TermSvg(inner), DataFormat::TermSvg) => inner.into(),
(DataInner::TermSvg(inner), DataFormat::TermSvg) => DataInner::TermSvg(inner),
(DataInner::Binary(inner), _) => {
if is_binary(&inner) {
Self::binary(inner)
DataInner::Binary(inner)
} else {
match String::from_utf8(inner) {
Ok(str) => {
let coerced = Self::text(str).coerce_to(format);
// if the Text cannot be coerced into the correct format
// reset it back to Binary
if coerced.format() != format {
let coerced = if coerced.format() != format {
coerced.coerce_to(DataFormat::Binary)
} else {
coerced
}
};
coerced.inner
}
Err(err) => {
let bin = err.into_bytes();
Self::binary(bin)
DataInner::Binary(bin)
}
}
}
}
#[cfg(feature = "json")]
(DataInner::Text(inner), DataFormat::Json) => {
match serde_json::from_str::<serde_json::Value>(&inner) {
Ok(json) => Self::json(json),
Err(_) => Self::text(inner),
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&inner) {
DataInner::Json(json)
} else {
DataInner::Text(inner)
}
}
#[cfg(feature = "json")]
(DataInner::Text(inner), DataFormat::JsonLines) => {
if let Ok(jsonlines) = parse_jsonlines(&inner) {
Self::jsonlines(jsonlines)
DataInner::JsonLines(serde_json::Value::Array(jsonlines))
} else {
Self::text(inner)
DataInner::Text(inner)
}
}
#[cfg(feature = "term-svg")]
(DataInner::Text(inner), DataFormat::TermSvg) => {
DataInner::TermSvg(anstyle_svg::Term::new().render_svg(&inner)).into()
DataInner::TermSvg(anstyle_svg::Term::new().render_svg(&inner))
}
(inner, DataFormat::Binary) => {
let remake: Self = inner.into();
Self::binary(remake.to_bytes().expect("error case handled"))
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();
if let Some(str) = remake.render() {
Self::text(str)
DataInner::Text(str)
} else {
remake
remake.inner
}
}
// reachable if more than one structured data format is enabled
#[allow(unreachable_patterns)]
#[cfg(feature = "json")]
(inner, DataFormat::Json) => inner.into(),
(inner, DataFormat::Json) => inner,
// reachable if more than one structured data format is enabled
#[allow(unreachable_patterns)]
#[cfg(feature = "json")]
(inner, DataFormat::JsonLines) => inner.into(),
(inner, DataFormat::JsonLines) => inner,
// reachable if more than one structured data format is enabled
#[allow(unreachable_patterns)]
#[cfg(feature = "term-svg")]
(inner, DataFormat::TermSvg) => inner.into(),
(inner, DataFormat::TermSvg) => inner,
};
data.source = self.source;
data
Self { inner, source }
}

/// Outputs the current `DataFormat` of the underlying data
Expand Down
2 changes: 1 addition & 1 deletion crates/snapbox/src/filters/redactions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ impl From<&'_ regex::Regex> for RedactedValue {

impl PartialOrd for RedactedValueInner {
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
self.as_cmp().partial_cmp(other.as_cmp())
Some(self.as_cmp().cmp(other.as_cmp()))
}
}

Expand Down