Skip to content

Commit

Permalink
refactor(sourcemap): avoid passing Results
Browse files Browse the repository at this point in the history
  • Loading branch information
overlookmotel committed Jul 29, 2024
1 parent d6974d4 commit a229c0b
Showing 1 changed file with 15 additions and 16 deletions.
31 changes: 15 additions & 16 deletions crates/oxc_sourcemap/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,10 @@ pub fn encode_to_string(sourcemap: &SourceMap) -> Result<String, Error> {
}

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 {
Expand All @@ -64,7 +63,7 @@ pub fn encode_to_string(sourcemap: &SourceMap) -> Result<String, Error> {
.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));
}
Expand All @@ -73,8 +72,7 @@ pub fn encode_to_string(sourcemap: &SourceMap) -> Result<String, Error> {

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());
Expand Down Expand Up @@ -211,20 +209,19 @@ impl<'a> PreAllocatedString<'a> {
}

#[inline]
fn push_list<I>(&mut self, mut iter: I) -> Result<(), Error>
fn push_list<I>(&mut self, mut iter: I)
where
I: Iterator<Item = Result<String, Error>>,
I: Iterator<Item = String>,
{
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]
Expand All @@ -239,12 +236,14 @@ impl<'a> PreAllocatedString<'a> {
}
}

fn escape_json_string<S: AsRef<str>>(s: S) -> Result<String, Error> {
fn escape_json_string<S: AsRef<str>>(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<u8>` 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]
Expand All @@ -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);
}
}

Expand Down

0 comments on commit a229c0b

Please sign in to comment.