Skip to content

Commit 2a91e37

Browse files
committed
refactor(data): Be consistent in initializing source
This will make it easier to catch it when new fields are added
1 parent c07249c commit 2a91e37

File tree

1 file changed

+25
-26
lines changed

1 file changed

+25
-26
lines changed

crates/snapbox/src/data/mod.rs

Lines changed: 25 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -321,55 +321,55 @@ impl Data {
321321
///
322322
/// This is generally used on `actual` data to make it match `expected`
323323
pub fn coerce_to(self, format: DataFormat) -> Self {
324-
let mut data = match (self.inner, format) {
325-
(DataInner::Error(inner), _) => inner.into(),
326-
(inner, DataFormat::Error) => inner.into(),
327-
(DataInner::Binary(inner), DataFormat::Binary) => Self::binary(inner),
328-
(DataInner::Text(inner), DataFormat::Text) => Self::text(inner),
324+
let source = self.source;
325+
let inner = match (self.inner, format) {
326+
(DataInner::Error(inner), _) => DataInner::Error(inner),
327+
(inner, DataFormat::Error) => inner,
328+
(DataInner::Binary(inner), DataFormat::Binary) => DataInner::Binary(inner),
329+
(DataInner::Text(inner), DataFormat::Text) => DataInner::Text(inner),
329330
#[cfg(feature = "json")]
330-
(DataInner::Json(inner), DataFormat::Json) => Self::json(inner),
331+
(DataInner::Json(inner), DataFormat::Json) => DataInner::Json(inner),
331332
#[cfg(feature = "json")]
332-
(DataInner::JsonLines(inner), DataFormat::JsonLines) => {
333-
DataInner::JsonLines(inner).into()
334-
}
333+
(DataInner::JsonLines(inner), DataFormat::JsonLines) => DataInner::JsonLines(inner),
335334
#[cfg(feature = "term-svg")]
336-
(DataInner::TermSvg(inner), DataFormat::TermSvg) => inner.into(),
335+
(DataInner::TermSvg(inner), DataFormat::TermSvg) => DataInner::TermSvg(inner),
337336
(DataInner::Binary(inner), _) => {
338337
if is_binary(&inner) {
339-
Self::binary(inner)
338+
DataInner::Binary(inner)
340339
} else {
341340
match String::from_utf8(inner) {
342341
Ok(str) => {
343342
let coerced = Self::text(str).coerce_to(format);
344343
// if the Text cannot be coerced into the correct format
345344
// reset it back to Binary
346-
if coerced.format() != format {
345+
let coerced = if coerced.format() != format {
347346
coerced.coerce_to(DataFormat::Binary)
348347
} else {
349348
coerced
350-
}
349+
};
350+
coerced.inner
351351
}
352352
Err(err) => {
353353
let bin = err.into_bytes();
354-
Self::binary(bin)
354+
DataInner::Binary(bin)
355355
}
356356
}
357357
}
358358
}
359359
#[cfg(feature = "json")]
360360
(DataInner::Text(inner), DataFormat::Json) => {
361361
if let Ok(json) = serde_json::from_str::<serde_json::Value>(&inner) {
362-
Self::json(json)
362+
DataInner::Json(json)
363363
} else {
364-
Self::text(inner)
364+
DataInner::Text(inner)
365365
}
366366
}
367367
#[cfg(feature = "json")]
368368
(DataInner::Text(inner), DataFormat::JsonLines) => {
369369
if let Ok(jsonlines) = parse_jsonlines(&inner) {
370-
Self::jsonlines(jsonlines)
370+
DataInner::JsonLines(serde_json::Value::Array(jsonlines))
371371
} else {
372-
Self::text(inner)
372+
DataInner::Text(inner)
373373
}
374374
}
375375
#[cfg(feature = "term-svg")]
@@ -378,33 +378,32 @@ impl Data {
378378
}
379379
(inner, DataFormat::Binary) => {
380380
let remake: Self = inner.into();
381-
Self::binary(remake.to_bytes().expect("error case handled"))
381+
DataInner::Binary(remake.to_bytes().expect("error case handled"))
382382
}
383383
// This variant is already covered unless structured data is enabled
384384
#[cfg(feature = "structured-data")]
385385
(inner, DataFormat::Text) => {
386386
let remake: Self = inner.into();
387387
if let Some(str) = remake.render() {
388-
Self::text(str)
388+
DataInner::Text(str)
389389
} else {
390-
remake
390+
remake.inner
391391
}
392392
}
393393
// reachable if more than one structured data format is enabled
394394
#[allow(unreachable_patterns)]
395395
#[cfg(feature = "json")]
396-
(inner, DataFormat::Json) => inner.into(),
396+
(inner, DataFormat::Json) => inner,
397397
// reachable if more than one structured data format is enabled
398398
#[allow(unreachable_patterns)]
399399
#[cfg(feature = "json")]
400-
(inner, DataFormat::JsonLines) => inner.into(),
400+
(inner, DataFormat::JsonLines) => inner,
401401
// reachable if more than one structured data format is enabled
402402
#[allow(unreachable_patterns)]
403403
#[cfg(feature = "term-svg")]
404-
(inner, DataFormat::TermSvg) => inner.into(),
404+
(inner, DataFormat::TermSvg) => inner,
405405
};
406-
data.source = self.source;
407-
data
406+
Self { inner, source }
408407
}
409408

410409
/// Outputs the current `DataFormat` of the underlying data

0 commit comments

Comments
 (0)