Skip to content

Commit

Permalink
Merge pull request #261 from epage/fixes
Browse files Browse the repository at this point in the history
fix(snap): Don't panic on error
  • Loading branch information
epage authored Feb 22, 2024
2 parents 7ac4423 + c47a7ac commit adba817
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 25 deletions.
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

0 comments on commit adba817

Please sign in to comment.