11use crate :: depends:: satisfies_ver;
2- use alpm:: { Alpm , Depend , Package , Result } ;
2+ use crate :: DbListExt ;
3+ use alpm:: { Alpm , Depend , Package , PackageReason , Result , SigLevel } ;
4+ use std:: path:: Path ;
35
46/// Extension methods to the [`Alpm`] type.
57pub trait AlpmExt {
68 /// Try to find a [`Package`] that satisfies a given dependency.
7- fn find_local_satisfier < S > ( & self , pkg : S ) -> Result < Option < Package > >
9+ fn find_local_satisfier < S > ( & self , pkg : S ) -> Result < Option < Package < ' _ > > >
810 where
911 S : Into < String > ;
1012}
1113
1214impl AlpmExt for Alpm {
13- fn find_local_satisfier < S > ( & self , pkg : S ) -> Result < Option < Package > >
15+ fn find_local_satisfier < S > ( & self , pkg : S ) -> Result < Option < Package < ' _ > > >
1416 where
1517 S : Into < String > ,
1618 {
@@ -26,3 +28,43 @@ impl AlpmExt for Alpm {
2628 Ok ( localdb. pkgs ( ) . find_satisfier ( pkg) )
2729 }
2830}
31+
32+ /// All orphaned packages.
33+ ///
34+ /// An orphan is a package that was installed as a dependency, but whose parent
35+ /// package is no longer installed.
36+ pub fn orphans ( alpm : & Alpm ) -> impl Iterator < Item = Package < ' _ > > {
37+ alpm. localdb ( ) . pkgs ( ) . into_iter ( ) . filter ( |p| {
38+ p. reason ( ) == PackageReason :: Depend
39+ && p. required_by ( ) . is_empty ( )
40+ && p. optional_for ( ) . is_empty ( )
41+ } )
42+ }
43+
44+ /// All official packages.
45+ pub fn officials ( alpm : & Alpm ) -> impl Iterator < Item = Package < ' _ > > {
46+ let syncs = alpm. syncdbs ( ) ;
47+
48+ alpm. localdb ( )
49+ . pkgs ( )
50+ . into_iter ( )
51+ . filter_map ( move |p| syncs. pkg ( p. name ( ) ) . ok ( ) )
52+ }
53+
54+ /// All foreign packages as an `Iterator`.
55+ pub fn foreigns ( alpm : & Alpm ) -> impl Iterator < Item = Package < ' _ > > {
56+ let syncs = alpm. syncdbs ( ) ;
57+
58+ alpm. localdb ( )
59+ . pkgs ( )
60+ . into_iter ( )
61+ . filter ( move |p| syncs. pkg ( p. name ( ) ) . is_err ( ) )
62+ }
63+
64+ /// Does the given `Path` point to a valid tarball that can can loaded by ALPM?
65+ pub fn is_valid_package ( alpm : & Alpm , path : & Path ) -> bool {
66+ match path. to_str ( ) {
67+ None => false ,
68+ Some ( p) => path. exists ( ) && alpm. pkg_load ( p, true , SigLevel :: USE_DEFAULT ) . is_ok ( ) ,
69+ }
70+ }
0 commit comments