Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor code for readability and efficiency #1151

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion scripts/mkmo
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ if [ -z "$1" ]; then
fi

for po in po/*.po; do
lang=$(basename ${po%.po})
lang=$(basename "${po%.po}")
install -dm755 "$1/$lang/LC_MESSAGES/"
msgfmt "$po" -o "$1/$lang/LC_MESSAGES/paru.mo"
done
6 changes: 3 additions & 3 deletions src/command_line.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,12 +106,12 @@ impl Config {
while let Some(c) = chars.next() {
let arg = Arg::Short(c);
if takes_value(arg) == TakesValue::Required {
if chars.as_str().is_empty() {
return if chars.as_str().is_empty() {
self.handle_arg(arg, value, op_count, false)?;
return Ok(true);
Ok(true)
} else {
self.handle_arg(arg, Some(chars.as_str()), op_count, false)?;
return Ok(false);
Ok(false)
}
}
self.handle_arg(arg, None, op_count, false)?;
Expand Down
8 changes: 4 additions & 4 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -696,11 +696,11 @@ impl Config {
}

self.args.op = self.op.as_str().to_string();
self.args.targets = self.targets.clone();
self.args.bin = self.pacman_bin.clone();
self.args.targets.clone_from(&self.targets);
self.args.bin.clone_from(&self.pacman_bin);

self.globals.op = self.op.as_str().to_string();
self.globals.bin = self.pacman_bin.clone();
self.globals.bin.clone_from(&self.pacman_bin);

if self.help {
match self.op {
Expand Down Expand Up @@ -848,7 +848,7 @@ impl Config {
)?;

if let Some(ref dbpath) = self.db_path {
self.pacman.db_path = dbpath.clone();
self.pacman.db_path.clone_from(dbpath);
}

self.ignore.extend(self.pacman.ignore_pkg.clone());
Expand Down
15 changes: 7 additions & 8 deletions src/devel.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::collections::{BTreeMap, BTreeSet, HashMap, HashSet};
use std::fs::{create_dir_all, read_to_string, OpenOptions};
use std::hash::{Hash, Hasher};
use std::io::Write;
use std::iter::FromIterator;
use std::time::Duration;

use alpm_utils::{DbListExt, Target};
Expand Down Expand Up @@ -59,7 +58,7 @@ impl Ord for RepoInfo {
}
}

impl std::cmp::PartialEq for RepoInfo {
impl PartialEq for RepoInfo {
fn eq(&self, other: &Self) -> bool {
self.url == other.url && self.branch == other.branch
}
Expand Down Expand Up @@ -348,7 +347,7 @@ pub async fn possible_devel_updates(config: &Config) -> Result<Vec<String>> {
let mut pkgbases: HashMap<&str, Vec<&alpm::Package>> = HashMap::new();

for pkg in db.pkgs().iter() {
let name = pkg_base_or_name(&pkg);
let name = pkg_base_or_name(pkg);
pkgbases.entry(name).or_default().push(pkg);
}

Expand Down Expand Up @@ -411,12 +410,12 @@ pub async fn filter_devel_updates(

let (_, dbs) = repo::repo_aur_dbs(config);
for pkg in dbs.iter().flat_map(|d| d.pkgs()) {
let name = pkg_base_or_name(&pkg);
let name = pkg_base_or_name(pkg);
pkgbases.entry(name).or_default().push(pkg);
}

for pkg in db.pkgs().iter() {
let name = pkg_base_or_name(&pkg);
let name = pkg_base_or_name(pkg);
pkgbases.entry(name).or_default().push(pkg);
}

Expand Down Expand Up @@ -571,19 +570,19 @@ pub fn load_devel_info(config: &Config) -> Result<Option<DevelInfo>> {
}

for pkg in config.alpm.localdb().pkgs().iter() {
let name = pkg_base_or_name(&pkg);
let name = pkg_base_or_name(pkg);
pkgbases.entry(name).or_default().push(pkg);
}

let (_, dbs) = repo::repo_aur_dbs(config);
for pkg in dbs.iter().flat_map(|d| d.pkgs()) {
let name = pkg_base_or_name(&pkg);
let name = pkg_base_or_name(pkg);
pkgbases.entry(name).or_default().push(pkg);
}

devel_info
.info
.retain(|pkg, _| pkgbases.get(pkg.as_str()).is_some());
.retain(|pkg, _| pkgbases.contains_key(pkg.as_str()));

save_devel_info(config, &devel_info)?;

Expand Down
3 changes: 1 addition & 2 deletions src/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ use crate::RaurHandle;

use std::collections::HashMap;
use std::io::Write;
use std::iter::FromIterator;
use std::process::{Command, Stdio};
use std::result::Result as StdResult;

Expand Down Expand Up @@ -238,7 +237,7 @@ fn repo_pkgbuilds(config: &Config, pkgs: &[Targ<'_>]) -> Result<i32> {
"{} {} export {}",
tr!("failed to run:"),
pkgctl,
targ.to_string()
targ
)
})?;

Expand Down
4 changes: 2 additions & 2 deletions src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -174,7 +174,7 @@ fn wait_for_lock(config: &Config) {
);

while path.exists() {
std::thread::sleep(Duration::from_secs(3));
thread::sleep(Duration::from_secs(3));
}
}
}
Expand All @@ -201,7 +201,7 @@ pub fn pacman<S: AsRef<str> + Display + Debug>(config: &Config, args: &Args<S>)
command_status(&mut cmd)
}

pub fn pacman_output<S: AsRef<str> + Display + std::fmt::Debug>(
pub fn pacman_output<S: AsRef<str> + Display + Debug>(
config: &Config,
args: &Args<S>,
) -> Result<Output> {
Expand Down
18 changes: 9 additions & 9 deletions src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -642,7 +642,7 @@ impl Installer {
.iter()
.find(|db| db.name() == *repo)
.unwrap();
let path = repo::file(&repo).unwrap();
let path = repo::file(repo).unwrap();
let name = repo.name().to_string();
repo::add(config, path, &name, &paths)?;
repo::refresh(config, &[name])?;
Expand Down Expand Up @@ -820,7 +820,7 @@ impl Installer {
let (_, repo) = repo::repo_aur_dbs(config);
let default_repo = repo.first();
if let Some(repo) = default_repo {
let file = repo::file(&repo).unwrap();
let file = repo::file(repo).unwrap();
repo::init(config, file, repo.name())?;
}

Expand All @@ -833,7 +833,7 @@ impl Installer {
}

let repo_server =
default_repo.map(|r| (r.name().to_string(), repo::file(&r).unwrap().to_string()));
default_repo.map(|r| (r.name().to_string(), repo::file(r).unwrap().to_string()));
drop(repo);

for base in build {
Expand Down Expand Up @@ -871,7 +871,7 @@ impl Installer {

config.set_op_args_globals(Op::Sync);
config.targets = targets_str.to_vec();
config.args.targets = config.targets.clone();
config.args.targets.clone_from(&config.targets);

let targets = args::parse_targets(targets_str);
let (mut repo_targets, aur_targets) = split_repo_aur_targets(config, &targets)?;
Expand Down Expand Up @@ -1243,7 +1243,7 @@ fn is_debug(pkg: &alpm::Package) -> bool {
}

fn print_warnings(config: &Config, cache: &Cache, actions: Option<&Actions>) {
let mut warnings = crate::download::Warnings::default();
let mut warnings = download::Warnings::default();

if !config.mode.aur() && !config.mode.pkgbuild() {
return;
Expand All @@ -1256,14 +1256,14 @@ fn print_warnings(config: &Config, cache: &Cache, actions: Option<&Actions>) {
warnings.missing = pkgs
.iter()
.filter(|pkg| !cache.contains(pkg.name()))
.filter(|pkg| !is_debug(**pkg))
.filter(|pkg| !is_debug(pkg))
.map(|pkg| pkg.name())
.filter(|pkg| !config.no_warn.is_match(pkg))
.collect::<Vec<_>>();

warnings.ood = pkgs
.iter()
.filter(|pkg| !is_debug(**pkg))
.filter(|pkg| !is_debug(pkg))
.filter_map(|pkg| cache.get(pkg.name()))
.filter(|pkg| pkg.out_of_date.is_some())
.map(|pkg| pkg.name.as_str())
Expand All @@ -1272,7 +1272,7 @@ fn print_warnings(config: &Config, cache: &Cache, actions: Option<&Actions>) {

warnings.orphans = pkgs
.iter()
.filter(|pkg| !is_debug(**pkg))
.filter(|pkg| !is_debug(pkg))
.filter_map(|pkg| cache.get(pkg.name()))
.filter(|pkg| pkg.maintainer.is_none())
.map(|pkg| pkg.name.as_str())
Expand Down Expand Up @@ -1698,7 +1698,7 @@ pub fn review(config: &Config, fetch: &aur_fetch::Fetch, pkgs: &[&str]) -> Resul
exec::RAISE_SIGPIPE.store(false, Ordering::Relaxed);
let mut command = Command::new("sh");

if std::env::var("LESS").is_err() {
if var("LESS").is_err() {
command.env("LESS", "SRXF");
}
let mut command = command
Expand Down
25 changes: 11 additions & 14 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ mod resolver;
#[cfg(not(feature = "mock"))]
type RaurHandle = raur::Handle;
#[cfg(feature = "mock")]
type RaurHandle = crate::mock::Mock;
type RaurHandle = mock::Mock;

#[macro_use]
extern crate smart_default;
Expand Down Expand Up @@ -147,18 +147,15 @@ pub async fn run<S: AsRef<str>>(args: &[S]) -> i32 {
}
};

match run2(&mut config, args).await {
Err(err) => {
let code = if let Some(e) = err.downcast_ref::<install::Status>() {
e.0
} else {
1
};
print_error(Style::new(), err);
code
}
Ok(ret) => ret,
}
run2(&mut config, args).await.unwrap_or_else(|err| {
let code = if let Some(e) = err.downcast_ref::<install::Status>() {
e.0
} else {
1
};
print_error(Style::new(), err);
code
})
}

async fn run2<S: AsRef<str>>(config: &mut Config, args: &[S]) -> Result<i32> {
Expand Down Expand Up @@ -362,7 +359,7 @@ async fn handle_sync(config: &mut Config) -> Result<i32> {
Ok(exec::pacman(config, &config.args)?.code())
} else {
if config.interactive {
search::interactive_search(config, true).await?;
interactive_search(config, true).await?;
if config.targets.is_empty() {
return Ok(1);
}
Expand Down
2 changes: 1 addition & 1 deletion src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ impl Mock {

#[async_trait]
impl Raur for Mock {
type Err = raur::Error;
type Err = Error;

async fn raw_info<S: AsRef<str> + Send + Sync>(
&self,
Expand Down
2 changes: 1 addition & 1 deletion src/remove.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub fn remove(config: &mut Config) -> Result<i32> {
.targets
.iter()
.filter_map(|pkg| db.pkg(pkg.as_str()).ok())
.map(|pkg| pkg_base_or_name(&pkg))
.map(|pkg| pkg_base_or_name(pkg))
.collect::<Vec<_>>();

let mut db_map: HashMap<String, Vec<String>> = HashMap::new();
Expand Down
8 changes: 4 additions & 4 deletions src/repo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ pub fn all_files(config: &Config) -> Vec<String> {
.collect()
}

fn is_local_db(db: &alpm::Db) -> bool {
fn is_local_db(db: &Db) -> bool {
!db.servers().is_empty() && db.servers().iter().all(|s| s.starts_with("file://"))
}

Expand All @@ -180,13 +180,13 @@ pub fn refresh<S: AsRef<OsStr>>(config: &mut Config, repos: &[S]) -> Result<i32>
}

for db in dbs {
let path = file(&db);
let path = file(db);
if let Some(path) = path {
init(config, path, db.name())?;
}
}

if !nix::unistd::getuid().is_root() && !cfg!(feature = "mock") {
if !unistd::getuid().is_root() && !cfg!(feature = "mock") {
let mut cmd = Command::new(&config.sudo_bin);

cmd.arg(exe);
Expand Down Expand Up @@ -277,7 +277,7 @@ pub fn clean(config: &mut Config) -> Result<i32> {

for pkgs in rem {
let repo = pkgs[0].db().unwrap();
let path = file(&repo).unwrap();
let path = file(repo).unwrap();
let pkgs = pkgs.iter().map(|p| p.name()).collect::<Vec<_>>();
remove(config, path, repo.name(), &pkgs)?;
}
Expand Down
4 changes: 2 additions & 2 deletions src/resolver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use aur_depends::{Flags, PkgbuildRepo, Resolver};
use raur::Cache;
use tr::tr;

pub fn flags(config: &mut Config) -> aur_depends::Flags {
pub fn flags(config: &mut Config) -> Flags {
let mut flags = Flags::new();

if config.args.has_arg("needed", "needed") {
Expand Down Expand Up @@ -71,7 +71,7 @@ pub fn resolver<'a, 'b>(
let c = config.color;
let no_confirm = config.no_confirm;

let mut resolver = aur_depends::Resolver::new(alpm, cache, raur, flags)
let mut resolver = Resolver::new(alpm, cache, raur, flags)
.pkgbuild_repos(pkgbuild_repos)
.custom_aur_namespace(Some(config.aur_namespace().to_string()))
.is_devel(move |pkg| devel_suffixes.iter().any(|suff| pkg.ends_with(suff)))
Expand Down
2 changes: 1 addition & 1 deletion src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -391,7 +391,7 @@ pub fn split_repo_aur_pkgs<S: AsRef<str> + Clone>(config: &Config, pkgs: &[S]) -
(repo, aur)
}

pub fn repo_aur_pkgs(config: &Config) -> (Vec<&alpm::Package>, Vec<&alpm::Package>) {
pub fn repo_aur_pkgs(config: &Config) -> (Vec<&Package>, Vec<&Package>) {
if config.repos != LocalRepos::None {
let (repo, aur) = repo::repo_aur_dbs(config);
let repo = repo.iter().flat_map(|db| db.pkgs()).collect::<Vec<_>>();
Expand Down