Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(snap): Don't panic on error #261

Merged
merged 2 commits into from
Feb 22, 2024
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
20 changes: 20 additions & 0 deletions crates/snapbox/src/data/format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
}
24 changes: 7 additions & 17 deletions crates/snapbox/src/data/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,8 @@ impl Data {
pub fn read_from(path: &std::path::Path, data_format: Option<DataFormat>) -> 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),
}
}

Expand All @@ -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),
}
Expand Down
20 changes: 12 additions & 8 deletions crates/snapbox/src/data/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand All @@ -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;
Expand Down
Loading