From 9e5e0716d11f4b8825d75f330db071597e1739dd Mon Sep 17 00:00:00 2001 From: Lux Aliaga Date: Thu, 9 Feb 2023 21:10:39 -0300 Subject: [PATCH] src: Error refactoring on vento CLI Since many of the errors were being reiterated in the files for the binaries I've decided to refactor them so we borrow which errors we want to show from an enum and then execute a function which essentially bails and matches each error. --- src/bin/drop.rs | 55 ++++++++++++++++++++++++++++-------------------- src/bin/take.rs | 17 +++++++++------ src/bin/vento.rs | 11 ++++++---- src/common.rs | 16 ++++++++++++++ src/error.rs | 41 ++++++++++++++++++++++++++++++++++++ src/lib.rs | 3 ++- 6 files changed, 108 insertions(+), 35 deletions(-) create mode 100644 src/error.rs diff --git a/src/bin/drop.rs b/src/bin/drop.rs index e7f7a55..af59138 100644 --- a/src/bin/drop.rs +++ b/src/bin/drop.rs @@ -17,11 +17,14 @@ * */ -use anyhow::{bail, Result}; -use colored::Colorize; +use anyhow::Result; use std::env; use std::path::Path; -use vento::{help, item}; +use vento::{ + common::get_current_dir, + error::{throw_error, ErrorType}, + help, item, +}; fn main() -> Result<()> { // Handles args in Drop @@ -30,34 +33,40 @@ fn main() -> Result<()> { if args[1].contains("--slot=") { // Checks if the user has provided the long argument "--slot=" match args.len() { - 4 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), Path::new(&args[4]).to_path_buf(), true)?, - 3 => item::drop(&args[2], &args[1].as_str().replace("--slot=", ""), match env::current_dir() { - Ok(dir) => dir, - Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red()) - }, true)?, - 2 => bail!("{}", "You need to specify a file".red()), - _ => bail!("{}", "Too many arguments".red()), + 4 => item::drop( + &args[2], + &args[1].as_str().replace("--slot=", ""), + Path::new(&args[4]).to_path_buf(), + true, + )?, + 3 => item::drop( + &args[2], + &args[1].as_str().replace("--slot=", ""), + get_current_dir()?, + true, + )?, + 2 => throw_error(ErrorType::SpecifyFile)?, + _ => throw_error(ErrorType::TooManyArgs)?, }; } else { match args[1].as_str() { "--help" | "-h" => help::drop()?, "-s" => match args.len() { 5 => item::drop(&args[3], &args[2], Path::new(&args[4]).to_path_buf(), true)?, - 4 => item::drop(&args[3], &args[2], match env::current_dir() { - Ok(dir) => dir, - Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red()) - }, true)?, - 3 => bail!("{}", "You need to specify a file".red()), - 2 => bail!("{}", "You need to specify a slot".red()), - _ => bail!("{}", "Too many arguments".red()), + 4 => item::drop(&args[3], &args[2], get_current_dir()?, true)?, + 3 => throw_error(ErrorType::SpecifyFile)?, + 2 => throw_error(ErrorType::SpecifySlot)?, + _ => throw_error(ErrorType::TooManyArgs)?, }, _ => match args.len() { - 3 => item::drop(&args[1], &String::from("active"), Path::new(&args[2]).to_path_buf(), true)?, - 2 => item::drop(&args[1], &String::from("active"), match env::current_dir() { - Ok(dir) => dir, - Err(_) => bail!("{}", "Vento was unable to detect your current directory. Have you configured your environment correctly?".red()) - }, true)?, - _ => bail!("{}", "Too many arguments".red()), + 3 => item::drop( + &args[1], + &String::from("active"), + Path::new(&args[2]).to_path_buf(), + true, + )?, + 2 => item::drop(&args[1], &String::from("active"), get_current_dir()?, true)?, + _ => throw_error(ErrorType::TooManyArgs)?, }, } } diff --git a/src/bin/take.rs b/src/bin/take.rs index dd91a62..125c087 100644 --- a/src/bin/take.rs +++ b/src/bin/take.rs @@ -20,7 +20,10 @@ use anyhow::{bail, Result}; use colored::Colorize; use std::env; -use vento::{help, item}; +use vento::{ + error::{throw_error, ErrorType}, + help, item, +}; fn main() -> Result<()> { // Handles args in Vento @@ -30,21 +33,21 @@ fn main() -> Result<()> { // Checks if the user has provided the long argument "--slot=" match args.len() { 3 => item::take(&args[2], &args[1].replace("--slot=", ""), true)?, - 2 => bail!("{}", "You need to specify a file".red()), - _ => bail!("{}", "Too many arguments".red()), + 2 => throw_error(ErrorType::SpecifyFile)?, + _ => throw_error(ErrorType::TooManyArgs)?, }; } else { match args[1].as_str() { "--help" | "-h" => help::take()?, "-s" => match args.len() { 4 => item::take(&args[3], &args[2], true)?, - 3 => bail!("{}", "You need to specify a file".red()), - 2 => bail!("{}", "You need to specify a slot".red()), - _ => bail!("{}", "Too many arguments".red()), + 3 => throw_error(ErrorType::SpecifyFile)?, + 2 => throw_error(ErrorType::SpecifySlot)?, + _ => throw_error(ErrorType::TooManyArgs)?, }, _ => match args.len() { 2 => item::take(&args[1], &String::from("active"), true)?, - _ => bail!("{}", "Too many arguments".red()), + _ => throw_error(ErrorType::TooManyArgs)?, }, } } diff --git a/src/bin/vento.rs b/src/bin/vento.rs index 52de841..0bb0f45 100644 --- a/src/bin/vento.rs +++ b/src/bin/vento.rs @@ -20,7 +20,10 @@ use anyhow::{bail, Result}; use colored::Colorize; use std::env; -use vento::{help, history, inv}; +use vento::{ + error::{throw_error, ErrorType}, + help, history, inv, +}; fn main() -> Result<()> { // Handles args in Vento @@ -32,7 +35,7 @@ fn main() -> Result<()> { match args.len() { 3 => inv::list(&args[1].replace("--slot=", ""), &args[2])?, 2 => inv::list(&args[1].replace("--slot=", ""), "")?, - _ => bail!("{}", "Too many arguments".red()), + _ => throw_error(ErrorType::TooManyArgs)?, }; } else { match args[1].as_str() { @@ -43,8 +46,8 @@ fn main() -> Result<()> { "-s" => match args.len() { 4 => inv::list(&args[2], &args[3])?, 3 => inv::list(&args[2], "")?, - 2 => bail!("{}", "You need to specify a slot".red()), - _ => bail!("{}", "Too many arguments".red()), + 2 => throw_error(ErrorType::SpecifySlot)?, + _ => throw_error(ErrorType::TooManyArgs)?, }, _ => inv::list("active", args[1].as_str())?, } diff --git a/src/common.rs b/src/common.rs index 668826c..749266a 100644 --- a/src/common.rs +++ b/src/common.rs @@ -17,9 +17,11 @@ * */ +use crate::error::{throw_error, ErrorType}; use anyhow::{bail, Result}; use colored::Colorize; use config::Config; +use std::env::current_dir; use std::fs::File; use std::io::Write; use std::path::{Path, PathBuf}; @@ -125,3 +127,17 @@ pub fn history(data: HistoryData) -> Result<()> { Ok(()) } + +/// Gets current directory for commands +pub fn get_current_dir() -> Result { + let currentdir = match current_dir() { + Ok(dir) => dir, + Err(_) => PathBuf::new(), + }; + + if currentdir == PathBuf::new() { + throw_error(ErrorType::NoCurrentDirectory)?; + } + + Ok(currentdir) +} diff --git a/src/error.rs b/src/error.rs new file mode 100644 index 0000000..3352db2 --- /dev/null +++ b/src/error.rs @@ -0,0 +1,41 @@ +/* + * Vento, a CLI inventory for your files. + * Copyright (C) 2023 Lux Aliaga + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + * + */ + +use anyhow::{bail, Result}; +use colored::Colorize; + +pub enum ErrorType { + TooManyArgs, + SpecifySlot, + SpecifyFile, + NoCurrentDirectory, +} + +pub fn throw_error(error: ErrorType) -> Result<()> { + bail!( + "{}", + match error { + ErrorType::TooManyArgs => "Too many arguments", + ErrorType::SpecifySlot => "You need to specify a file", + ErrorType::SpecifyFile => "You need to specify a slot", + ErrorType::NoCurrentDirectory => "Vento was unable to detect your current directory. Have you configured your environment correctly?", + } + .red() + ); +} diff --git a/src/lib.rs b/src/lib.rs index 9090ff5..b25da4b 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,7 +17,8 @@ * */ -mod common; +pub mod common; +pub mod error; pub mod help; pub mod history; pub mod inv;