From 6be7aea531893c3ba79e20af76def81c769466a7 Mon Sep 17 00:00:00 2001 From: Isaac Mills Date: Sat, 29 Jan 2022 19:10:49 -0500 Subject: [PATCH] Optimize compile times by converting to lib.rs standard --- Cargo.toml | 13 ++-- src/{uwuify => }/constants.rs | 0 src/{uwuify => }/io.rs | 16 +++-- src/{uwuify.rs => lib.rs} | 108 +++++++++++++++++++------------ src/main.rs | 36 ++++++++--- src/{uwuify => }/progress_bar.rs | 0 src/{uwuify => }/seeder.rs | 0 7 files changed, 113 insertions(+), 60 deletions(-) rename src/{uwuify => }/constants.rs (100%) rename src/{uwuify => }/io.rs (88%) rename src/{uwuify.rs => lib.rs} (72%) rename src/{uwuify => }/progress_bar.rs (100%) rename src/{uwuify => }/seeder.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index 38c7219..848e3b4 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -10,18 +10,21 @@ repository = "https://github.com/sgoudham/uwuifyy" keywords = ["cli", "uwu", "owo", "uwuify", "anime"] categories = ["command-line-utilities"] exclude = [ - "examples/the-complete-works-of-william-shakespeare.txt", - "examples/tiktok_app_reviews.csv", - "examples/tokyo-2020-olympics-tweets.csv", - "examples/uwu/**" + "examples/the-complete-works-of-william-shakespeare.txt", + "examples/tiktok_app_reviews.csv", + "examples/tokyo-2020-olympics-tweets.csv", + "examples/uwu/**", ] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html +[lib] +name = "uwuify" + [dependencies] clap = { version = "3.0.10", features = ["derive"] } rand = "0.8.4" rand_seeder = "0.2.2" rand_pcg = "0.3.1" indicatif = "0.16.2" -linkify = "0.8.0" \ No newline at end of file +linkify = "0.8.0" diff --git a/src/uwuify/constants.rs b/src/constants.rs similarity index 100% rename from src/uwuify/constants.rs rename to src/constants.rs diff --git a/src/uwuify/io.rs b/src/io.rs similarity index 88% rename from src/uwuify/io.rs rename to src/io.rs index 43a0823..f44d3b2 100644 --- a/src/uwuify/io.rs +++ b/src/io.rs @@ -1,6 +1,6 @@ use std::fs::File; use std::io::{BufRead, BufReader, BufWriter, Error, Write}; -use std::path::{Path}; +use std::path::Path; pub struct UwUInFile { file_bytes: u64, @@ -16,17 +16,21 @@ impl UwUInFile { pub fn new(path: &Path) -> Result { let file = match File::open(path) { Ok(file) => file, - Err(err) => return Err(err) + Err(err) => return Err(err), }; let file_metadata = match file.metadata() { Ok(file_metadata) => file_metadata, - Err(err) => return Err(err) + Err(err) => return Err(err), }; let file_bytes = file_metadata.len(); let reader = BufReader::new(file); let buffer = Vec::new(); - Ok(UwUInFile { file_bytes, reader, buffer }) + Ok(UwUInFile { + file_bytes, + reader, + buffer, + }) } pub fn read_until_newline(&mut self) -> Result { @@ -53,7 +57,7 @@ impl UwUOutFile { pub fn new(path: &str) -> Result { let file = match File::create(path) { Ok(file) => file, - Err(err) => return Err(err) + Err(err) => return Err(err), }; let writer = BufWriter::new(file); @@ -77,4 +81,4 @@ impl UwUOutFile { Err(err) => Err(err), } } -} \ No newline at end of file +} diff --git a/src/uwuify.rs b/src/lib.rs similarity index 72% rename from src/uwuify.rs rename to src/lib.rs index 173fc74..646240d 100644 --- a/src/uwuify.rs +++ b/src/lib.rs @@ -1,19 +1,19 @@ +use linkify::{LinkFinder, LinkKind}; use std::error::Error; use std::path::PathBuf; -use linkify::{LinkFinder, LinkKind}; -use crate::uwuify::constants::ACTIONS; -use crate::uwuify::constants::ACTIONS_SIZE; -use crate::uwuify::constants::FACES; -use crate::uwuify::constants::FACES_SIZE; -use crate::uwuify::io::{UwUInFile, UwUOutFile}; -use crate::uwuify::progress_bar::UwUProgressBar; -use crate::uwuify::seeder::UwUSeeder; +use constants::ACTIONS; +use constants::ACTIONS_SIZE; +use constants::FACES; +use constants::FACES_SIZE; +use io::{UwUInFile, UwUOutFile}; +use progress_bar::UwUProgressBar; +use seeder::UwUSeeder; mod constants; -mod seeder; -mod progress_bar; mod io; +mod progress_bar; +mod seeder; #[derive(Debug)] struct Modifiers { @@ -35,15 +35,18 @@ pub struct UwUify { } impl UwUify { - pub fn new(text: Option, - infile: Option, - outfile: Option, - supplied_at_runtime: bool, - words: f32, - faces: f32, - actions: f32, - stutters: f32, - random: bool) -> UwUify { // I dislike this + pub fn new( + text: Option, + infile: Option, + outfile: Option, + supplied_at_runtime: bool, + words: f32, + faces: f32, + actions: f32, + stutters: f32, + random: bool, + ) -> UwUify { + // I dislike this let mut linkify = LinkFinder::new(); linkify.kinds(&[LinkKind::Email, LinkKind::Url]); @@ -53,7 +56,13 @@ impl UwUify { text: text.unwrap_or_default(), input: infile.unwrap_or_default(), output: outfile.unwrap_or_default(), - modifiers: Modifiers { supplied_at_runtime, words, faces, actions, stutters }, + modifiers: Modifiers { + supplied_at_runtime, + words, + faces, + actions, + stutters, + }, random, linkify, } @@ -67,12 +76,16 @@ impl UwUify { // Handle Text Output if !self.output.is_empty() { if UwUOutFile::exists(&self.output) { - return Err(format!("File '{}' already exists, aborting operation", &self.output).into()); + return Err(format!( + "File '{}' already exists, aborting operation", + &self.output + ) + .into()); } let mut uwu_out_file = match UwUOutFile::new(&self.output) { Ok(uwu_out_file) => uwu_out_file, - Err(err) => return Err(err.into()) + Err(err) => return Err(err.into()), }; let mut uwu_progress_bar = UwUProgressBar::new(uwu_text.len() as u64); @@ -90,7 +103,9 @@ impl UwUify { } else { // Handle File I/O if UwUOutFile::exists(&self.output) { - return Err(format!("File '{}' already exists, aborting operation", &self.output).into()); + return Err( + format!("File '{}' already exists, aborting operation", &self.output).into(), + ); }; let mut uwu_in_file = match UwUInFile::new(&self.input) { @@ -99,7 +114,7 @@ impl UwUify { }; let mut uwu_out_file = match UwUOutFile::new(&self.output) { Ok(uwu_out_file) => uwu_out_file, - Err(err) => return Err(err.into()) + Err(err) => return Err(err.into()), }; let mut uwu_progress_bar = UwUProgressBar::new(uwu_in_file.get_file_bytes()); @@ -108,7 +123,9 @@ impl UwUify { Ok(bytes_read_in) => bytes_read_in, Err(err) => return Err(err.into()), }; - if bytes_read_in == 0 { break; } + if bytes_read_in == 0 { + break; + } let utf8_str = uwu_in_file.get_buffer_as_utf8_str(); let uwu_sentence = self.uwuify_sentence(&utf8_str); @@ -128,8 +145,7 @@ impl UwUify { } fn uwuify_sentence(&self, text: &str) -> String { - text - .split_whitespace() + text.split_whitespace() .map(|word| { let uwu_word = self.uwuify_word(word.to_string()); self.uwuify_spaces(uwu_word) @@ -144,15 +160,19 @@ impl UwUify { } let mut seeder = UwUSeeder::new(&word, self.random); - if seeder.random() > self.modifiers.words { return word; } + if seeder.random() > self.modifiers.words { + return word; + } let word_bytes = word.as_bytes(); let uwu_text_count = word.len(); let mut uwu_text = String::new(); for index in 0..uwu_text_count { - let previous_previous_char = *word_bytes.get(index - 2).unwrap_or_else(|| &word_bytes[0]) as char; - let previous_char = *word_bytes.get(index - 1).unwrap_or_else(|| &word_bytes[0]) as char; + let previous_previous_char = + *word_bytes.get(index - 2).unwrap_or_else(|| &word_bytes[0]) as char; + let previous_char = + *word_bytes.get(index - 1).unwrap_or_else(|| &word_bytes[0]) as char; let current_char = word_bytes[index] as char; match current_char { @@ -166,15 +186,15 @@ impl UwUify { uwu_text.pop(); uwu_text.push_str("uv"); } - _ => uwu_text.push(current_char) - } - _ => uwu_text.push(current_char) - } + _ => uwu_text.push(current_char), + }, + _ => uwu_text.push(current_char), + }, 'A' | 'I' | 'O' | 'U' | 'a' | 'i' | 'o' | 'u' => match previous_char { 'N' | 'n' => uwu_text.push_str(format!("y{}", current_char).as_str()), - _ => uwu_text.push(current_char) - } - _ => uwu_text.push(current_char) + _ => uwu_text.push(current_char), + }, + _ => uwu_text.push(current_char), } } @@ -192,12 +212,20 @@ impl UwUify { word = format!("{} {}", ACTIONS[seeder.random_int(0, ACTIONS_SIZE)], word); } else if random_value <= self.modifiers.stutters { let first_char_stutter = format!("{}-", word.chars().next().unwrap()); - word = format!("{}{}", first_char_stutter.repeat(seeder.random_int(1, 2)), word); + word = format!( + "{}{}", + first_char_stutter.repeat(seeder.random_int(1, 2)), + word + ); } } else { if random_value <= self.modifiers.stutters { let first_char_stutter = format!("{}-", word.chars().next().unwrap()); - word = format!("{}{}", first_char_stutter.repeat(seeder.random_int(1, 2)), word); + word = format!( + "{}{}", + first_char_stutter.repeat(seeder.random_int(1, 2)), + word + ); } if random_value <= self.modifiers.faces { word = format!("{} {}", FACES[seeder.random_int(0, FACES_SIZE)], word); @@ -209,4 +237,4 @@ impl UwUify { word } -} \ No newline at end of file +} diff --git a/src/main.rs b/src/main.rs index a4cad31..c416664 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,8 +1,6 @@ use clap::{ArgGroup, ErrorKind, IntoApp, Parser}; -use crate::uwuify::UwUify; - -mod uwuify; +use uwuify::UwUify; #[derive(Parser)] #[clap(author, version, about, long_about = None)] @@ -45,8 +43,22 @@ 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); + 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, + ); match uwuify.uwuify() { Ok(_) => (), Err(err) => { @@ -59,13 +71,19 @@ fn main() { fn is_between_zero_and_one(input: &str) -> Result<(), String> { let value = match input.parse::() { Ok(value) => value, - Err(_) => return Err(String::from("The value must be a decimal number")) + Err(_) => return Err(String::from("The value must be a decimal number")), }; - if (0.0..=1.0).contains(&value) { return Ok(()); } + if (0.0..=1.0).contains(&value) { + return Ok(()); + } Err(String::from("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 { +fn modifiers_supplied_at_runtime( + faces_occurrences: u64, + actions_occurrences: u64, + stutters_occurrences: u64, +) -> bool { faces_occurrences > 0 || actions_occurrences > 0 || stutters_occurrences > 0 -} \ No newline at end of file +} diff --git a/src/uwuify/progress_bar.rs b/src/progress_bar.rs similarity index 100% rename from src/uwuify/progress_bar.rs rename to src/progress_bar.rs diff --git a/src/uwuify/seeder.rs b/src/seeder.rs similarity index 100% rename from src/uwuify/seeder.rs rename to src/seeder.rs