From 004556fff5af97620fbcb7f7aac2452841540a57 Mon Sep 17 00:00:00 2001 From: axtlos <3alouchi2006@gmail.com> Date: Sat, 16 Oct 2021 20:56:28 +0200 Subject: [PATCH] replaced the if else in main.rs with match, and fixed database --- src/main.rs | 97 +++++++++++++++++++++----------------------- src/mods/database.rs | 72 ++++++++++++++++++++------------ 2 files changed, 94 insertions(+), 75 deletions(-) diff --git a/src/main.rs b/src/main.rs index fdcf379..b1c71fc 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,20 +1,5 @@ mod mods; -use mods::{ - clearcache::clearcache, - clone::clone, - help::help, - inssort::inssort, - install::install, - search::{a_search, r_search}, - strs::err_rec, - strs::err_unrec, - strs::inf, - uninstall::uninstall, - update::update, - upgrade::upgrade, - ver::ver, - xargs::*, -}; +use mods::{clearcache::{clearcache}, clone::clone, help::help, inssort::inssort, install::install, search::{a_search, r_search}, strs::err_rec, strs::err_unrec, strs::inf, uninstall::{uninstall}, update::{update}, upgrade::{upgrade}, ver::ver, xargs::*}; use std::{env, process::exit, process::Command}; fn main() { @@ -33,42 +18,54 @@ fn main() { // at some point weve GOT TO rework this into a `match` statement - if oper == "-S" || oper == "-Sn" || oper == "ins" { - inssort(noconfirm, pkgs); - } else if oper == "-R" || oper == "-Rn " || oper == "-Rsn" || oper == "-Rs" || oper == "rm" { - uninstall(noconfirm, pkgs); - } else if oper == "-Syu" || oper == "-Syun" || oper == "upg" { - upgrade(noconfirm); - } else if oper == "-Sy" || oper == "upd" { - update(); - } else if oper == "-Ss" || oper == "sea" { - r_search(&args[2]); - a_search(&args[2]); - } else if oper == "-Sa" || oper == "aursea" { - a_search(&args[2]); - } else if oper == "-Sr" || oper == "repsea" { - r_search(&args[2]); - } else if oper == "-Cc" || oper == "clr" { - clearcache(); - } else if oper == "-v" || oper == "-V" || oper == "ver" { - ver(); - } else if oper == "-h" || oper == "help" { - help(); - } else { - let pass = Command::new("pacman") - .args(env::args().skip(1)) - .status() - .expect("Something has gone wrong."); + match oper.as_str() { + "-S" | "-Sn" | "ins" => { + inssort(noconfirm, pkgs); + } + "-R" | "-Rn" | "-Rsn" | "-Rs" | "rm" => { + uninstall(noconfirm, pkgs); + } + "-Syu" | "-Syun" |"upg" => { + upgrade(noconfirm); + } + "-Sy" | "upd" => { + update(); + } + "-Ss" | "sea" => { + r_search(&args[2]); + a_search(&args[2]); + } + "-Sa" | "aursea" => { + a_search(&args[2]); + } + "-Sr" | "repsea" => { + r_search(&args[2]); + } + "-Cc" | "clr" => { + clearcache(); + } + "-v" | "-V" | "ver" => { + ver(); + } + "-h" | "help" => { + help() + } + _ => { + let pass = Command::new("pacman") + .args(env::args().skip(1)) + .status() + .expect("Something has gone wrong."); - match pass.code() { - Some(1) => { - err_rec(format!("No such operation \"{}\"", args.join(" "))); - inf(format!( - "Try running \"ame help\" for an overview of how to use ame" - )) + match pass.code() { + Some(1) => { + err_rec(format!("No such operation \"{}\"", args.join(" "))); + inf(format!( + "Try running \"ame help\" for an overview of how to use ame" + )) + } + Some(_) => {} + None => err_unrec(format!("Something has gone terribly wrong.")), } - Some(_) => {} - None => err_unrec(format!("Something has gone terribly wrong.")), } } } diff --git a/src/mods/database.rs b/src/mods/database.rs index 3e97ca2..1044a20 100644 --- a/src/mods/database.rs +++ b/src/mods/database.rs @@ -5,21 +5,34 @@ use toml_edit::{value, Document}; use crate::mods::strs::{err_rec}; pub fn rem_pkg(pkgs: &Vec) { + let homepath = std::env::var("HOME").unwrap(); let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); - let database = String::new(); - if std::path::Path::new(&file).exists() { - let _db = std::fs::read_to_string(&file); - match _db { - Ok(_) => { - inf(format!("Database parsed")) + let mut database = String::new(); + match std::path::Path::new(&file).exists() { + true => { + database = std::fs::read_to_string(&file).expect("Can't Open Database"); + } + false => { + let _cdar = std::fs::create_dir_all(format!("/{}/.local/ame/",homepath)); + match _cdar { + Ok(_) => { + inf(format!("Created path for database (previously missing)")) + } + Err(_) => { + err_unrec(format!("Couldn't create path for database (~/.local/ame)")) + } } - Err(_) => { - err_unrec(format!("Couldn't open database")) + err_rec(String::from("Datbase wasn't found, creating new one")); + let _dbfile = std::fs::File::create(&file); + match _dbfile { + Ok(_) => { + inf(format!("Created empty database (previously missing)")) + } + Err(_) => { + err_unrec(format!("Couldn't create database")) + } } } - } else { - err_rec(String::from("Database wasn't found, creating new one")); - let _dbfile = File::create(&file); } let mut update_database = database; @@ -46,26 +59,35 @@ pub fn rem_pkg(pkgs: &Vec) { pub fn add_pkg(from_repo: bool, pkg: &str) -> Result<(), Error> { let homepath = std::env::var("HOME").unwrap(); let file = format!("{}/.local/ame/aurPkgs.db", std::env::var("HOME").unwrap()); - let database = String::new(); - if std::path::Path::new(&file).exists() { - let database = std::fs::read_to_string(&file).expect("Can't Open Database"); - } else { - let _cdar = std::fs::create_dir_all(format!("/{}/.local/ame/", homepath)); - match _cdar { - Ok(_) => { - inf(format!("Created cache directory (previously missing)")) + let mut database = String::new(); + match std::path::Path::new(&file).exists() { + true => { + database = std::fs::read_to_string(&file).expect("Can't Open Database"); + } + false => { + let _cdar = std::fs::create_dir_all(format!("/{}/.local/ame/",homepath)); + match _cdar { + Ok(_) => { + inf(format!("Created path for database (previously missing)")) + } + Err(_) => { + err_unrec(format!("Couldn't create path for database (~/.local/ame)")) + } } - Err(_) => { - err_unrec(format!("Couldn't create cache directory")) + err_rec(String::from("Datbase wasn't found, creating new one")); + let _dbfile = std::fs::File::create(&file); + match _dbfile { + Ok(_) => { + inf(format!("Created empty database (previously missing)")) + } + Err(_) => { + err_unrec(format!("Couldn't create database")) + } } } - err_rec(String::from("Database wasn't found, creating new one")); - let _dbfile = std::fs::File::create(&file); - let database = String::new(); } let mut db_parsed = database.parse::().expect("Invalid Database"); let mut file_as_path = File::create(std::path::Path::new(&file))?; - if from_repo == false { let results = raur::search(&pkg); for res in &results {