Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add file support #142

Merged
merged 6 commits into from
Dec 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions src/checkers/english.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,17 +146,17 @@ mod tests {
}
#[test]
fn test_check_normalise_string_works_with_lowercasing() {
let x = normalise_string(&"Hello Dear");
let x = normalise_string("Hello Dear");
assert_eq!(x, "hello dear")
}
#[test]
fn test_check_normalise_string_works_with_puncuation() {
let x = normalise_string(&"Hello, Dear");
let x = normalise_string("Hello, Dear");
assert_eq!(x, "hello dear")
}
#[test]
fn test_check_normalise_string_works_with_messy_puncuation() {
let x = normalise_string(&".He/ll?O, Dea!r");
let x = normalise_string(".He/ll?O, Dea!r");
assert_eq!(x, "hello dear")
}

Expand Down
47 changes: 41 additions & 6 deletions src/cli/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use crate::config::Config;
use std::{fs::File, io::Read};

use crate::{cli_pretty_printing::panic_failure_both_input_and_fail_provided, config::Config};
/// This doc string acts as a help message when the usees run '--help' in CLI mode
/// as do all doc strings on fields
use clap::Parser;
Expand All @@ -11,7 +13,7 @@ use log::trace;
pub struct Opts {
/// Some input. Because this isn't an Option<T> it's required to be used
#[arg(short, long)]
text: String,
text: Option<String>,

/// A level of verbosity, and can be used multiple times
#[arg(short, long, action = clap::ArgAction::Count)]
Expand All @@ -31,13 +33,19 @@ pub struct Opts {
/// Default is False
#[arg(short, long)]
api_mode: Option<bool>,
/// Opens a file for decoding
/// Use instead of `--text`
#[arg(short, long)]
file: Option<String>,
}

/// Parse CLI Arguments turns a Clap Opts struct, seen above
/// Into a library Struct for use within the program
/// The library struct can be found in the [config](../config) folder.
/// # Panics
/// This function can panic when it gets both a file and text input at the same time.
pub fn parse_cli_args() -> (String, Config) {
let opts: Opts = Opts::parse();
let mut opts: Opts = Opts::parse();
let min_log_level = match opts.verbose {
0 => "Warn",
1 => "Info",
Expand All @@ -48,16 +56,43 @@ pub fn parse_cli_args() -> (String, Config) {
env_logger::Env::default().filter_or(env_logger::DEFAULT_FILTER_ENV, min_log_level),
);

// If both the file and text are proivded, panic because we're not sure which one to use
if opts.file.is_some() && opts.text.is_some() {
panic_failure_both_input_and_fail_provided();
}

let input_text: String = if opts.file.is_some() {
// TODO pretty match on the errors to provide better output
// Else it'll panic
let mut file = File::open(opts.file.unwrap()).unwrap();
let mut contents = String::new();
file.read_to_string(&mut contents).unwrap();
// We can just put the file into the `Opts.text` and the program will work as normal
// On Unix systems a line is defined as "\n{text}\n"
// https://stackoverflow.com/a/729795
// Which means if a user creates a file on Unix, it'll have a new line appended.
// This is probably not what they wanted to decode (it is not what I wanted) so we are removing them
contents.strip_suffix(['\n', '\r']).unwrap().to_owned()
} else {
opts.text
.expect("Error. No input was provided. Please use ares --help")
};

// Fixes bug where opts.text and opts.file are partially borrowed
opts.text = None;
opts.file = None;

trace!("Program was called with CLI 😉");
trace!("Parsed the arguments");
trace!("The inputted text is {}", &input_text);

cli_args_into_config_struct(opts)
cli_args_into_config_struct(opts, input_text)
}

/// Turns our CLI arguments into a config stuct
fn cli_args_into_config_struct(opts: Opts) -> (String, Config) {
fn cli_args_into_config_struct(opts: Opts, text: String) -> (String, Config) {
(
opts.text,
text,
Config {
verbose: opts.verbose,
lemmeknow_config: Identifier::default(),
Expand Down
22 changes: 22 additions & 0 deletions src/cli_pretty_printing/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -132,3 +132,25 @@ pub fn return_early_because_input_text_is_plaintext() {
}
println!("Your input text is the plaintext 🥳");
}

/// The user has provided both textual input and file input
/// # Panics
/// This function panics and is only used in the CLI.
pub fn panic_failure_both_input_and_fail_provided() {
let config = crate::config::get_config();
if config.api_mode {
return;
}
panic!("Failed -- both file and text were provided. Please only use one.")
}

/// The user has not provided any input.
/// # Panics
/// This function panics and is only used in the CLI.
pub fn panic_failure_no_input_provided() {
let config = crate::config::get_config();
if config.api_mode {
return;
}
panic!("Failed -- no input was provided. Please use -t for text or -f for files.")
}
Loading