Merge branch 'main' into feature/async

i18n
trivernis 2 years ago
commit f1bbb2626c
Signed by: Trivernis
GPG Key ID: DFFFCC2C7A02DB45

@ -1,5 +1,6 @@
#![allow(clippy::module_name_repetitions)] #![allow(clippy::module_name_repetitions)]
use crate::operations::SearchBy;
use clap::{Parser, Subcommand, ValueHint}; use clap::{Parser, Subcommand, ValueHint};
#[derive(Debug, Clone, Parser)] #[derive(Debug, Clone, Parser)]
@ -104,7 +105,11 @@ pub struct SearchArgs {
/// The string the package must match in the search /// The string the package must match in the search
#[clap(required = true)] #[clap(required = true)]
pub search: Vec<String>, pub search: String,
/// Searches by a specific field
#[clap(long, short)]
pub by: Option<SearchBy>,
} }
#[derive(Default, Debug, Clone, Parser)] #[derive(Default, Debug, Clone, Parser)]

@ -1,4 +1,4 @@
use aur_rpc::{PackageInfo, PackageMetadata}; use aur_rpc::{PackageInfo, PackageMetadata, SearchField};
use super::error::AppResult; use super::error::AppResult;
pub const URL: &str = "https://aur.archlinux.org/"; pub const URL: &str = "https://aur.archlinux.org/";
@ -9,8 +9,15 @@ pub async fn rpcinfo(pkg: &str) -> AppResult<Option<PackageInfo>> {
Ok(packages.into_iter().next()) Ok(packages.into_iter().next())
} }
pub async fn rpcsearch(pkg: String) -> AppResult<Vec<PackageMetadata>> { pub async fn rpcsearch(
let search_results = aur_rpc::search(pkg).await?; query: String,
by_field: Option<SearchField>,
) -> AppResult<Vec<PackageMetadata>> {
let search_results = if let Some(field) = by_field {
aur_rpc::search_by(field, query).await?
} else {
aur_rpc::search(query).await?
};
Ok(search_results) Ok(search_results)
} }

@ -126,10 +126,11 @@ async fn cmd_remove(args: RemoveArgs, options: Options) {
#[tracing::instrument(level = "trace")] #[tracing::instrument(level = "trace")]
async fn cmd_search(args: SearchArgs, options: Options) { async fn cmd_search(args: SearchArgs, options: Options) {
let query_string = args.search.join(" "); let query_string = args.search;
if args.aur { if args.aur {
info!("Searching AUR for {}", &query_string); info!("Searching AUR for {}", &query_string);
operations::aur_search(&query_string, options).await; operations::aur_search(&query_string, args.by, options).await;
} }
if args.repo { if args.repo {
info!("Searching repos for {}", &query_string); info!("Searching repos for {}", &query_string);
@ -139,7 +140,7 @@ async fn cmd_search(args: SearchArgs, options: Options) {
if !args.aur && !args.repo { if !args.aur && !args.repo {
info!("Searching AUR and repos for {}", &query_string); info!("Searching AUR and repos for {}", &query_string);
operations::search(&query_string, options).await; operations::search(&query_string, options).await;
operations::aur_search(&query_string, options).await; operations::aur_search(&query_string, args.by, options).await;
} }
} }

@ -1,7 +1,7 @@
pub use aur_install::*; pub use aur_install::*;
pub use clean::*; pub use clean::*;
pub use install::*; pub use install::*;
pub use search::{aur_search, repo_search as search}; pub use search::{aur_search, repo_search as search, SearchBy};
pub use uninstall::*; pub use uninstall::*;
pub use upgrade::*; pub use upgrade::*;

@ -1,13 +1,16 @@
use std::str::FromStr;
use crate::internal::commands::ShellCommand; use crate::internal::commands::ShellCommand;
use crate::internal::error::SilentUnwrap; use crate::internal::error::SilentUnwrap;
use crate::internal::exit_code::AppExitCode; use crate::internal::exit_code::AppExitCode;
use crate::internal::rpc::rpcsearch; use crate::internal::rpc::rpcsearch;
use crate::{log, Options}; use crate::{log, Options};
use aur_rpc::SearchField;
#[tracing::instrument(level = "trace")] #[tracing::instrument(level = "trace")]
pub async fn aur_search(query: &str, options: Options) { pub async fn aur_search(query: &str, by_field: Option<SearchBy>, options: Options) {
let verbosity = options.verbosity; let verbosity = options.verbosity;
let packages = rpcsearch(query.to_string()) let packages = rpcsearch(query.to_string(), by_field.map(SearchBy::into))
.await .await
.silent_unwrap(AppExitCode::RpcError); .silent_unwrap(AppExitCode::RpcError);
let total_results = packages.len(); let total_results = packages.len();
@ -45,3 +48,56 @@ pub async fn repo_search(query: &str, options: Options) {
println!("{}", output) println!("{}", output)
} }
/// Represents a field to search by
#[derive(Debug, Clone, Copy)]
pub enum SearchBy {
/// Searches by name
Name,
/// Searches name and description
NameDesc,
/// Searches by package maintainer
Maintainer,
/// Searches for packages that depend on the given keywods
Depends,
/// Searches for packages that require the given keywords to be build
MakeDepends,
/// Searches for packages that optionally depend on the given keywods
OptDepends,
/// Searches for packages that require the given keywods to be present
CheckDepends,
}
impl FromStr for SearchBy {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let arg = match s {
"name" => Self::Name,
"name-desc" => Self::NameDesc,
"maintainer" => Self::Maintainer,
"depends" => Self::Depends,
"makedepends" | "make-depends" => Self::MakeDepends,
"optdepends" | "opt-depends" => Self::OptDepends,
"checkdepends" | "check-depends" => Self::CheckDepends,
directive => return Err(format!("Invalid search by directive '{directive}'")),
};
Ok(arg)
}
}
#[allow(clippy::from_over_into)]
impl Into<SearchField> for SearchBy {
fn into(self) -> SearchField {
match self {
SearchBy::Name => SearchField::Name,
SearchBy::NameDesc => SearchField::NameDesc,
SearchBy::Maintainer => SearchField::Maintainer,
SearchBy::Depends => SearchField::Depends,
SearchBy::MakeDepends => SearchField::MakeDepends,
SearchBy::OptDepends => SearchField::OptDepends,
SearchBy::CheckDepends => SearchField::CheckDepends,
}
}
}

Loading…
Cancel
Save