Skip to content

Commit

Permalink
feat(file): change "file_contents" to not be public
Browse files Browse the repository at this point in the history
this is required so "modified" tracking works properly
  • Loading branch information
hasezoey committed Sep 30, 2023
1 parent aa9057f commit 78dbb4b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 3 deletions.
14 changes: 13 additions & 1 deletion src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::path::PathBuf;

pub struct MarkedFile {
/// File contents that were read / need to be written
pub file_contents: String,
file_contents: String,
/// Path of the read / to write file
pub path: PathBuf,
modified: bool,
Expand Down Expand Up @@ -31,6 +31,10 @@ impl MarkedFile {
})
}

pub fn get_file_contents(&self) -> &str {
&self.file_contents
}

pub fn is_modified(&self) -> bool {
self.modified
}
Expand All @@ -43,6 +47,14 @@ impl MarkedFile {
self.file_contents.contains(&format!("pub mod {mod_name};"))
}

pub fn change_file_contents(&mut self, new_content: String) {
if !self.modified && self.file_contents != new_content {
self.modified = true;
}

self.file_contents = new_content;
}

pub fn add_use_stmt(&mut self, use_name: &str) {
self.file_contents = self.file_contents.trim().to_string();
if !self.file_contents.is_empty() {
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ pub fn generate_files(
let mut table_mod_rs = MarkedFile::new(table_dir.join("mod.rs"))?;

table_generated_rs.ensure_file_signature()?;
table_generated_rs.file_contents = table.generated_code.clone();
table_generated_rs.change_file_contents(table.generated_code.clone());
table_generated_rs.write()?;

file_changes.push(FileChange::from(&table_generated_rs));
Expand Down Expand Up @@ -340,7 +340,7 @@ pub fn generate_files(
table_mod_rs.remove_use_stmt("generated::*");
table_mod_rs.write()?;

if table_mod_rs.file_contents.trim().is_empty() {
if table_mod_rs.get_file_contents().trim().is_empty() {
let table_mod_rs = table_mod_rs.delete()?;
file_changes.push(FileChange::new(table_mod_rs, FileChangeStatus::Deleted));
} else {
Expand Down

0 comments on commit 78dbb4b

Please sign in to comment.