diff --git a/src/cli.rs b/src/cli.rs index d163163..302d371 100644 --- a/src/cli.rs +++ b/src/cli.rs @@ -7,7 +7,7 @@ use std::{ use strsim::normalized_damerau_levenshtein; -use crate::{arg_flag, flag, oof}; +use crate::{arg_flag, flag, oof, Error}; #[derive(PartialEq, Eq, Debug)] pub enum Command { @@ -27,8 +27,8 @@ pub enum Command { /// Calls parse_args_and_flags_from using argv (std::env::args_os) /// -/// This function is also responsible for treating and checking the cli input -/// Like calling canonicale, checking if it exists. +/// This function is also responsible for treating and checking the command-line input, +/// such as calling [`canonicalize`](std::fs::canonicalize), checking if it the given files exists, etc. pub fn parse_args() -> crate::Result { // From argv, but ignoring empty arguments let args = env::args_os().skip(1).filter(|arg| !arg.is_empty()).collect(); @@ -55,8 +55,7 @@ pub struct ParsedArgs { pub flags: oof::Flags, } -/// check_for_typo checks if the first argument is -/// a typo for the compress subcommand. +/// 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(path: impl AsRef) -> bool { if path.as_ref().exists() { @@ -74,7 +73,7 @@ fn canonicalize(path: impl AsRef) -> crate::Result { Ok(abs_path) => Ok(abs_path), Err(io_err) => { if !path.as_ref().exists() { - Err(crate::Error::FileNotFound(PathBuf::from(path.as_ref()))) + Err(Error::FileNotFound(path.as_ref().into())) } else { Err(io_err.into()) } @@ -105,7 +104,7 @@ pub fn parse_args_from(mut args: Vec) -> crate::Result { let mut files: Vec = args.into_iter().map(PathBuf::from).collect(); if files.len() < 2 { - return Err(crate::Error::MissingArgumentsForCompression); + return Err(Error::MissingArgumentsForCompression); } // Safety: we checked that args.len() >= 2 @@ -120,7 +119,7 @@ pub fn parse_args_from(mut args: Vec) -> crate::Result { if let Some(first_arg) = args.first() { if is_typo(first_arg) { - return Err(crate::Error::CompressionTypo); + return Err(Error::CompressionTypo); } } else { todo!("Complain that no decompression arguments were given."); @@ -171,7 +170,7 @@ mod tests { test_cli("compress foo bar baz.zip").unwrap().command, Command::Compress { files: vec!["foo".into(), "bar".into()], output_path: "baz.zip".into() } ); - assert_eq!(test_cli("compress").unwrap_err(), crate::Error::MissingArgumentsForCompression); + assert_eq!(test_cli("compress").unwrap_err(), Error::MissingArgumentsForCompression); } #[test] diff --git a/src/dialogs.rs b/src/dialogs.rs index 5120713..d434907 100644 --- a/src/dialogs.rs +++ b/src/dialogs.rs @@ -2,13 +2,12 @@ use std::io::{self, Write}; use crate::utils::colors; +/// Represents a confirmation dialog pub struct Confirmation<'a> { pub prompt: &'a str, pub placeholder: Option<&'a str>, } -#[derive(Debug)] -pub struct Error; impl<'a> Confirmation<'a> { pub const fn new(prompt: &'a str, pattern: Option<&'a str>) -> Self { diff --git a/src/utils.rs b/src/utils.rs index 1899331..f0485e5 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -19,11 +19,7 @@ pub fn create_dir_if_non_existent(path: &Path) -> crate::Result<()> { pub fn cd_into_same_dir_as(filename: &Path) -> crate::Result { let previous_location = env::current_dir()?; - let parent = if let Some(parent) = filename.parent() { - parent - } else { - return Err(crate::Error::CompressingRootFolder); - }; + let parent = filename.parent().ok_or(crate::Error::CompressingRootFolder)?; // TODO: fix this error variant, as it is not the only possible error that can // come out of this operation