Skip to content

Commit

Permalink
Merge pull request #282 from epage/norm
Browse files Browse the repository at this point in the history
fix(snap)!: Centralize logic into normalization mod
  • Loading branch information
epage authored Apr 20, 2024
2 parents d69ba66 + b52ab61 commit 5210887
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 42 deletions.
16 changes: 8 additions & 8 deletions crates/snapbox/src/data/normalize.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,24 +12,24 @@ impl Normalize for NormalizeNewlines {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
DataInner::Text(text) => {
let lines = crate::utils::normalize_lines(&text);
let lines = crate::normalization::normalize_lines(&text);
Data::text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::utils::normalize_lines);
normalize_value(&mut value, crate::normalization::normalize_lines);
Data::json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
normalize_value(&mut value, crate::utils::normalize_lines);
normalize_value(&mut value, crate::normalization::normalize_lines);
DataInner::JsonLines(value).into()
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::utils::normalize_lines(&text);
let lines = crate::normalization::normalize_lines(&text);
DataInner::TermSvg(lines).into()
}
};
Expand All @@ -45,24 +45,24 @@ impl Normalize for NormalizePaths {
DataInner::Error(err) => err.into(),
DataInner::Binary(bin) => Data::binary(bin),
DataInner::Text(text) => {
let lines = crate::utils::normalize_paths(&text);
let lines = crate::normalization::normalize_paths(&text);
Data::text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::utils::normalize_paths);
normalize_value(&mut value, crate::normalization::normalize_paths);
Data::json(value)
}
#[cfg(feature = "json")]
DataInner::JsonLines(value) => {
let mut value = value;
normalize_value(&mut value, crate::utils::normalize_paths);
normalize_value(&mut value, crate::normalization::normalize_paths);
DataInner::JsonLines(value).into()
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::utils::normalize_paths(&text);
let lines = crate::normalization::normalize_paths(&text);
DataInner::TermSvg(lines).into()
}
};
Expand Down
5 changes: 2 additions & 3 deletions crates/snapbox/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,10 +98,10 @@ mod action;
mod assert;
mod error;
mod macros;
mod substitutions;

pub mod cmd;
pub mod data;
pub mod normalization;
pub mod path;
pub mod report;
pub mod utils;
Expand All @@ -115,9 +115,8 @@ pub use assert::Assert;
pub use data::Data;
pub use data::ToDebug;
pub use error::Error;
pub use normalization::Substitutions;
pub use snapbox_macros::debug;
pub use substitutions::SubstitutionValue;
pub use substitutions::Substitutions;

pub type Result<T, E = Error> = std::result::Result<T, E>;

Expand Down
33 changes: 33 additions & 0 deletions crates/snapbox/src/normalization/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
mod substitutions;

#[cfg(feature = "regex")]
pub use regex::Regex;
pub use substitutions::SubstitutionValue;
pub use substitutions::Substitutions;

/// Normalize line endings
pub fn normalize_lines(data: &str) -> String {
normalize_lines_chars(data.chars()).collect()
}

fn normalize_lines_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item = char> {
normalize_line_endings::normalized(data)
}

/// Normalize path separators
pub fn normalize_paths(data: &str) -> String {
normalize_paths_chars(data.chars()).collect()
}

fn normalize_paths_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item = char> {
data.map(|c| if c == '\\' { '/' } else { c })
}

/// "Smart" text normalization
///
/// This includes
/// - Line endings
/// - Path separators
pub fn normalize_text(data: &str) -> String {
normalize_paths_chars(normalize_lines_chars(data.chars())).collect()
}
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ impl From<&'static str> for SubstitutionValue {
} else {
Self {
inner: Some(SubstitutionValueInner::String(
crate::utils::normalize_text(inner),
crate::normalization::normalize_text(inner),
)),
}
}
Expand All @@ -175,7 +175,7 @@ impl From<String> for SubstitutionValue {
} else {
Self {
inner: Some(SubstitutionValueInner::String(
crate::utils::normalize_text(&inner),
crate::normalization::normalize_text(&inner),
)),
}
}
Expand Down
27 changes: 0 additions & 27 deletions crates/snapbox/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -1,30 +1,3 @@
mod lines;

pub use lines::LinesWithTerminator;

/// Normalize line endings
pub fn normalize_lines(data: &str) -> String {
normalize_lines_chars(data.chars()).collect()
}

fn normalize_lines_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item = char> {
normalize_line_endings::normalized(data)
}

/// Normalize path separators
pub fn normalize_paths(data: &str) -> String {
normalize_paths_chars(data.chars()).collect()
}

fn normalize_paths_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item = char> {
data.map(|c| if c == '\\' { '/' } else { c })
}

/// "Smart" text normalization
///
/// This includes
/// - Line endings
/// - Path separators
pub fn normalize_text(data: &str) -> String {
normalize_paths_chars(normalize_lines_chars(data.chars())).collect()
}
4 changes: 2 additions & 2 deletions src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ impl TryCmd {
} else if ext == std::ffi::OsStr::new("trycmd") || ext == std::ffi::OsStr::new("md") {
let raw = std::fs::read_to_string(path)
.map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
let normalized = snapbox::utils::normalize_lines(&raw);
let normalized = snapbox::normalization::normalize_lines(&raw);
Self::parse_trycmd(&normalized)?
} else {
return Err(format!("Unsupported extension: {}", ext.to_string_lossy()).into());
Expand Down Expand Up @@ -159,7 +159,7 @@ impl TryCmd {

let raw = std::fs::read_to_string(path)
.map_err(|e| format!("Failed to read {}: {}", path.display(), e))?;
let mut normalized = snapbox::utils::normalize_lines(&raw);
let mut normalized = snapbox::normalization::normalize_lines(&raw);

overwrite_trycmd_status(exit, step, &mut line_nums, &mut normalized)?;

Expand Down

0 comments on commit 5210887

Please sign in to comment.