Skip to content
Merged
Show file tree
Hide file tree
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
8 changes: 4 additions & 4 deletions cli/src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ impl Callback for FormatContext {
let mut extension = doc.filepath.extension().unwrap_or_default().to_os_string();
extension.push(".formatted");
let copied = doc.filepath.with_extension(extension);
doc.save(Some(&copied))
doc.save_to(copied)
} else {
doc.save(None)
doc.save()
}
}
}
Expand Down Expand Up @@ -234,9 +234,9 @@ impl RemoveContext {
let mut extension = doc.filepath.extension().unwrap_or_default().to_os_string();
extension.push(".removed");
let copied = doc.filepath.with_extension(extension);
doc.save(Some(&copied))
doc.save_to(copied)
} else {
doc.save(None)
doc.save()
}
}
}
Expand Down
11 changes: 9 additions & 2 deletions fmt/src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::collections::HashMap;
use std::fs;
use std::fs::File;
use std::io::BufRead;
use std::path::Path;
use std::path::PathBuf;

use exn::ErrorExt;
Expand Down Expand Up @@ -164,8 +165,14 @@ impl Document {
}
}

pub fn save(&mut self, filepath: Option<&PathBuf>) -> Result<(), Error> {
let filepath = filepath.unwrap_or(&self.filepath);
pub fn save(&mut self) -> Result<(), Error> {
let filepath = self.filepath.as_path();
fs::write(filepath, self.parser.file_content.content())
.or_raise(|| Error::new(format!("cannot save document {}", filepath.display())))
}

pub fn save_to(&mut self, filepath: impl AsRef<Path>) -> Result<(), Error> {
let filepath = filepath.as_ref();
fs::write(filepath, self.parser.file_content.content())
.or_raise(|| Error::new(format!("cannot save document {}", filepath.display())))
}
Comment on lines +168 to 178

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually we don't need &mut self but &self here. But use &mut for write operation is fine and it's good to distinguish save/save_to.

Expand Down
Loading