Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Only overwrite Codable.swift if it has changed. #205

Merged
merged 2 commits into from
Nov 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading