pub fn best(
        &mut self,
        pkg: &PackageId,
        con: &Constraint,
        minimize: bool,
    ) -> Result<Version, Error> {
        // With stuff from lockfiles, we try to retrieve whatever version was specified in the
        // lockfile. However, if it fails, we don't want to error out; we want to try to find
        // the best version we can otherwise.
        let pkg_version = self
            .lockfile
            .find_by(|sum| sum.id == *pkg)
            .map(|meta| &meta.version);
        if let Some(v) = pkg_version {
            if con.satisfies(&v) {
                let dir = if let Resolution::Direct(loc) = pkg.resolution() {
                    Some(loc)
                } else {
                    self.indices
                        .select(&Summary::new(pkg.clone(), v.clone()))
                        .map(|e| &e.location)
                        .ok()
                };

                if let Some(dir) = dir {
                    let dir = dir.clone();
                    if let Ok(src) = self.direct_checkout(pkg, &dir) {
                        return Ok(src.meta().version().clone());
                    }
                }
            }
        }

        if let Resolution::Direct(loc) = pkg.resolution() {
            return Ok(self.direct_checkout(pkg, loc)?.meta().version().clone());
        }

        let (mut pre, mut not_pre): (Vec<Version>, Vec<Version>) = self
            .indices
            .entries(pkg)?
            .clone()
            .into_iter()
            .map(|v| v.0)
            .filter(|v| con.satisfies(v))
            .partition(|v| v.is_prerelease());

        if !not_pre.is_empty() {
            if !minimize {
                Ok(not_pre.pop().unwrap())
            } else {
                Ok(not_pre.remove(0))
            }
        } else if !pre.is_empty() {
            if !minimize {
                Ok(pre.pop().unwrap())
            } else {
                Ok(pre.remove(0))
            }
        } else {
            Err(Error::from(ErrorKind::PackageNotFound))
        }
    }
/// Chooses the best version of a package given a constraint.
Chooses the best version of a package given a constraint.
Chooses the best version of a package given a constraint .
pub fn best(
        &mut self,
        pkg: &PackageId,
        con: &Constraint,
        minimize: bool,
    ) -> Result<Version, Error> {
        // With stuff from lockfiles, we try to retrieve whatever version was specified in the
        // lockfile. However, if it fails, we don't want to error out; we want to try to find
        // the best version we can otherwise.
        let pkg_version = self
            .lockfile
            .find_by(|sum| sum.id == *pkg)
            .map(|meta| &meta.version);
        if let Some(v) = pkg_version {
            if con.satisfies(&v) {
                let dir = if let Resolution::Direct(loc) = pkg.resolution() {
                    Some(loc)
                } else {
                    self.indices
                        .select(&Summary::new(pkg.clone(), v.clone()))
                        .map(|e| &e.location)
                        .ok()
                };
                if let Some(dir) = dir {
                    let dir = dir.clone();
                    if let Ok(src) = self.direct_checkout(pkg, &dir) {
                        return Ok(src.meta().version().clone());
                    }
                }
            }
        }
        if let Resolution::Direct(loc) = pkg.resolution() {
            return Ok(self.direct_checkout(pkg, loc)?.meta().version().clone());
        }
        let (mut pre, mut not_pre): (Vec<Version>, Vec<Version>) = self
            .indices
            .entries(pkg)?
            .clone()
            .into_iter()
            .map(|v| v.0)
            .filter(|v| con.satisfies(v))
            .partition(|v| v.is_prerelease());
        if !not_pre.is_empty() {
            if !minimize {
                Ok(not_pre.pop().unwrap())
            } else {
                Ok(not_pre.remove(0))
            }
        } else if !pre.is_empty() {
            if !minimize {
                Ok(pre.pop().unwrap())
            } else {
                Ok(pre.remove(0))
            }
        } else {
            Err(Error::from(ErrorKind::PackageNotFound))
        }
    }
pub fn best ( & mut self , pkg : & PackageId , con : & Constraint , minimize : bool , ) -> Result < Version , Error > { // With stuff from lockfiles, we try to retrieve whatever version was specified in the // lockfile. However, if it fails, we don't want to error out; we want to try to find // the best version we can otherwise. let pkg_version = self . lockfile . find_by ( | sum | sum . id == * pkg ) . map ( | meta | & meta . version ) ; if let Some ( v ) = pkg_version { if con . satisfies ( & v ) { let dir = if let Resolution :: Direct ( loc ) = pkg . resolution ( ) { Some ( loc ) } else { self . indices . select ( & Summary :: new ( pkg . clone ( ) , v . clone ( ) ) ) . map ( | e | & e . location ) . ok ( ) } ; if let Some ( dir ) = dir { let dir = dir . clone ( ) ; if let Ok ( src ) = self . direct_checkout ( pkg , & dir ) { return Ok ( src . meta ( ) . version ( ) . clone ( ) ) ; } } } } if let Resolution :: Direct ( loc ) = pkg . resolution ( ) { return Ok ( self . direct_checkout ( pkg , loc ) ? . meta ( ) . version ( ) . clone ( ) ) ; } let ( mut pre , mut not_pre ) : ( Vec < Version > , Vec < Version > ) = self . indices . entries ( pkg ) ? . clone ( ) . into_iter ( ) . map ( | v | v . 0 ) . filter ( | v | con . satisfies ( v ) ) . partition ( | v | v . is_prerelease ( ) ) ; if ! not_pre . is_empty ( ) { if ! minimize { Ok ( not_pre . pop ( ) . unwrap ( ) ) } else { Ok ( not_pre . remove ( 0 ) ) } } else if ! pre . is_empty ( ) { if ! minimize { Ok ( pre . pop ( ) . unwrap ( ) ) } else { Ok ( pre . remove ( 0 ) ) } } else { Err ( Error :: from ( ErrorKind :: PackageNotFound ) ) } }
Chooses the best version of a package given a constraint.
Chooses the best version of a package given a constraint .
&mut self
pkg
&PackageId
con
&Constraint
minimize
bool