diff --git a/crates/snapbox/src/data/format.rs b/crates/snapbox/src/data/format.rs index f22eec32..cc573b52 100644 --- a/crates/snapbox/src/data/format.rs +++ b/crates/snapbox/src/data/format.rs @@ -23,3 +23,23 @@ impl DataFormat { } } } + +impl From<&std::path::Path> for DataFormat { + fn from(path: &std::path::Path) -> Self { + let file_name = path + .file_name() + .and_then(|e| e.to_str()) + .unwrap_or_default(); + let (file_stem, mut ext) = file_name.split_once('.').unwrap_or((file_name, "")); + if file_stem.is_empty() { + (_, ext) = file_stem.split_once('.').unwrap_or((file_name, "")); + } + match ext { + #[cfg(feature = "json")] + "json" => DataFormat::Json, + #[cfg(feature = "term-svg")] + "term.svg" => Self::TermSvg, + _ => DataFormat::Text, + } + } +} diff --git a/crates/snapbox/src/data/mod.rs b/crates/snapbox/src/data/mod.rs index d9519698..20e9cca3 100644 --- a/crates/snapbox/src/data/mod.rs +++ b/crates/snapbox/src/data/mod.rs @@ -161,7 +161,8 @@ impl Data { pub fn read_from(path: &std::path::Path, data_format: Option) -> Self { match Self::try_read_from(path, data_format) { Ok(data) => data, - Err(err) => Self::error(err, data_format.unwrap_or_default()).with_path(path), + Err(err) => Self::error(err, data_format.unwrap_or_else(|| DataFormat::from(path))) + .with_path(path), } } @@ -176,25 +177,14 @@ impl Data { let data = match data_format { Some(df) => data.is(df), None => { - let file_name = path - .file_name() - .and_then(|e| e.to_str()) - .unwrap_or_default(); - let (file_stem, mut ext) = file_name.split_once('.').unwrap_or((file_name, "")); - if file_stem.is_empty() { - (_, ext) = file_stem.split_once('.').unwrap_or((file_name, "")); - } - match ext { + let inferred_format = DataFormat::from(path); + match inferred_format { #[cfg(feature = "json")] - "json" => data.coerce_to(DataFormat::Json), + DataFormat::Json => data.coerce_to(inferred_format), #[cfg(feature = "term-svg")] - "term.svg" => { + DataFormat::TermSvg => { let data = data.coerce_to(DataFormat::Text); - let inner = match data.inner { - DataInner::Text(text) => DataInner::TermSvg(text), - other => other, - }; - inner.into() + data.is(inferred_format) } _ => data.coerce_to(DataFormat::Text), } diff --git a/crates/snapbox/src/data/normalize.rs b/crates/snapbox/src/data/normalize.rs index ca0455ca..afa3229e 100644 --- a/crates/snapbox/src/data/normalize.rs +++ b/crates/snapbox/src/data/normalize.rs @@ -79,10 +79,12 @@ impl Normalize for NormalizeMatches<'_> { DataInner::Error(err) => err.into(), DataInner::Binary(bin) => Data::binary(bin), DataInner::Text(text) => { - let lines = self - .substitutions - .normalize(&text, &self.pattern.render().unwrap()); - Data::text(lines) + if let Some(pattern) = self.pattern.render() { + let lines = self.substitutions.normalize(&text, &pattern); + Data::text(lines) + } else { + DataInner::Text(text).into() + } } #[cfg(feature = "json")] DataInner::Json(value) => { @@ -94,10 +96,12 @@ impl Normalize for NormalizeMatches<'_> { } #[cfg(feature = "term-svg")] DataInner::TermSvg(text) => { - let lines = self - .substitutions - .normalize(&text, &self.pattern.render().unwrap()); - DataInner::TermSvg(lines).into() + if let Some(pattern) = self.pattern.render() { + let lines = self.substitutions.normalize(&text, &pattern); + DataInner::TermSvg(lines).into() + } else { + DataInner::TermSvg(text).into() + } } }; new.source = data.source;