Skip to content

Commit

Permalink
fix: improve JSON parsing by cleaning up version strings
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefan-Ethernal committed Jan 21, 2025
1 parent 04b8363 commit 62fb775
Showing 1 changed file with 7 additions and 9 deletions.
16 changes: 7 additions & 9 deletions crates/cdk/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,14 +79,12 @@ fn build_versions() -> io::Result<()> {
let re_comments = Regex::new(r"#.*$").unwrap(); // Regex to remove comments
let re_trailing_commas = Regex::new(r",(\s*})").unwrap(); // Regex to fix trailing commas

// The versions string is a JSON object we can parse
let versions_json: serde_json::Value = match serde_json::from_str(&versions) {
Ok(json) => json,
Err(e) => {
eprintln!("Failed to parse JSON: {}", e);
return Err(std::io::Error::new(std::io::ErrorKind::Other, "Failed to parse JSON"));
}
};
let cleaned_versions = raw_versions
.lines()
.map(|line| re_comments.replace_all(line, "").trim().to_string()) // Remove comments and trim spaces
.filter(|line| !line.is_empty()) // Filter out empty lines
.collect::<Vec<_>>()
.join("\n");

// Fix improperly placed trailing commas
let cleaned_versions = re_trailing_commas.replace_all(&cleaned_versions, "$1");
Expand All @@ -113,4 +111,4 @@ fn build_versions() -> io::Result<()> {
)?;

Ok(())
}
}

0 comments on commit 62fb775

Please sign in to comment.