Skip to main content

lux_lib/package/db/
search.rs

1use miette::Diagnostic;
2use thiserror::Error;
3
4use super::PackageDB;
5use crate::manifest;
6use crate::operations::install;
7use crate::package;
8use crate::pipeline::discover::{self, FoundPackage};
9use crate::remote;
10
11#[derive(Error, Debug, Diagnostic)]
12#[non_exhaustive]
13pub enum Error {
14    #[error("no rock that matches '{0}' found in the lockfile or in the manifests")]
15    RockNotFound(package::Req),
16    #[error("error when pulling manifest")]
17    #[diagnostic(forward(0))]
18    Manifest(#[from] manifest::Error),
19}
20
21impl PackageDB {
22    /// Find a remote package that matches the requirement, returning the latest match.
23    #[tracing::instrument(
24          name = "Searching",
25          level = "info",
26          skip_all,
27          fields(package = package_req.to_string()),
28    )]
29    pub(crate) async fn find(
30        &self,
31        package_req: &package::Req,
32        filter: Option<remote::package::kind::FilterSpec>,
33    ) -> Result<remote::Package, Error> {
34        for provider in &self.providers {
35            if let Some(package) = provider.find(package_req, filter.clone()).await? {
36                return Ok(package);
37            }
38        }
39
40        Err(Error::RockNotFound(package_req.clone()))
41    }
42
43    /// Resolves a package install spec to a package that is already available.
44    pub(crate) async fn resolve(
45        &self,
46        spec: &install::Spec,
47    ) -> Result<Option<FoundPackage>, discover::Error> {
48        for provider in &self.providers {
49            // Providers that can serve a package without downloading it (local packages)
50            // resolve early.
51            if let Some(found) = provider.resolve(spec).await? {
52                return Ok(Some(found));
53            }
54
55            // Otherwise break on the first provider that has the package. The `break` prevents a
56            // further invocation of `provider.resolve` on a provider we don't want.
57            if provider.find(&spec.package, None).await?.is_some() {
58                break;
59            }
60        }
61        Ok(None)
62    }
63
64    /// Search for all packages that match the requirement.
65    pub async fn search(
66        &self,
67        package_req: &package::Req,
68    ) -> Result<Vec<(package::Name, Vec<package::Version>)>, Error> {
69        let mut results = Vec::new();
70        for provider in &self.providers {
71            results.extend(provider.search(package_req).await?);
72        }
73        Ok(results)
74    }
75
76    /// Find the latest version for a package by name.
77    pub(crate) async fn latest_version(
78        &self,
79        rock_name: &package::Name,
80    ) -> Option<package::Version> {
81        self.latest_match(&rock_name.clone().into(), None)
82            .await
83            .map(|result| result.version().clone())
84    }
85
86    /// Find the latest package that matches the requirement.
87    pub async fn latest_match(
88        &self,
89        package_req: &package::Req,
90        filter: Option<remote::package::kind::FilterSpec>,
91    ) -> Option<package::Spec> {
92        match self.find(package_req, filter).await {
93            Ok(result) => Some(result.spec),
94            Err(_) => None,
95        }
96    }
97}