decompression: panic if there are no arguments

This commit is contained in:
João M. Bezerra 2021-05-15 01:12:03 -03:00
parent 8548f73870
commit 864fc1a29b

View File

@ -5,9 +5,8 @@ use std::{
vec::Vec,
};
use strsim::normalized_damerau_levenshtein;
use oof::{arg_flag, flag};
use strsim::normalized_damerau_levenshtein;
#[derive(PartialEq, Eq, Debug)]
pub enum Command {
@ -36,11 +35,10 @@ pub struct ParsedArgs {
pub flags: oof::Flags,
}
/// check_for_typo checks if the first argument is
/// a typo for the compress subcommand.
/// check_for_typo checks if the first argument is
/// a typo for the compress subcommand.
/// Returns true if the arg is probably a typo or false otherwise.
fn is_typo<'a, P>(path: P) -> bool
fn is_typo<'a, P>(path: P) -> bool
where
P: AsRef<Path> + 'a,
{
@ -70,8 +68,6 @@ where
}
}
fn canonicalize_files<'a, P>(files: Vec<P>) -> crate::Result<Vec<PathBuf>>
where
P: AsRef<Path> + 'a,
@ -94,9 +90,7 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {
});
}
let subcommands = &[
"c", "compress"
];
let subcommands = &["c", "compress"];
let mut flags_info = vec![flag!('y', "yes"), flag!('n', "no")];
@ -124,13 +118,14 @@ pub fn parse_args_from(mut args: Vec<OsString>) -> crate::Result<ParsedArgs> {
// Defaults to decompression when there is no subcommand
None => {
flags_info.push(arg_flag!('o', "output"));
{
let first_arg = args.first().unwrap();
if let Some(first_arg) = args.first() {
if is_typo(first_arg) {
return Err(crate::Error::CompressionTypo);
}
} else {
todo!("Complain that no decompression arguments were given.");
}
// Parse flags
let (args, mut flags) = oof::filter_flags(args, &flags_info)?;