mirror of
https://git.sr.ht/~nixgoat/vento
synced 2024-11-16 12:02:49 +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:
parent
b9cea29592
commit
d51163ef09
|
@ -22,7 +22,13 @@ use colored::Colorize;
|
|||
use config::Config;
|
||||
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
|
||||
let home = match dirs::home_dir() {
|
||||
Option::Some(dir) => dir,
|
||||
|
@ -45,7 +51,11 @@ pub fn env_config() -> Result<Vec<PathBuf>> {
|
|||
.iter()
|
||||
.collect();
|
||||
|
||||
Ok(vec![vento_dir, active_dir, inactive_dir])
|
||||
Ok(Settings {
|
||||
vento_dir,
|
||||
active_dir,
|
||||
inactive_dir,
|
||||
})
|
||||
}
|
||||
|
||||
fn dir_config() -> Result<String> {
|
||||
|
|
18
src/inv.rs
18
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?";
|
||||
|
||||
|
|
12
src/item.rs
12
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(),
|
||||
};
|
||||
|
||||
|
|
Loading…
Reference in a new issue