diff --git a/Cargo.toml b/Cargo.toml index cf6a606..8b15df1 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -22,7 +22,7 @@ exclude = [ name = "uwuify" [dependencies] -clap = { version = "3.0.10", features = ["derive"] } +clap = { version = "3.0.13", features = ["derive"] } rand = "0.8.4" indicatif = "0.16.2" linkify = "0.8.0" diff --git a/src/lib.rs b/src/lib.rs index 39cbb5b..47ed5c1 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,10 +1,9 @@ #![cfg_attr(test, feature(test))] use linkify::{LinkFinder, LinkKind}; -use std::error::Error; use std::fs::File; -use std::io::Write; -use std::path::PathBuf; +use std::io::{Error, Write}; +use std::path::Path; use constants::ACTIONS; use constants::ACTIONS_SIZE; @@ -29,27 +28,27 @@ struct Modifiers { } #[derive(Debug)] -pub struct UwUify { - text: String, - input: PathBuf, - output: String, +pub struct UwUify<'a> { + text: &'a str, + input: &'a Path, + output: &'a str, modifiers: Modifiers, random: bool, linkify: LinkFinder, } -impl UwUify { +impl<'a> UwUify<'a> { pub fn new( - text: Option, - infile: Option, - outfile: Option, + text: Option<&'a str>, + infile: Option<&'a Path>, + outfile: Option<&'a str>, supplied_at_runtime: bool, words: f64, faces: f64, actions: f64, stutters: f64, random: bool, - ) -> UwUify { + ) -> UwUify<'a> { // I dislike this let mut linkify = LinkFinder::new(); @@ -58,7 +57,7 @@ impl UwUify { UwUify { text: text.unwrap_or_default(), - input: infile.unwrap_or_default(), + input: infile.unwrap_or(Path::new("")), output: outfile.unwrap_or_default(), modifiers: Modifiers { supplied_at_runtime, @@ -72,17 +71,16 @@ impl UwUify { } } - pub fn uwuify(&self) -> Result<(), Box> { + pub fn uwuify(&self) -> Result<(), Error> { // Handle Text if !self.text.is_empty() { // Handle Text Output if !self.output.is_empty() { - if std::path::Path::new(&self.output).exists() { - return Err(format!( - "File '{}' already exists, aborting operation", - &self.output - ) - .into()); + if Path::new(&self.output).exists() { + return Err(Error::new( + std::io::ErrorKind::AlreadyExists, + format!("File '{}' already exists, aborting operation", &self.output), + )); } let mut uwu_out_file = UwUOutFile::new(File::create(&self.output)?); @@ -107,10 +105,11 @@ impl UwUify { } } else { // Handle File I/O - if std::path::Path::new(&self.output).exists() { - return Err( - format!("File '{}' already exists, aborting operation", &self.output).into(), - ); + if Path::new(&self.output).exists() { + return Err(Error::new( + std::io::ErrorKind::AlreadyExists, + format!("File '{}' already exists, aborting operation", &self.output), + )); } let mut uwu_in_file = UwUInFile::new(&self.input)?; @@ -144,11 +143,7 @@ impl UwUify { }) } - fn uwuify_word( - &self, - word: &str, - out: &mut UwUOutFile, - ) -> Result<(), std::io::Error> { + fn uwuify_word(&self, word: &str, out: &mut UwUOutFile) -> Result<(), Error> { let mut seeder = UwUSeeder::new(word, self.random); let random_value = seeder.random(); @@ -223,7 +218,7 @@ mod tests { #[bench] fn uwu_bench(b: &mut test::Bencher) { let uwuify = super::UwUify::new( - Some(include_str!("test.txt").to_string()), + Some(include_str!("test.txt")), None, None, false, diff --git a/src/main.rs b/src/main.rs index 3b49d8b..60bb41d 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,6 +2,12 @@ use clap::{ArgGroup, ErrorKind, IntoApp, Parser}; use uwuify::UwUify; +macro_rules! modifiers_supplied_at_runtime { + ($faces_occurrences:expr,$actions_occurrences:expr,$stutters_occurrences:expr) => { + $faces_occurrences > 0 || $actions_occurrences > 0 || $stutters_occurrences > 0 + }; +} + #[derive(Parser)] #[clap(author, version, about, long_about = None)] #[clap(group(ArgGroup::new("uwu").required(true).args(& ["text", "infile"]),))] @@ -40,30 +46,28 @@ struct Args { } fn main() { - let args = Args::parse(); let matches = Args::into_app().get_matches(); - let supplied_at_runtime = modifiers_supplied_at_runtime( - matches.occurrences_of("faces"), - matches.occurrences_of("actions"), - matches.occurrences_of("stutters"), - ); let uwuify = UwUify::new( - args.text, - args.infile, - args.outfile, - supplied_at_runtime, - args.words, - args.faces, - args.actions, - args.stutters, - args.random, + matches.value_of("text"), + matches.value_of("infile").map(|f| std::path::Path::new(f)), + matches.value_of("outfile"), + modifiers_supplied_at_runtime!( + matches.occurrences_of("faces"), + matches.occurrences_of("actions"), + matches.occurrences_of("stutters") + ), + matches.value_of_t("words").unwrap_or_else(|e| e.exit()), + matches.value_of_t("faces").unwrap_or_else(|e| e.exit()), + matches.value_of_t("actions").unwrap_or_else(|e| e.exit()), + matches.value_of_t("stutters").unwrap_or_else(|e| e.exit()), + matches.value_of_t("random").unwrap_or_else(|e| e.exit()), ); match uwuify.uwuify() { Ok(_) => (), Err(err) => { let mut app = Args::into_app(); - app.error(ErrorKind::DisplayHelp, err.to_string()).exit(); + app.error(ErrorKind::DisplayHelp, err).exit(); } } } @@ -79,11 +83,3 @@ fn is_between_zero_and_one(input: &str) -> Result<(), &'static str> { } Err("The value must be between 0.0 and 1.0") } - -fn modifiers_supplied_at_runtime( - faces_occurrences: u64, - actions_occurrences: u64, - stutters_occurrences: u64, -) -> bool { - faces_occurrences > 0 || actions_occurrences > 0 || stutters_occurrences > 0 -}