From a229c0bfa211f299465a3a4555e48a56fe05abed Mon Sep 17 00:00:00 2001 From: overlookmotel Date: Mon, 29 Jul 2024 20:32:55 +0100 Subject: [PATCH] refactor(sourcemap): avoid passing `Result`s --- crates/oxc_sourcemap/src/encode.rs | 31 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 16 deletions(-) diff --git a/crates/oxc_sourcemap/src/encode.rs b/crates/oxc_sourcemap/src/encode.rs index 88a7ed1028429b..dc403b7882e754 100644 --- a/crates/oxc_sourcemap/src/encode.rs +++ b/crates/oxc_sourcemap/src/encode.rs @@ -49,11 +49,10 @@ pub fn encode_to_string(sourcemap: &SourceMap) -> Result { } contents.push("\"names\":[".into()); - contents.push_list(sourcemap.names.iter().map(escape_json_string))?; + contents.push_list(sourcemap.names.iter().map(escape_json_string)); contents.push("],\"sources\":[".into()); - - contents.push_list(sourcemap.sources.iter().map(escape_json_string))?; + contents.push_list(sourcemap.sources.iter().map(escape_json_string)); // Quote `source_content` in parallel if let Some(source_contents) = &sourcemap.source_contents { @@ -64,7 +63,7 @@ pub fn encode_to_string(sourcemap: &SourceMap) -> Result { .par_iter() .map(escape_json_string) .collect(); - contents.push_list(quoted_source_contents.into_iter())?; + contents.push_list(quoted_source_contents.into_iter()); } else { contents.push_list(source_contents.iter().map(escape_json_string)); } @@ -73,8 +72,7 @@ pub fn encode_to_string(sourcemap: &SourceMap) -> Result { if let Some(x_google_ignore_list) = &sourcemap.x_google_ignore_list { contents.push("],\"x_google_ignoreList\":[".into()); - contents - .push_list(x_google_ignore_list.iter().map(std::string::ToString::to_string).map(Ok))?; + contents.push_list(x_google_ignore_list.iter().map(ToString::to_string)); } contents.push("],\"mappings\":\"".into()); @@ -211,20 +209,19 @@ impl<'a> PreAllocatedString<'a> { } #[inline] - fn push_list(&mut self, mut iter: I) -> Result<(), Error> + fn push_list(&mut self, mut iter: I) where - I: Iterator>, + I: Iterator, { let Some(first) = iter.next() else { - return Ok(()); + return; }; - self.push(Cow::Owned(first?)); + self.push(Cow::Owned(first)); for other in iter { self.push(Cow::Borrowed(",")); - self.push(Cow::Owned(other?)); + self.push(Cow::Owned(other)); } - Ok(()) } #[inline] @@ -239,12 +236,14 @@ impl<'a> PreAllocatedString<'a> { } } -fn escape_json_string>(s: S) -> Result { +fn escape_json_string>(s: S) -> String { let s = s.as_ref(); let mut escaped_buf = Vec::with_capacity(s.len() * 2 + 2); - serde::Serialize::serialize(s, &mut serde_json::Serializer::new(&mut escaped_buf))?; + // This call is infallible as only error it can return is if the writer errors. + // Writing to a `Vec` is infallible, so that's not possible here. + serde::Serialize::serialize(s, &mut serde_json::Serializer::new(&mut escaped_buf)).unwrap(); // Safety: `escaped_buf` is valid utf8. - Ok(unsafe { String::from_utf8_unchecked(escaped_buf) }) + unsafe { String::from_utf8_unchecked(escaped_buf) } } #[test] @@ -267,7 +266,7 @@ fn test_escape_json_string() { for (c, expected) in FIXTURES { let mut input = String::new(); input.push(*c); - assert_eq!(escape_json_string(input).unwrap(), *expected); + assert_eq!(escape_json_string(input), *expected); } }