SUPWW SPWEED >w< *shits aggessively*

pull/2/head
Isaac Mills 3 years ago
parent 2ad28b49ac
commit 71f408ef58
No known key found for this signature in database
GPG Key ID: B67D7410F33A0F61

@ -22,7 +22,7 @@ exclude = [
name = "uwuify" name = "uwuify"
[dependencies] [dependencies]
clap = { version = "3.0.10", features = ["derive"] } clap = { version = "3.0.13", features = ["derive"] }
rand = "0.8.4" rand = "0.8.4"
indicatif = "0.16.2" indicatif = "0.16.2"
linkify = "0.8.0" linkify = "0.8.0"

@ -1,10 +1,9 @@
#![cfg_attr(test, feature(test))] #![cfg_attr(test, feature(test))]
use linkify::{LinkFinder, LinkKind}; use linkify::{LinkFinder, LinkKind};
use std::error::Error;
use std::fs::File; use std::fs::File;
use std::io::Write; use std::io::{Error, Write};
use std::path::PathBuf; use std::path::Path;
use constants::ACTIONS; use constants::ACTIONS;
use constants::ACTIONS_SIZE; use constants::ACTIONS_SIZE;
@ -29,27 +28,27 @@ struct Modifiers {
} }
#[derive(Debug)] #[derive(Debug)]
pub struct UwUify { pub struct UwUify<'a> {
text: String, text: &'a str,
input: PathBuf, input: &'a Path,
output: String, output: &'a str,
modifiers: Modifiers, modifiers: Modifiers,
random: bool, random: bool,
linkify: LinkFinder, linkify: LinkFinder,
} }
impl UwUify { impl<'a> UwUify<'a> {
pub fn new( pub fn new(
text: Option<String>, text: Option<&'a str>,
infile: Option<PathBuf>, infile: Option<&'a Path>,
outfile: Option<String>, outfile: Option<&'a str>,
supplied_at_runtime: bool, supplied_at_runtime: bool,
words: f64, words: f64,
faces: f64, faces: f64,
actions: f64, actions: f64,
stutters: f64, stutters: f64,
random: bool, random: bool,
) -> UwUify { ) -> UwUify<'a> {
// I dislike this // I dislike this
let mut linkify = LinkFinder::new(); let mut linkify = LinkFinder::new();
@ -58,7 +57,7 @@ impl UwUify {
UwUify { UwUify {
text: text.unwrap_or_default(), text: text.unwrap_or_default(),
input: infile.unwrap_or_default(), input: infile.unwrap_or(Path::new("")),
output: outfile.unwrap_or_default(), output: outfile.unwrap_or_default(),
modifiers: Modifiers { modifiers: Modifiers {
supplied_at_runtime, supplied_at_runtime,
@ -72,17 +71,16 @@ impl UwUify {
} }
} }
pub fn uwuify(&self) -> Result<(), Box<dyn Error>> { pub fn uwuify(&self) -> Result<(), Error> {
// Handle Text // Handle Text
if !self.text.is_empty() { if !self.text.is_empty() {
// Handle Text Output // Handle Text Output
if !self.output.is_empty() { if !self.output.is_empty() {
if std::path::Path::new(&self.output).exists() { if Path::new(&self.output).exists() {
return Err(format!( return Err(Error::new(
"File '{}' already exists, aborting operation", std::io::ErrorKind::AlreadyExists,
&self.output format!("File '{}' already exists, aborting operation", &self.output),
) ));
.into());
} }
let mut uwu_out_file = UwUOutFile::new(File::create(&self.output)?); let mut uwu_out_file = UwUOutFile::new(File::create(&self.output)?);
@ -107,10 +105,11 @@ impl UwUify {
} }
} else { } else {
// Handle File I/O // Handle File I/O
if std::path::Path::new(&self.output).exists() { if Path::new(&self.output).exists() {
return Err( return Err(Error::new(
format!("File '{}' already exists, aborting operation", &self.output).into(), std::io::ErrorKind::AlreadyExists,
); format!("File '{}' already exists, aborting operation", &self.output),
));
} }
let mut uwu_in_file = UwUInFile::new(&self.input)?; let mut uwu_in_file = UwUInFile::new(&self.input)?;
@ -144,11 +143,7 @@ impl UwUify {
}) })
} }
fn uwuify_word<T: Write>( fn uwuify_word<T: Write>(&self, word: &str, out: &mut UwUOutFile<T>) -> Result<(), Error> {
&self,
word: &str,
out: &mut UwUOutFile<T>,
) -> Result<(), std::io::Error> {
let mut seeder = UwUSeeder::new(word, self.random); let mut seeder = UwUSeeder::new(word, self.random);
let random_value = seeder.random(); let random_value = seeder.random();
@ -223,7 +218,7 @@ mod tests {
#[bench] #[bench]
fn uwu_bench(b: &mut test::Bencher) { fn uwu_bench(b: &mut test::Bencher) {
let uwuify = super::UwUify::new( let uwuify = super::UwUify::new(
Some(include_str!("test.txt").to_string()), Some(include_str!("test.txt")),
None, None,
None, None,
false, false,

@ -2,6 +2,12 @@ use clap::{ArgGroup, ErrorKind, IntoApp, Parser};
use uwuify::UwUify; 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)] #[derive(Parser)]
#[clap(author, version, about, long_about = None)] #[clap(author, version, about, long_about = None)]
#[clap(group(ArgGroup::new("uwu").required(true).args(& ["text", "infile"]),))] #[clap(group(ArgGroup::new("uwu").required(true).args(& ["text", "infile"]),))]
@ -40,30 +46,28 @@ struct Args {
} }
fn main() { fn main() {
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 uwuify = UwUify::new( let uwuify = UwUify::new(
args.text, matches.value_of("text"),
args.infile, matches.value_of("infile").map(|f| std::path::Path::new(f)),
args.outfile, matches.value_of("outfile"),
supplied_at_runtime, modifiers_supplied_at_runtime!(
args.words, matches.occurrences_of("faces"),
args.faces, matches.occurrences_of("actions"),
args.actions, matches.occurrences_of("stutters")
args.stutters, ),
args.random, 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() { match uwuify.uwuify() {
Ok(_) => (), Ok(_) => (),
Err(err) => { Err(err) => {
let mut app = Args::into_app(); 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") 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
}

Loading…
Cancel
Save