From c3a41151e78a789850fd3336be34c4244c696b88 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Wed, 15 May 2024 16:03:34 -0500 Subject: [PATCH] fix(filter): Move in normalize_lines/paths Cherry pick b52ab61ee860423ec5d900956cedcd41ff67663f (#282) --- crates/snapbox/src/filter/mod.rs | 30 +++++++++++++++++++++++------ crates/snapbox/src/utils/mod.rs | 33 +++++++++++++------------------- 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/crates/snapbox/src/filter/mod.rs b/crates/snapbox/src/filter/mod.rs index 3a4089fa..88a55517 100644 --- a/crates/snapbox/src/filter/mod.rs +++ b/crates/snapbox/src/filter/mod.rs @@ -27,18 +27,18 @@ 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) } }; @@ -46,6 +46,15 @@ impl Filter for FilterNewlines { } } +/// Normalize line endings +pub fn normalize_lines(data: &str) -> String { + normalize_lines_chars(data.chars()).collect() +} + +fn normalize_lines_chars(data: impl Iterator) -> impl Iterator { + normalize_line_endings::normalized(data) +} + pub struct FilterPaths; impl Filter for FilterPaths { fn normalize(&self, data: Data) -> Data { @@ -54,18 +63,18 @@ 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) } }; @@ -73,6 +82,15 @@ impl Filter for FilterPaths { } } +/// Normalize path separators +pub fn normalize_paths(data: &str) -> String { + normalize_paths_chars(data.chars()).collect() +} + +fn normalize_paths_chars(data: impl Iterator) -> impl Iterator { + data.map(|c| if c == '\\' { '/' } else { c }) +} + pub struct FilterMatches<'a> { substitutions: &'a crate::Substitutions, pattern: &'a Data, diff --git a/crates/snapbox/src/utils/mod.rs b/crates/snapbox/src/utils/mod.rs index 75a990dc..4ddd2243 100644 --- a/crates/snapbox/src/utils/mod.rs +++ b/crates/snapbox/src/utils/mod.rs @@ -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) -> impl Iterator { - 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) -> impl Iterator { - data.map(|c| if c == '\\' { '/' } else { c }) + crate::filter::normalize_paths(data) } /// "Smart" text normalization @@ -26,12 +25,6 @@ fn normalize_paths_chars(data: impl Iterator) -> impl Iterator 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;