1
1
use 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 ;
3
5
4
6
/// Extension methods to the [`Alpm`] type.
5
7
pub trait AlpmExt {
6
8
/// 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 < ' _ > > >
8
10
where
9
11
S : Into < String > ;
10
12
}
11
13
12
14
impl 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 < ' _ > > >
14
16
where
15
17
S : Into < String > ,
16
18
{
@@ -26,3 +28,43 @@ impl AlpmExt for Alpm {
26
28
Ok ( localdb. pkgs ( ) . find_satisfier ( pkg) )
27
29
}
28
30
}
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