Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 20 additions & 16 deletions crates/lib/src/cellar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,19 @@ use tokio::fs;
pub async fn ls(project: &str, config: &Config) -> Result<Vec<Installation>, Box<dyn Error>> {
let d = config.pkgx_dir.join(project);

if !fs::metadata(&d).await?.is_dir() {
return Ok(vec![]);
match fs::metadata(&d).await {
Ok(metadata) => {
if !metadata.is_dir() {
return Err(format!("err: expected directory: {:?}", d).into());
}
}
Err(e) => {
if e.kind() == std::io::ErrorKind::NotFound {
return Ok(vec![]);
} else {
return Err(e.into());
}
}
}

let mut rv = vec![];
Expand Down Expand Up @@ -39,23 +50,16 @@ pub async fn ls(project: &str, config: &Config) -> Result<Vec<Installation>, Box
Ok(rv)
}

pub async fn resolve(pkgreq: &PackageReq, config: &Config) -> Result<Installation, Box<dyn Error>> {
let installations = ls(&pkgreq.project, config).await?;

if let Some(i) = installations
pub async fn resolve(
pkgreq: &PackageReq,
config: &Config,
) -> Result<Option<Installation>, Box<dyn Error>> {
Ok(ls(&pkgreq.project, config)
.await?
.iter()
.filter(|i| pkgreq.constraint.satisfies(&i.pkg.version))
.max_by_key(|i| i.pkg.version.clone())
{
Ok(i.clone())
} else {
// If no matching version is found, return an error
Err(format!("couldn’t resolve {:?}", pkgreq).into())
}
}

pub async fn has(pkg: &PackageReq, config: &Config) -> Option<Installation> {
resolve(pkg, config).await.ok()
.cloned())
}

pub fn dst(pkg: &Package, config: &Config) -> PathBuf {
Expand Down
4 changes: 2 additions & 2 deletions crates/lib/src/resolve.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ pub async fn resolve(reqs: Vec<PackageReq>, config: &Config) -> Result<Resolutio

for req in reqs {
futures.push(async move {
if let Some(installation) = cellar::has(&req, config).await {
if let Some(installation) = cellar::resolve(&req, config).await? {
Ok::<_, Box<dyn Error>>((
Some((installation.clone(), installation.pkg.clone())),
None,
))
} else if let Ok(Some(version)) = inventory::select(&req, config).await {
} else if let Some(version) = inventory::select(&req, config).await? {
let pkg = Package {
project: req.project.clone(),
version,
Expand Down