Skip to content
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
76 changes: 64 additions & 12 deletions alpm-utils/src/alpm.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,74 @@
use crate::depends::satisfies_ver;
//! Extension methods for the [`Alpm`] type.

use alpm::{Alpm, Result, Package, Depend};
use crate::DbListExt;
use alpm::{Alpm, AlpmList, Db, IntoIter, Package};

/// Extension methods for [`Alpm`] which aren't critical enough to live in the
/// main crate, directly within `Alpm`.
pub trait AlpmExt {
fn find_local_satisfier<S: Into<String>>(&self, pkg: S) -> Result<Option<Package>>;
/// An iterator of [`Package`]s that are found in "sync databases",
/// typically registered in one's `pacman.conf`.
fn native_packages(&self) -> NativePkgs<'_>;

/// The opposite of [`AlpmExt::native_packages`]; installed packages that
/// aren't found in any registered "sync database".
fn foreign_packages(&self) -> ForeignPkgs<'_>;
}

impl AlpmExt for Alpm {
fn find_local_satisfier<S: Into<String>>(&self, pkg: S) -> Result<Option<Package>> {
let localdb = self.localdb();
let pkg = pkg.into();
fn native_packages(&self) -> NativePkgs<'_> {
NativePkgs::new(self)
}

fn foreign_packages(&self) -> ForeignPkgs<'_> {
ForeignPkgs::new(self)
}
}

/// [`Package`]s that are found in registered "sync databases".
pub struct NativePkgs<'a> {
local: IntoIter<'a, Package<'a>>,
sync: AlpmList<'a, Db<'a>>,
}

impl<'a> NativePkgs<'a> {
fn new(alpm: &'a Alpm) -> NativePkgs<'a> {
let local = alpm.localdb().pkgs().into_iter();
let sync = alpm.syncdbs();

NativePkgs { local, sync }
}
}

impl<'a> Iterator for NativePkgs<'a> {
type Item = Package<'a>;

fn next(&mut self) -> Option<Self::Item> {
let s = self.sync;
self.local.find(|p| s.pkg(p.name()).is_ok())
}
}

/// Installed [`Package`]s that are _not_ found in registered "sync databases".
pub struct ForeignPkgs<'a> {
local: IntoIter<'a, Package<'a>>,
sync: AlpmList<'a, Db<'a>>,
}

impl<'a> ForeignPkgs<'a> {
fn new(alpm: &'a Alpm) -> ForeignPkgs<'a> {
let local = alpm.localdb().pkgs().into_iter();
let sync = alpm.syncdbs();

ForeignPkgs { local, sync }
}
}

if let Ok(alpm_pkg) = localdb.pkg(&pkg) {
if satisfies_ver(&Depend::new(&pkg), alpm_pkg.version()) {
return Ok(Some(alpm_pkg));
}
}
impl<'a> Iterator for ForeignPkgs<'a> {
type Item = Package<'a>;

return Ok(localdb.pkgs()?.find_satisfier(pkg));
fn next(&mut self) -> Option<Self::Item> {
let s = self.sync;
self.local.find(|p| s.pkg(p.name()).is_err())
}
}
2 changes: 1 addition & 1 deletion alpm-utils/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use alpm::{AlpmList, Db, Package, Result};

use crate::AsTarg;

/// Extention for AlpmList<Db>
/// Extentions for `AlpmList<Db>`.
pub trait DbListExt<'a> {
/// Similar to find_satisfier() but expects a Target instead of a &str.
fn find_target_satisfier<T: AsTarg>(&self, target: T) -> Option<Package<'a>>;
Expand Down
6 changes: 1 addition & 5 deletions alpm-utils/src/depends.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,7 @@
use alpm::{AsDep, DepModVer, Ver};

/// Checks if a dependency is satisfied by a package (name + version).
pub fn satisfies_dep<S: AsRef<str>, V: AsRef<Ver>>(
dep: impl AsDep,
name: S,
version: V,
) -> bool {
pub fn satisfies_dep<S: AsRef<str>, V: AsRef<Ver>>(dep: impl AsDep, name: S, version: V) -> bool {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have you got some custom cargo fmt config?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't, but the last changes to these lines occurred in 2019, so there's a chance that rustfmt changed since then.

let name = name.as_ref();
let dep = dep.as_dep();

Expand Down
5 changes: 5 additions & 0 deletions alpm-utils/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,20 @@

#[cfg(feature = "conf")]
mod conf;

#[cfg(feature = "alpm")]
mod alpm;
mod db;

/// Utils for dependency checking.
#[cfg(feature = "alpm")]
pub mod depends;
mod target;

#[cfg(feature = "conf")]
pub use crate::conf::*;

#[cfg(feature = "alpm")]
pub use crate::alpm::*;
pub use crate::db::*;
pub use crate::target::*;