1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
use alpm::{AlpmList, AlpmListMut, Db, Package, Result};

use crate::AsTarg;

/// Extention 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<&'a Package>;
    /// Similar to pkg() but expects a Target instead of a &str.
    fn find_target<T: AsTarg>(&self, target: T) -> Result<&'a Package>;
    /// The same as pkg() on Db but will try each Db in order return the first match.
    fn pkg<S: Into<Vec<u8>>>(&self, pkg: S) -> Result<&'a Package>;
}

impl<'a> DbListExt<'a> for AlpmListMut<&'a Db> {
    fn find_target_satisfier<T: AsTarg>(&self, target: T) -> Option<&'a Package> {
        self.list().find_target_satisfier(target)
    }

    fn find_target<T: AsTarg>(&self, target: T) -> Result<&'a Package> {
        self.list().find_target(target)
    }

    fn pkg<S: Into<Vec<u8>>>(&self, pkg: S) -> Result<&'a Package> {
        self.list().pkg(pkg)
    }
}

impl<'a> DbListExt<'a> for AlpmList<'_, &'a Db> {
    fn find_target_satisfier<T: AsTarg>(&self, target: T) -> Option<&'a Package> {
        let target = target.as_targ();

        if let Some(repo) = target.repo {
            if let Some(db) = self.iter().find(|r| r.name() == repo) {
                db.pkgs().find_satisfier(target.pkg)
            } else {
                None
            }
        } else {
            self.find_satisfier(target.pkg)
        }
    }

    fn find_target<T: AsTarg>(&self, target: T) -> Result<&'a Package> {
        let target = target.as_targ();

        if let Some(repo) = target.repo {
            if let Some(db) = self.iter().find(|r| r.name() == repo) {
                db.pkg(target.pkg)
            } else {
                Err(alpm::Error::PkgNotFound)
            }
        } else {
            self.pkg(target.pkg)
        }
    }

    fn pkg<S: Into<Vec<u8>>>(&self, pkg: S) -> Result<&'a Package> {
        let mut pkg = pkg.into();
        pkg.reserve(1);
        let pkg = self.iter().find_map(|db| db.pkg(pkg.clone()).ok());
        pkg.ok_or(alpm::Error::PkgNotFound)
    }
}