Skip to content

Commit

Permalink
Only overwrite Codable.swift if it has changed. (#205)
Browse files Browse the repository at this point in the history
* Only overwrite Codable if it has changed.

* Avoid cloning expected output.
  • Loading branch information
kevinbhayes authored Nov 12, 2024
1 parent 41de151 commit 330a40f
Showing 1 changed file with 20 additions and 13 deletions.
33 changes: 20 additions & 13 deletions core/src/language/swift.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use lazy_format::lazy_format;
use std::{
borrow::Cow,
collections::{BTreeSet, HashMap},
fs::File,
fs,
io::{self, Write},
path::Path,
sync::atomic::{AtomicBool, Ordering},
Expand Down Expand Up @@ -231,7 +231,7 @@ impl Language for Swift {

fn end_file(&mut self, w: &mut dyn Write) -> io::Result<()> {
if self.should_emit_codable_void.load(Ordering::SeqCst) && !self.multi_file {
self.write_codable(w)?;
self.write_codable(w, &self.get_codable_contents())?;
}

Ok(())
Expand Down Expand Up @@ -756,18 +756,20 @@ impl Swift {
/// When using multiple file generation we write this into a separate module vs at the
/// end of the generated file.
fn write_codable_file(&self, output_folder: &str) -> std::io::Result<()> {
let mut w = File::create(Path::new(output_folder).join("Codable.swift"))?;
self.write_codable(&mut w)
}
let output_string = self.get_codable_contents();
let output_path = Path::new(output_folder).join("Codable.swift");

/// Write the `CodableVoid` type.
fn write_codable(&self, w: &mut dyn Write) -> io::Result<()> {
writeln!(w)?;
writeln!(
w,
r"/// () isn't codable, so we use this instead to represent Rust's unit type"
)?;
if let Ok(buf) = fs::read(&output_path) {
if buf == output_string.as_bytes() {
return Ok(());
}
}

let mut w = fs::File::create(output_path)?;
self.write_codable(&mut w, &output_string)
}

fn get_codable_contents(&self) -> String {
let mut decs = self
.get_default_decorators()
.chain(self.codablevoid_constraints.iter().map(|s| s.as_str()))
Expand All @@ -778,7 +780,12 @@ impl Swift {
decs.push(CODABLE);
}

writeln!(w, "public struct CodableVoid: {} {{}}", decs.join(", "))
format!("\n/// () isn't codable, so we use this instead to represent Rust's unit type\npublic struct CodableVoid: {} {{}}", decs.join(", "))
}

/// Write the `CodableVoid` type.
fn write_codable(&self, w: &mut dyn Write, output_string: &str) -> io::Result<()> {
writeln!(w, "{}", output_string)
}

/// Build the generic constraints output. This checks for the `swiftGenericConstraints` typeshare attribute and combines
Expand Down

0 comments on commit 330a40f

Please sign in to comment.