Optimize compile times by converting to lib.rs standard

pull/2/head
Isaac Mills 3 years ago
parent 47ef218783
commit 6be7aea531
No known key found for this signature in database
GPG Key ID: B67D7410F33A0F61

@ -10,18 +10,21 @@ repository = "https://github.com/sgoudham/uwuifyy"
keywords = ["cli", "uwu", "owo", "uwuify", "anime"] keywords = ["cli", "uwu", "owo", "uwuify", "anime"]
categories = ["command-line-utilities"] categories = ["command-line-utilities"]
exclude = [ exclude = [
"examples/the-complete-works-of-william-shakespeare.txt", "examples/the-complete-works-of-william-shakespeare.txt",
"examples/tiktok_app_reviews.csv", "examples/tiktok_app_reviews.csv",
"examples/tokyo-2020-olympics-tweets.csv", "examples/tokyo-2020-olympics-tweets.csv",
"examples/uwu/**" "examples/uwu/**",
] ]
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "uwuify"
[dependencies] [dependencies]
clap = { version = "3.0.10", features = ["derive"] } clap = { version = "3.0.10", features = ["derive"] }
rand = "0.8.4" rand = "0.8.4"
rand_seeder = "0.2.2" rand_seeder = "0.2.2"
rand_pcg = "0.3.1" rand_pcg = "0.3.1"
indicatif = "0.16.2" indicatif = "0.16.2"
linkify = "0.8.0" linkify = "0.8.0"

@ -1,6 +1,6 @@
use std::fs::File; use std::fs::File;
use std::io::{BufRead, BufReader, BufWriter, Error, Write}; use std::io::{BufRead, BufReader, BufWriter, Error, Write};
use std::path::{Path}; use std::path::Path;
pub struct UwUInFile { pub struct UwUInFile {
file_bytes: u64, file_bytes: u64,
@ -16,17 +16,21 @@ impl UwUInFile {
pub fn new(path: &Path) -> Result<UwUInFile, Error> { pub fn new(path: &Path) -> Result<UwUInFile, Error> {
let file = match File::open(path) { let file = match File::open(path) {
Ok(file) => file, Ok(file) => file,
Err(err) => return Err(err) Err(err) => return Err(err),
}; };
let file_metadata = match file.metadata() { let file_metadata = match file.metadata() {
Ok(file_metadata) => file_metadata, Ok(file_metadata) => file_metadata,
Err(err) => return Err(err) Err(err) => return Err(err),
}; };
let file_bytes = file_metadata.len(); let file_bytes = file_metadata.len();
let reader = BufReader::new(file); let reader = BufReader::new(file);
let buffer = Vec::new(); let buffer = Vec::new();
Ok(UwUInFile { file_bytes, reader, buffer }) Ok(UwUInFile {
file_bytes,
reader,
buffer,
})
} }
pub fn read_until_newline(&mut self) -> Result<usize, Error> { pub fn read_until_newline(&mut self) -> Result<usize, Error> {
@ -53,7 +57,7 @@ impl UwUOutFile {
pub fn new(path: &str) -> Result<UwUOutFile, Error> { pub fn new(path: &str) -> Result<UwUOutFile, Error> {
let file = match File::create(path) { let file = match File::create(path) {
Ok(file) => file, Ok(file) => file,
Err(err) => return Err(err) Err(err) => return Err(err),
}; };
let writer = BufWriter::new(file); let writer = BufWriter::new(file);
@ -77,4 +81,4 @@ impl UwUOutFile {
Err(err) => Err(err), Err(err) => Err(err),
} }
} }
} }

@ -1,19 +1,19 @@
use linkify::{LinkFinder, LinkKind};
use std::error::Error; use std::error::Error;
use std::path::PathBuf; use std::path::PathBuf;
use linkify::{LinkFinder, LinkKind};
use crate::uwuify::constants::ACTIONS; use constants::ACTIONS;
use crate::uwuify::constants::ACTIONS_SIZE; use constants::ACTIONS_SIZE;
use crate::uwuify::constants::FACES; use constants::FACES;
use crate::uwuify::constants::FACES_SIZE; use constants::FACES_SIZE;
use crate::uwuify::io::{UwUInFile, UwUOutFile}; use io::{UwUInFile, UwUOutFile};
use crate::uwuify::progress_bar::UwUProgressBar; use progress_bar::UwUProgressBar;
use crate::uwuify::seeder::UwUSeeder; use seeder::UwUSeeder;
mod constants; mod constants;
mod seeder;
mod progress_bar;
mod io; mod io;
mod progress_bar;
mod seeder;
#[derive(Debug)] #[derive(Debug)]
struct Modifiers { struct Modifiers {
@ -35,15 +35,18 @@ pub struct UwUify {
} }
impl UwUify { impl UwUify {
pub fn new(text: Option<String>, pub fn new(
infile: Option<PathBuf>, text: Option<String>,
outfile: Option<String>, infile: Option<PathBuf>,
supplied_at_runtime: bool, outfile: Option<String>,
words: f32, supplied_at_runtime: bool,
faces: f32, words: f32,
actions: f32, faces: f32,
stutters: f32, actions: f32,
random: bool) -> UwUify { // I dislike this stutters: f32,
random: bool,
) -> UwUify {
// I dislike this
let mut linkify = LinkFinder::new(); let mut linkify = LinkFinder::new();
linkify.kinds(&[LinkKind::Email, LinkKind::Url]); linkify.kinds(&[LinkKind::Email, LinkKind::Url]);
@ -53,7 +56,13 @@ impl UwUify {
text: text.unwrap_or_default(), text: text.unwrap_or_default(),
input: infile.unwrap_or_default(), input: infile.unwrap_or_default(),
output: outfile.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, random,
linkify, linkify,
} }
@ -67,12 +76,16 @@ impl UwUify {
// Handle Text Output // Handle Text Output
if !self.output.is_empty() { if !self.output.is_empty() {
if UwUOutFile::exists(&self.output) { 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) { 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.into()) 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);
@ -90,7 +103,9 @@ impl UwUify {
} else { } else {
// Handle File I/O // Handle File I/O
if UwUOutFile::exists(&self.output) { 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) { 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) { 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.into()) 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());
@ -108,7 +123,9 @@ impl UwUify {
Ok(bytes_read_in) => bytes_read_in, Ok(bytes_read_in) => bytes_read_in,
Err(err) => return Err(err.into()), 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 utf8_str = uwu_in_file.get_buffer_as_utf8_str();
let uwu_sentence = self.uwuify_sentence(&utf8_str); let uwu_sentence = self.uwuify_sentence(&utf8_str);
@ -128,8 +145,7 @@ impl UwUify {
} }
fn uwuify_sentence(&self, text: &str) -> String { fn uwuify_sentence(&self, text: &str) -> String {
text text.split_whitespace()
.split_whitespace()
.map(|word| { .map(|word| {
let uwu_word = self.uwuify_word(word.to_string()); let uwu_word = self.uwuify_word(word.to_string());
self.uwuify_spaces(uwu_word) self.uwuify_spaces(uwu_word)
@ -144,15 +160,19 @@ impl UwUify {
} }
let mut seeder = UwUSeeder::new(&word, self.random); 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 word_bytes = word.as_bytes();
let uwu_text_count = word.len(); let uwu_text_count = word.len();
let mut uwu_text = String::new(); let mut uwu_text = String::new();
for index in 0..uwu_text_count { 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_previous_char =
let previous_char = *word_bytes.get(index - 1).unwrap_or_else(|| &word_bytes[0]) as 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; let current_char = word_bytes[index] as char;
match current_char { match current_char {
@ -166,15 +186,15 @@ impl UwUify {
uwu_text.pop(); uwu_text.pop();
uwu_text.push_str("uv"); 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 { 'A' | 'I' | 'O' | 'U' | 'a' | 'i' | 'o' | 'u' => match previous_char {
'N' | 'n' => uwu_text.push_str(format!("y{}", current_char).as_str()), '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); word = format!("{} {}", ACTIONS[seeder.random_int(0, ACTIONS_SIZE)], word);
} else if random_value <= self.modifiers.stutters { } else if random_value <= self.modifiers.stutters {
let first_char_stutter = format!("{}-", word.chars().next().unwrap()); 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 { } else {
if random_value <= self.modifiers.stutters { if random_value <= self.modifiers.stutters {
let first_char_stutter = format!("{}-", word.chars().next().unwrap()); 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 { if random_value <= self.modifiers.faces {
word = format!("{} {}", FACES[seeder.random_int(0, FACES_SIZE)], word); word = format!("{} {}", FACES[seeder.random_int(0, FACES_SIZE)], word);
@ -209,4 +237,4 @@ impl UwUify {
word word
} }
} }

@ -1,8 +1,6 @@
use clap::{ArgGroup, ErrorKind, IntoApp, Parser}; use clap::{ArgGroup, ErrorKind, IntoApp, Parser};
use crate::uwuify::UwUify; use uwuify::UwUify;
mod uwuify;
#[derive(Parser)] #[derive(Parser)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
@ -45,8 +43,22 @@ fn main() {
let args = Args::parse(); let args = Args::parse();
let matches = Args::into_app().get_matches(); 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 supplied_at_runtime = modifiers_supplied_at_runtime(
let uwuify = UwUify::new(args.text, args.infile, args.outfile, supplied_at_runtime, args.words, args.faces, args.actions, args.stutters, args.random); 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() { match uwuify.uwuify() {
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {
@ -59,13 +71,19 @@ fn main() {
fn is_between_zero_and_one(input: &str) -> Result<(), String> { fn is_between_zero_and_one(input: &str) -> Result<(), String> {
let value = match input.parse::<f32>() { let value = match input.parse::<f32>() {
Ok(value) => value, 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")) 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 faces_occurrences > 0 || actions_occurrences > 0 || stutters_occurrences > 0
} }

Loading…
Cancel
Save