diff --git a/Cargo.lock b/Cargo.lock index 2e4285712..c86ee2357 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -95,7 +95,7 @@ version = "1.1.5" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -106,7 +106,7 @@ checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" dependencies = [ "anstyle", "once_cell_polyfill", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -779,7 +779,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "39cab71617ae0d63f51a36d69f866391735b51691dbda63cf6f96d042b63efeb" dependencies = [ "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -1073,9 +1073,9 @@ dependencies = [ [[package]] name = "granit-parser" -version = "0.0.3" +version = "0.0.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "f50ba32164f9e098d5da618776a32afbb32270adcbe3d3d006107dae11e37c91" +checksum = "14668345710c92cb04181d9268407067689327d4b055273e3c4179a0777ee6a1" dependencies = [ "arraydeque", "smallvec", @@ -1473,7 +1473,7 @@ checksum = "3640c1c38b8e4e43584d8df18be5fc6b0aa314ce6ebf51b53313d4306cca8e46" dependencies = [ "hermit-abi", "libc", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -1812,7 +1812,7 @@ version = "0.50.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "7957b9740744892f114936ab4a57b3f487491bbeafaf8083688b16841a4240e5" dependencies = [ - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -2487,7 +2487,7 @@ dependencies = [ "errno", "libc", "linux-raw-sys", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2544,7 +2544,7 @@ dependencies = [ "security-framework", "security-framework-sys", "webpki-root-certs", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -2682,9 +2682,9 @@ dependencies = [ [[package]] name = "serde-saphyr" -version = "0.0.27" +version = "0.0.28" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5897b4c3faadadd35fdb6689f015641f3bc481d5adaaac56231ea15aeb243db3" +checksum = "a15e3bc297370dc9c526bcf0986f29167a9286cfaad404f2f572b3074dabe5ec" dependencies = [ "ahash", "annotate-snippets", @@ -2694,7 +2694,7 @@ dependencies = [ "granit-parser", "nohash-hasher", "num-traits", - "serde", + "serde_core", "smallvec", "zmij", ] @@ -2857,7 +2857,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3a766e1110788c36f4fa1c2b71b387a7815aa65f88ce0229841826633d93723e" dependencies = [ "libc", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -2885,7 +2885,7 @@ dependencies = [ "cfg-if", "libc", "psm", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -3018,7 +3018,7 @@ dependencies = [ "getrandom 0.4.2", "once_cell", "rustix", - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] @@ -3028,7 +3028,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "230a1b821ccbd75b185820a1f1ff7b14d21da1e442e22c0863ea5f08771a8874" dependencies = [ "rustix", - "windows-sys 0.60.2", + "windows-sys 0.61.2", ] [[package]] @@ -3649,7 +3649,7 @@ version = "0.1.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "c2a7b1c03c876122aa43f3020e6c3c3ee5c05081c9a00739faf7503aeba10d22" dependencies = [ - "windows-sys 0.52.0", + "windows-sys 0.61.2", ] [[package]] diff --git a/Cargo.toml b/Cargo.toml index de0923c9d..51d0bcc27 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -84,7 +84,7 @@ serde_json = { version = "1.0.132", features = [ "unbounded_depth", ] } serde_stacker = { version = "0.1.12" } -serde-saphyr = { version = "0.0.27", default-features = false, features = [ +serde-saphyr = { version = "0.0.28", default-features = false, features = [ "deserialize", "serialize", ] } diff --git a/crates/prek/src/yaml.rs b/crates/prek/src/yaml.rs index 62489da4b..de4ea772f 100644 --- a/crates/prek/src/yaml.rs +++ b/crates/prek/src/yaml.rs @@ -4,42 +4,20 @@ // option. This file may not be copied, modified, or distributed // except according to those terms. -use std::fmt::Write; +use serde_saphyr::{DoubleQuoted, SingleQuoted}; /// Serialize a YAML scalar while preserving the caller's quote style. pub(crate) fn serialize_yaml_scalar(value: &str, quote: &str) -> anyhow::Result { - match quote { - "'" => Ok(format!("'{}'", escape_single_quoted(value))), - "\"" => Ok(format!("\"{}\"", escape_double_quoted(value))), - _ => { - let rendered = serde_saphyr::to_string(&value)?; - Ok(rendered.trim_end_matches('\n').to_owned()) - } - } -} - -/// YAML single-quoted strings escape a single quote by doubling it. -fn escape_single_quoted(value: &str) -> String { - value.replace('\'', "''") -} + let mut rendered = match quote { + "'" => serde_saphyr::to_string(&SingleQuoted(value))?, + "\"" => serde_saphyr::to_string(&DoubleQuoted(value))?, + _ => serde_saphyr::to_string(&value)?, + }; -/// YAML double-quoted strings use backslash escapes for control characters. -fn escape_double_quoted(value: &str) -> String { - let mut escaped = String::with_capacity(value.len()); - for ch in value.chars() { - match ch { - '\\' => escaped.push_str("\\\\"), - '"' => escaped.push_str("\\\""), - '\t' => escaped.push_str("\\t"), - '\n' => escaped.push_str("\\n"), - '\r' => escaped.push_str("\\r"), - c if c.is_control() => { - let _ = write!(escaped, "\\u{:04X}", c as u32); - } - c => escaped.push(c), - } + if rendered.ends_with('\n') { + rendered.pop(); } - escaped + Ok(rendered) } #[cfg(test)]