Skip to content

Commit

Permalink
fix(filter): Move in normalize_lines/paths
Browse files Browse the repository at this point in the history
Cherry pick b52ab61 (assert-rs#282)
  • Loading branch information
epage committed May 15, 2024
1 parent 31ab285 commit c3a4115
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
30 changes: 24 additions & 6 deletions crates/snapbox/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,25 +27,34 @@ impl Filter for FilterNewlines {
DataInner::Error(err) => DataInner::Error(err),
DataInner::Binary(bin) => DataInner::Binary(bin),
DataInner::Text(text) => {
let lines = crate::utils::normalize_lines(&text);
let lines = normalize_lines(&text);
DataInner::Text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::utils::normalize_lines);
normalize_value(&mut value, normalize_lines);
DataInner::Json(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::utils::normalize_lines(&text);
let lines = normalize_lines(&text);
DataInner::TermSvg(lines)
}
};
Data { inner, source }
}
}

/// 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)
}

pub struct FilterPaths;
impl Filter for FilterPaths {
fn normalize(&self, data: Data) -> Data {
Expand All @@ -54,25 +63,34 @@ impl Filter for FilterPaths {
DataInner::Error(err) => DataInner::Error(err),
DataInner::Binary(bin) => DataInner::Binary(bin),
DataInner::Text(text) => {
let lines = crate::utils::normalize_paths(&text);
let lines = normalize_paths(&text);
DataInner::Text(lines)
}
#[cfg(feature = "json")]
DataInner::Json(value) => {
let mut value = value;
normalize_value(&mut value, crate::utils::normalize_paths);
normalize_value(&mut value, normalize_paths);
DataInner::Json(value)
}
#[cfg(feature = "term-svg")]
DataInner::TermSvg(text) => {
let lines = crate::utils::normalize_paths(&text);
let lines = normalize_paths(&text);
DataInner::TermSvg(lines)
}
};
Data { inner, source }
}
}

/// 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 })
}

pub struct FilterMatches<'a> {
substitutions: &'a crate::Substitutions,
pattern: &'a Data,
Expand Down
33 changes: 13 additions & 20 deletions crates/snapbox/src/utils/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,21 @@ mod lines;

pub use lines::LinesWithTerminator;

/// Normalize line endings
pub fn normalize_lines(data: &str) -> String {
normalize_lines_chars(data.chars()).collect()
}
#[doc(inline)]
pub use crate::cargo_rustc_current_dir;
#[doc(inline)]
pub use crate::current_dir;
#[doc(inline)]
pub use crate::current_rs;

fn normalize_lines_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item = char> {
normalize_line_endings::normalized(data)
#[deprecated(since = "0.5.11", note = "Replaced with `filter::normalize_lines`")]
pub fn normalize_lines(data: &str) -> String {
crate::filter::normalize_lines(data)
}

/// Normalize path separators
#[deprecated(since = "0.5.11", note = "Replaced with `filter::normalize_paths`")]
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 })
crate::filter::normalize_paths(data)
}

/// "Smart" text normalization
Expand All @@ -26,12 +25,6 @@ fn normalize_paths_chars(data: impl Iterator<Item = char>) -> impl Iterator<Item
/// - Line endings
/// - Path separators
pub fn normalize_text(data: &str) -> String {
normalize_paths_chars(normalize_lines_chars(data.chars())).collect()
#[allow(deprecated)]
normalize_paths(&normalize_lines(data))
}

#[doc(inline)]
pub use crate::cargo_rustc_current_dir;
#[doc(inline)]
pub use crate::current_dir;
#[doc(inline)]
pub use crate::current_rs;

0 comments on commit c3a4115

Please sign in to comment.