pacwrap/pacwrap/src/query.rs
Xavier Moffett d1622eb0b4
Revised error handling and some cleanup
- `fatal()` function for handling the termination of the program when a
  fatal human-readable error digestable error occurs. This function
  terminates the program and will never return a value.
- Much like the aforementioned function, the `error()` function now
  prints the error and terminates the program, instead of printing the
  error and returning the error code as an i32 integer.
- ErrorType enum implemented as an encapsulate to delineate error type.
- Display trait implemented for ErrorType enum for use cases whereby
  the program being terminated is inconvenient for the given code path.
- `generic()` function added to GenericError trait for handling errors
  of which do not require a message to be prepended.
- Global configuration is now statically cached with OnceLock instead of
  lazy_static, and is loaded on-demand instead of during the invocation
  of the `init()` function from the init module upon program startup.
- Use of lazy_static was also eliminated in pacwrap-core's sync module.
2024-09-19 23:19:26 -04:00

72 lines
2.2 KiB
Rust

/*
* pacwrap
*
* Copyright (C) 2023-2024 Xavier Moffett <sapphirus@azorium.net>
* SPDX-License-Identifier: GPL-3.0-only
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, version 3.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
use alpm::PackageReason;
use pacwrap_core::{
config,
constants::{BOLD_GREEN, RESET},
err,
error::*,
sync::{instantiate_alpm, transaction::TransactionFlags},
utils::{
arguments::{Arguments, InvalidArgument, Operand},
check_root,
},
};
pub fn query(arguments: &mut Arguments) -> Result<()> {
let mut flags: TransactionFlags = TransactionFlags::NONE;
let mut target = "";
let mut explicit = false;
let mut quiet = false;
check_root()?;
while let Some(arg) = arguments.next() {
match arg {
Operand::Long("debug") => flags |= TransactionFlags::DEBUG,
Operand::Long("target") | Operand::Short('t') => continue,
Operand::Short('e') | Operand::Long("explicit") => explicit = true,
Operand::Short('q') | Operand::Long("quiet") => quiet = true,
Operand::LongPos(_, t) | Operand::ShortPos(_, t) | Operand::Value(t) => target = t,
_ => arguments.invalid_operand()?,
}
}
if target.is_empty() {
err!(InvalidArgument::TargetUnspecified)?
}
let handle = config::provide_handle(target)?;
let handle = instantiate_alpm(&handle, &flags)?;
for pkg in handle.localdb().pkgs() {
if explicit && pkg.reason() != PackageReason::Explicit {
continue;
}
match quiet {
true => println!("{} ", pkg.name()),
false => println!("{} {}{}{} ", pkg.name(), *BOLD_GREEN, pkg.version(), *RESET),
}
}
Ok(())
}