eprint dont err on conflict

master
Bel LaPointe 2024-06-14 09:24:05 -06:00
parent e389f34bd6
commit 41cab74028
1 changed files with 11 additions and 13 deletions

View File

@ -167,22 +167,14 @@ impl TasksAndMetadata {
}
}
pub fn save(&self, dry_run: bool) -> Result<(), String> {
fn save(&self, dry_run: bool) -> Result<(), String> {
let version = file_version(self.file.clone())?;
let mut file = self.file.clone();
if version != self.version {
file = format!("{}.{}", &self.file, &self.version.to_string());
file = format!("{}.{}", &self.file, TS::now().to_string());
eprintln!("saving conflicting {} as {}", &self.file, &file);
}
self.save_as(file.clone(), dry_run)?;
match file == self.file {
true => Ok(()),
false => Err(format!("{} has been updated", self.file)),
}
}
fn save_as(&self, file: String, dry_run: bool) -> Result<(), String> {
match dry_run {
true => {
match &file == &self.file {
@ -193,10 +185,16 @@ impl TasksAndMetadata {
Ok(())
},
false => {
match std::fs::File::create(&file) {
let tmpf = format!("{}.{}", &file, TS::now().to_string());
match std::fs::File::create(&tmpf) {
Ok(mut f) => {
match f.write_all(self.tasks.to_string().as_bytes()) {
Ok(_) => Ok(()),
Ok(_) => {
match std::fs::rename(&tmpf, &file) {
Ok(_) => Ok(()),
Err(msg) => Err(format!("failed to flush {}: {}", file, msg)),
}
},
Err(msg) => Err(format!("failed to write {}: {}", file, msg)),
}
},