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.
This commit is contained in:
Lux Aliaga 2023-02-09 21:10:39 -03:00
parent 986c0f7f20
commit 9e5e0716d1
Signed by: lux
GPG Key ID: B56C805968637437
6 changed files with 108 additions and 35 deletions

View File

@ -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)?,
},
}
}

View File

@ -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)?,
},
}
}

View File

@ -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())?,
}

View File

@ -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<PathBuf> {
let currentdir = match current_dir() {
Ok(dir) => dir,
Err(_) => PathBuf::new(),
};
if currentdir == PathBuf::new() {
throw_error(ErrorType::NoCurrentDirectory)?;
}
Ok(currentdir)
}

41
src/error.rs Normal file
View File

@ -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 <https://www.gnu.org/licenses/>.
*
*/
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()
);
}

View File

@ -17,7 +17,8 @@
*
*/
mod common;
pub mod common;
pub mod error;
pub mod help;
pub mod history;
pub mod inv;