From d51163ef09fb5453f2d0f6b5c7d5e17ca98eabf1 Mon Sep 17 00:00:00 2001 From: Lux Aliaga Date: Mon, 17 Oct 2022 18:11:38 -0300 Subject: [PATCH 1/3] common: Use struct to deliver settings After learning more about structs, I've decided to try and use them in the common module. It replaces the original behavior of delivering the data using a vector in favor of the Settings struct, also defined on the same file. --- src/common.rs | 14 ++++++++++++-- src/inv.rs | 18 +++++++++--------- src/item.rs | 12 ++++++------ 3 files changed, 27 insertions(+), 17 deletions(-) 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..8ab9e87 100644 --- a/src/inv.rs +++ b/src/inv.rs @@ -27,7 +27,7 @@ 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 @@ -49,7 +49,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 +60,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 +168,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,8 +187,8 @@ 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?"; diff --git a/src/item.rs b/src/item.rs index c7eb1c9..7cbfa06 100644 --- a/src/item.rs +++ b/src/item.rs @@ -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(), }; @@ -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(), }; From 5e05bfa2ef5dbefe1c55d26eda31f1e84e206ef9 Mon Sep 17 00:00:00 2001 From: Lux Aliaga Date: Mon, 17 Oct 2022 19:56:23 -0300 Subject: [PATCH 2/3] src: Removed unnecessary contexts This commit should remove some contexts that actually made some errors more difficult to read. They will now display the idiomatic errors generated by anyhow. --- src/inv.rs | 10 +++------- src/item.rs | 15 +++++++-------- 2 files changed, 10 insertions(+), 15 deletions(-) diff --git a/src/inv.rs b/src/inv.rs index 8ab9e87..701d82d 100644 --- a/src/inv.rs +++ b/src/inv.rs @@ -34,9 +34,7 @@ pub fn init() -> Result<()> { 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), @@ -190,10 +188,8 @@ fn create_slots() -> Result<()> { 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 7cbfa06..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; @@ -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()); } @@ -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()); } From 3b9a405614cabec2a36b69280e38f462b71f6c83 Mon Sep 17 00:00:00 2001 From: Lux Aliaga Date: Mon, 17 Oct 2022 20:09:44 -0300 Subject: [PATCH 3/3] build: Use struct for page data The tuple approach was a bit complicated, so I decided to switch it out for a Page struct. Yes, I have an obsession with structs. --- build.rs | 45 +++++++++++++++++++++++++++++++-------------- 1 file changed, 31 insertions(+), 14 deletions(-) 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"), + }) }