Ensure that files are not overwritten when uwu'ifying

pull/1/head
sgoudham 3 years ago
parent db51a18c2a
commit 1e96fa989d

@ -13,11 +13,11 @@ struct Args {
text: Option<String>, text: Option<String>,
/// The file to uwu'ify /// The file to uwu'ify
#[clap(short, long, parse(from_os_str), conflicts_with = "text", requires = "outfile", value_name = "FILE", display_order = 2)] #[clap(short, long, parse(from_os_str), conflicts_with = "text", requires = "outfile", value_name = "FILE", value_hint = clap::ValueHint::FilePath, display_order = 2)]
infile: Option<std::path::PathBuf>, infile: Option<std::path::PathBuf>,
/// The file to output uwu'ified text /// The file to output uwu'ified text
#[clap(short, long, value_name = "FILE", display_order = 3)] #[clap(short, long, value_name = "FILE", value_hint = clap::ValueHint::FilePath, display_order = 3)]
outfile: Option<String>, outfile: Option<String>,
/// The modifier to determine how many words to be uwu'ified /// The modifier to determine how many words to be uwu'ified

@ -1,4 +1,4 @@
use std::io::Error; use std::error::Error;
use std::path::PathBuf; use std::path::PathBuf;
use linkify::{LinkFinder, LinkKind}; use linkify::{LinkFinder, LinkKind};
@ -59,22 +59,27 @@ impl UwUify {
} }
} }
pub fn uwuify(&self) -> Result<(), Error> { pub fn uwuify(&self) -> Result<(), Box<dyn Error>> {
// Handle Text // Handle Text
if !self.text.is_empty() { if !self.text.is_empty() {
let uwu_text = self.uwuify_sentence(&self.text); let uwu_text = self.uwuify_sentence(&self.text);
// Handle Text Output // Handle Text Output
if !self.output.is_empty() { if !self.output.is_empty() {
if UwUOutFile::exists(&self.output) {
return Err(format!("File '{}' already exists, aborting operation", &self.output).into());
}
let mut uwu_out_file = match UwUOutFile::new(&self.output) { let mut uwu_out_file = match UwUOutFile::new(&self.output) {
Ok(uwu_out_file) => uwu_out_file, Ok(uwu_out_file) => uwu_out_file,
Err(err) => return Err(err) Err(err) => return Err(err.into())
}; };
let mut uwu_progress_bar = UwUProgressBar::new(uwu_text.len() as u64); let mut uwu_progress_bar = UwUProgressBar::new(uwu_text.len() as u64);
match uwu_out_file.write_string(&uwu_text) { match uwu_out_file.write_string(&uwu_text) {
Ok(_) => (), Ok(_) => (),
Err(err) => return Err(err), Err(err) => return Err(err.into()),
}; };
uwu_progress_bar.update_progess(uwu_text.len()); uwu_progress_bar.update_progess(uwu_text.len());
@ -84,20 +89,24 @@ impl UwUify {
} }
} else { } else {
// Handle File I/O // Handle File I/O
if UwUOutFile::exists(&self.output) {
return Err(format!("File '{}' already exists, aborting operation", &self.output).into());
};
let mut uwu_in_file = match UwUInFile::new(&self.input) { let mut uwu_in_file = match UwUInFile::new(&self.input) {
Ok(uwu_in_file) => uwu_in_file, Ok(uwu_in_file) => uwu_in_file,
Err(err) => return Err(err), Err(err) => return Err(err.into()),
}; };
let mut uwu_out_file = match UwUOutFile::new(&self.output) { let mut uwu_out_file = match UwUOutFile::new(&self.output) {
Ok(uwu_out_file) => uwu_out_file, Ok(uwu_out_file) => uwu_out_file,
Err(err) => return Err(err) Err(err) => return Err(err.into())
}; };
let mut uwu_progress_bar = UwUProgressBar::new(uwu_in_file.get_file_bytes()); let mut uwu_progress_bar = UwUProgressBar::new(uwu_in_file.get_file_bytes());
loop { loop {
let bytes_read_in = match uwu_in_file.read_until_newline() { let bytes_read_in = match uwu_in_file.read_until_newline() {
Ok(bytes_read_in) => bytes_read_in, Ok(bytes_read_in) => bytes_read_in,
Err(err) => return Err(err), Err(err) => return Err(err.into()),
}; };
if bytes_read_in == 0 { break; } if bytes_read_in == 0 { break; }
@ -105,7 +114,7 @@ impl UwUify {
let uwu_sentence = self.uwuify_sentence(&utf8_str); let uwu_sentence = self.uwuify_sentence(&utf8_str);
match uwu_out_file.write_string_with_newline(&uwu_sentence) { match uwu_out_file.write_string_with_newline(&uwu_sentence) {
Ok(_) => (), Ok(_) => (),
Err(err) => return Err(err), Err(err) => return Err(err.into()),
}; };
uwu_progress_bar.update_progess(bytes_read_in); uwu_progress_bar.update_progess(bytes_read_in);

Loading…
Cancel
Save