diff --git a/crates/snapbox/src/data/normalize.rs b/crates/snapbox/src/data/normalize.rs index a96c9c88..aa66972b 100644 --- a/crates/snapbox/src/data/normalize.rs +++ b/crates/snapbox/src/data/normalize.rs @@ -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() } }; @@ -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() } }; diff --git a/crates/snapbox/src/lib.rs b/crates/snapbox/src/lib.rs index cbdeeea6..1b7aeb9e 100644 --- a/crates/snapbox/src/lib.rs +++ b/crates/snapbox/src/lib.rs @@ -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; @@ -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 = std::result::Result; diff --git a/crates/snapbox/src/normalization/mod.rs b/crates/snapbox/src/normalization/mod.rs new file mode 100644 index 00000000..c0b6487c --- /dev/null +++ b/crates/snapbox/src/normalization/mod.rs @@ -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) -> impl Iterator { + 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) -> impl Iterator { + 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() +} diff --git a/crates/snapbox/src/substitutions.rs b/crates/snapbox/src/normalization/substitutions.rs similarity index 99% rename from crates/snapbox/src/substitutions.rs rename to crates/snapbox/src/normalization/substitutions.rs index 2ff2352a..f2291fe9 100644 --- a/crates/snapbox/src/substitutions.rs +++ b/crates/snapbox/src/normalization/substitutions.rs @@ -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), )), } } @@ -175,7 +175,7 @@ impl From for SubstitutionValue { } else { Self { inner: Some(SubstitutionValueInner::String( - crate::utils::normalize_text(&inner), + crate::normalization::normalize_text(&inner), )), } } diff --git a/crates/snapbox/src/utils/mod.rs b/crates/snapbox/src/utils/mod.rs index d5192419..8212959f 100644 --- a/crates/snapbox/src/utils/mod.rs +++ b/crates/snapbox/src/utils/mod.rs @@ -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) -> impl Iterator { - 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) -> impl Iterator { - 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() -} diff --git a/src/schema.rs b/src/schema.rs index 35afed44..26211df5 100644 --- a/src/schema.rs +++ b/src/schema.rs @@ -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()); @@ -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)?;