diff --git a/build.rs b/build.rs index 80ecac3..85638f5 100644 --- a/build.rs +++ b/build.rs @@ -23,6 +23,11 @@ use std::env; use std::fs::{create_dir_all, File}; use std::io::Write; +struct Page { + content: String, + file: String, +} + fn main() -> Result<()> { if cfg!(unix) { let pages = [vento()?, take()?, drop()?, ventotoml()?]; @@ -32,16 +37,16 @@ fn main() -> Result<()> { create_dir_all(tempdir.clone())?; for page in &pages { - let tempfile = tempdir.join(page.clone().1); + let tempfile = tempdir.join(&page.file); let mut file = File::create(tempfile).unwrap(); - write!(&mut file, "{}", page.clone().0).unwrap(); + write!(&mut file, "{}", &page.content).unwrap(); } } Ok(()) } -fn vento() -> Result<(String, String)> { - let page = Manual::new("vento") +fn vento() -> Result { + let content = Manual::new("vento") .about("a CLI inventory for your files") .author(Author::new("Lux Aliaga").email("they@mint.lgbt")) .description("List files and directories in the currently active inventory, the files in SLOT, the files in DIRECTORY or the files in DIRECTORY in SLOT.") @@ -76,11 +81,14 @@ fn vento() -> Result<(String, String)> { ) .render(); - Ok((page, String::from("vento.1"))) + Ok(Page { + content, + file: String::from("vento.1"), + }) } -fn take() -> Result<(String, String)> { - let page = Manual::new("take") +fn take() -> Result { + let content = Manual::new("take") .about("a file grabber for Vento") .author(Author::new("Lux Aliaga").email("they@mint.lgbt")) .description("Take FILE and put it in the inventory.") @@ -93,11 +101,14 @@ fn take() -> Result<(String, String)> { .arg(Arg::new("FILE")) .render(); - Ok((page, String::from("take.1"))) + Ok(Page { + content, + file: String::from("take.1"), + }) } -fn drop() -> Result<(String, String)> { - let page = Manual::new("drop") +fn drop() -> Result { + let content = Manual::new("drop") .about("a file dropper for Vento") .author(Author::new("Lux Aliaga").email("they@mint.lgbt")) .description("Take FILE off the inventory and drop it in DESTINATION.") @@ -111,11 +122,14 @@ fn drop() -> Result<(String, String)> { .arg(Arg::new("[DESTINATION]")) .render(); - Ok((page, String::from("drop.1"))) + Ok(Page { + content, + file: String::from("drop.1"), + }) } -fn ventotoml() -> Result<(String, String)> { - let page = Manual::new("vento.toml") +fn ventotoml() -> Result { + let content = Manual::new("vento.toml") .about("configuration file for Vento") .author(Author::new("Lux Aliaga").email("they@mint.lgbt")) .description("This is the configuration file for the vento(1), take(1) and drop(1) utilities. Its presence and all its directives are optional.") @@ -131,5 +145,8 @@ fn ventotoml() -> Result<(String, String)> { ) .render(); - Ok((page, String::from("vento.toml.1"))) + Ok(Page { + content, + file: String::from("vento.toml.1"), + }) } diff --git a/src/common.rs b/src/common.rs index c35edaa..072b1f2 100644 --- a/src/common.rs +++ b/src/common.rs @@ -22,7 +22,13 @@ use colored::Colorize; use config::Config; use std::path::{Path, PathBuf}; -pub fn env_config() -> Result> { +pub struct Settings { + pub vento_dir: PathBuf, + pub active_dir: PathBuf, + pub inactive_dir: PathBuf, +} + +pub fn env_config() -> Result { // Configures the directories for Vento let home = match dirs::home_dir() { Option::Some(dir) => dir, @@ -45,7 +51,11 @@ pub fn env_config() -> Result> { .iter() .collect(); - Ok(vec![vento_dir, active_dir, inactive_dir]) + Ok(Settings { + vento_dir, + active_dir, + inactive_dir, + }) } fn dir_config() -> Result { diff --git a/src/inv.rs b/src/inv.rs index 7d78775..701d82d 100644 --- a/src/inv.rs +++ b/src/inv.rs @@ -27,16 +27,14 @@ use std::{fs, process}; pub fn init() -> Result<()> { // Initializes Vento - let ventodir = &common::env_config()?[0]; + let ventodir = &common::env_config()?.vento_dir; if ventodir.is_dir() { // Checks if Vento has already been initialized and prompts the user if they want to initialize it again let mut answer = String::new(); print!("⚠️ {} Vento has already been initialized. Reinitializing will delete all files on the directory for Vento. Do you wish to proceed? (y/N) ", "WARNING:".bold().red()); let _ = io::stdout().flush(); - io::stdin() - .read_line(&mut answer) - .context("Failed to read input")?; + io::stdin().read_line(&mut answer)?; match answer.as_str().trim() { "y" | "Y" => fs::remove_dir_all(&ventodir)?, _ => process::exit(0), @@ -49,7 +47,7 @@ pub fn init() -> Result<()> { pub fn list(slot: &str, dir: &str) -> Result<()> { // Lists files in inventory - let ventodir = &common::env_config()?[0]; + let ventodir = &common::env_config()?.vento_dir; if !ventodir.is_dir() { // Detects if Vento hasn't been initialized and bails if so @@ -60,8 +58,8 @@ pub fn list(slot: &str, dir: &str) -> Result<()> { } let mut slotdir: PathBuf = match slot { - "active" | "a" => common::env_config()?[1].clone(), - "inactive" | "i" => common::env_config()?[2].clone(), + "active" | "a" => common::env_config()?.active_dir.clone(), + "inactive" | "i" => common::env_config()?.inactive_dir.clone(), _ => PathBuf::new(), }; @@ -168,9 +166,9 @@ pub fn list(slot: &str, dir: &str) -> Result<()> { pub fn switch() -> Result<()> { // Switches between inventory slots - let ventodir = &common::env_config()?[0]; - let active = &common::env_config()?[1]; - let inactive = &common::env_config()?[2]; + let ventodir = &common::env_config()?.vento_dir; + let active = &common::env_config()?.active_dir; + let inactive = &common::env_config()?.inactive_dir; let temp: PathBuf = [ventodir.to_path_buf(), Path::new("temp").to_path_buf()] .iter() .collect(); @@ -187,13 +185,11 @@ pub fn switch() -> Result<()> { fn create_slots() -> Result<()> { // Used only on init. Creates all required directories - let active = &common::env_config()?[1]; - let inactive = &common::env_config()?[2]; + let active = &common::env_config()?.active_dir; + let inactive = &common::env_config()?.inactive_dir; - let initialize_error = "Vento was unable to initalize. Do you have the correct permissions?"; - - fs::create_dir_all(active).context(initialize_error)?; - fs::create_dir_all(inactive).context(initialize_error)?; + fs::create_dir_all(active)?; + fs::create_dir_all(inactive)?; println!("🎉 {}", "Vento has been succesfully initialized!".green()); Ok(()) diff --git a/src/item.rs b/src/item.rs index c7eb1c9..f2f38a2 100644 --- a/src/item.rs +++ b/src/item.rs @@ -18,7 +18,7 @@ */ use super::common; -use anyhow::{bail, Context, Result}; +use anyhow::{bail, Result}; use colored::Colorize; use fs_extra::dir::{move_dir, CopyOptions}; use std::fs; @@ -26,7 +26,7 @@ use std::path::{Path, PathBuf}; pub fn take(file: &String, slot: &str) -> Result<()> { // Takes a file or directory - let ventodir = &common::env_config()?[0]; + let ventodir = &common::env_config()?.vento_dir; if !ventodir.is_dir() { // Detects if Vento hasn't been initialized and bails if so @@ -36,8 +36,8 @@ pub fn take(file: &String, slot: &str) -> Result<()> { ); }; let slotdir: PathBuf = match slot { - "active" | "a" => common::env_config()?[1].clone(), - "inactive" | "i" => common::env_config()?[2].clone(), + "active" | "a" => common::env_config()?.active_dir.clone(), + "inactive" | "i" => common::env_config()?.inactive_dir.clone(), _ => PathBuf::new(), }; @@ -80,11 +80,11 @@ pub fn take(file: &String, slot: &str) -> Result<()> { if sourcepath.is_file() | sourcepath.is_symlink() { // Checks the path's file type - fs::copy(&file, &destpath).context("Vento was unable to copy the file.")?; - fs::remove_file(&file).context("Vento was unable to remove the file.")?; + fs::copy(&file, &destpath)?; + fs::remove_file(&file)?; } else if sourcepath.is_dir() { let options = CopyOptions::new(); - move_dir(&file, &slotdir, &options).context("Vento was unable to move the directory.")?; + move_dir(&file, &slotdir, &options)?; } else { bail!("{}", "No such file or directory.".red()); } @@ -94,7 +94,7 @@ pub fn take(file: &String, slot: &str) -> Result<()> { pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> { // Drops a file or directory - let ventodir = &common::env_config()?[0]; + let ventodir = &common::env_config()?.vento_dir; if !ventodir.is_dir() { // Detects if Vento hasn't been initialized and bails if so @@ -105,8 +105,8 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> { }; let slotdir: PathBuf = match slot { - "active" | "a" => common::env_config()?[1].clone(), - "inactive" | "i" => common::env_config()?[2].clone(), + "active" | "a" => common::env_config()?.active_dir.clone(), + "inactive" | "i" => common::env_config()?.inactive_dir.clone(), _ => PathBuf::new(), }; @@ -138,13 +138,12 @@ pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> { if sourcepath.is_file() | sourcepath.is_symlink() { // Checks the path's file type - fs::copy(&sourcepath, &destpath).context("Vento was unable to copy the file.")?; - fs::remove_file(&sourcepath).context("Vento was unable to remove the file.")?; + fs::copy(&sourcepath, &destpath)?; + fs::remove_file(&sourcepath)?; } else if sourcepath.is_dir() { let destpath: PathBuf = Path::new(&dest).to_path_buf(); let options = CopyOptions::new(); - move_dir(&sourcepath, &destpath, &options) - .context("Vento was unable to move the directory.")?; + move_dir(&sourcepath, &destpath, &options)?; } else { bail!("{}", "No such file or directory.".red()); }