1
0
Fork 0
mirror of https://git.sr.ht/~nixgoat/vento synced 2025-12-01 08:57:24 +00:00

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.
This commit is contained in:
Lux Aliaga 2022-10-17 18:11:38 -03:00
parent b9cea29592
commit d51163ef09
Signed by: lux
GPG key ID: B56C805968637437
3 changed files with 27 additions and 17 deletions

View file

@ -22,7 +22,13 @@ use colored::Colorize;
use config::Config; use config::Config;
use std::path::{Path, PathBuf}; use std::path::{Path, PathBuf};
pub fn env_config() -> Result<Vec<PathBuf>> { pub struct Settings {
pub vento_dir: PathBuf,
pub active_dir: PathBuf,
pub inactive_dir: PathBuf,
}
pub fn env_config() -> Result<Settings> {
// Configures the directories for Vento // Configures the directories for Vento
let home = match dirs::home_dir() { let home = match dirs::home_dir() {
Option::Some(dir) => dir, Option::Some(dir) => dir,
@ -45,7 +51,11 @@ pub fn env_config() -> Result<Vec<PathBuf>> {
.iter() .iter()
.collect(); .collect();
Ok(vec![vento_dir, active_dir, inactive_dir]) Ok(Settings {
vento_dir,
active_dir,
inactive_dir,
})
} }
fn dir_config() -> Result<String> { fn dir_config() -> Result<String> {

View file

@ -27,7 +27,7 @@ use std::{fs, process};
pub fn init() -> Result<()> { pub fn init() -> Result<()> {
// Initializes Vento // Initializes Vento
let ventodir = &common::env_config()?[0]; let ventodir = &common::env_config()?.vento_dir;
if ventodir.is_dir() { if ventodir.is_dir() {
// Checks if Vento has already been initialized and prompts the user if they want to initialize it again // 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<()> { pub fn list(slot: &str, dir: &str) -> Result<()> {
// Lists files in inventory // Lists files in inventory
let ventodir = &common::env_config()?[0]; let ventodir = &common::env_config()?.vento_dir;
if !ventodir.is_dir() { if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so // 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 { let mut slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?[1].clone(), "active" | "a" => common::env_config()?.active_dir.clone(),
"inactive" | "i" => common::env_config()?[2].clone(), "inactive" | "i" => common::env_config()?.inactive_dir.clone(),
_ => PathBuf::new(), _ => PathBuf::new(),
}; };
@ -168,9 +168,9 @@ pub fn list(slot: &str, dir: &str) -> Result<()> {
pub fn switch() -> Result<()> { pub fn switch() -> Result<()> {
// Switches between inventory slots // Switches between inventory slots
let ventodir = &common::env_config()?[0]; let ventodir = &common::env_config()?.vento_dir;
let active = &common::env_config()?[1]; let active = &common::env_config()?.active_dir;
let inactive = &common::env_config()?[2]; let inactive = &common::env_config()?.inactive_dir;
let temp: PathBuf = [ventodir.to_path_buf(), Path::new("temp").to_path_buf()] let temp: PathBuf = [ventodir.to_path_buf(), Path::new("temp").to_path_buf()]
.iter() .iter()
.collect(); .collect();
@ -187,8 +187,8 @@ pub fn switch() -> Result<()> {
fn create_slots() -> Result<()> { fn create_slots() -> Result<()> {
// Used only on init. Creates all required directories // Used only on init. Creates all required directories
let active = &common::env_config()?[1]; let active = &common::env_config()?.active_dir;
let inactive = &common::env_config()?[2]; let inactive = &common::env_config()?.inactive_dir;
let initialize_error = "Vento was unable to initalize. Do you have the correct permissions?"; let initialize_error = "Vento was unable to initalize. Do you have the correct permissions?";

View file

@ -26,7 +26,7 @@ use std::path::{Path, PathBuf};
pub fn take(file: &String, slot: &str) -> Result<()> { pub fn take(file: &String, slot: &str) -> Result<()> {
// Takes a file or directory // Takes a file or directory
let ventodir = &common::env_config()?[0]; let ventodir = &common::env_config()?.vento_dir;
if !ventodir.is_dir() { if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so // 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 { let slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?[1].clone(), "active" | "a" => common::env_config()?.active_dir.clone(),
"inactive" | "i" => common::env_config()?[2].clone(), "inactive" | "i" => common::env_config()?.inactive_dir.clone(),
_ => PathBuf::new(), _ => PathBuf::new(),
}; };
@ -94,7 +94,7 @@ pub fn take(file: &String, slot: &str) -> Result<()> {
pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> { pub fn drop(file: &String, slot: &str, dest: PathBuf) -> Result<()> {
// Drops a file or directory // Drops a file or directory
let ventodir = &common::env_config()?[0]; let ventodir = &common::env_config()?.vento_dir;
if !ventodir.is_dir() { if !ventodir.is_dir() {
// Detects if Vento hasn't been initialized and bails if so // 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 { let slotdir: PathBuf = match slot {
"active" | "a" => common::env_config()?[1].clone(), "active" | "a" => common::env_config()?.active_dir.clone(),
"inactive" | "i" => common::env_config()?[2].clone(), "inactive" | "i" => common::env_config()?.inactive_dir.clone(),
_ => PathBuf::new(), _ => PathBuf::new(),
}; };