Skip to content

Commit

Permalink
get_version_from_cache_path
Browse files Browse the repository at this point in the history
  • Loading branch information
ericswanson-dfinity committed Nov 7, 2024
1 parent 91ea56a commit 3df2131
Show file tree
Hide file tree
Showing 4 changed files with 35 additions and 25 deletions.
19 changes: 17 additions & 2 deletions src/dfx-core/src/config/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
use crate::config::directories::project_dirs;
use crate::error::cache::{
DeleteCacheError, EnsureCacheVersionsDirError, GetBinaryCommandPathError, GetCacheRootError,
IsCacheInstalledError, ListCacheVersionsError,
GetVersionFromCachePathError, IsCacheInstalledError, ListCacheVersionsError,
};
#[cfg(not(windows))]
use crate::foundation::get_user_home;
use crate::fs::composite::ensure_dir_exists;
use semver::Version;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

pub trait Cache {
fn version_str(&self) -> String;
Expand Down Expand Up @@ -50,6 +50,21 @@ pub fn get_cache_path_for_version(v: &str) -> Result<PathBuf, GetCacheRootError>
Ok(p)
}

pub fn get_version_from_cache_path(
cache_path: &Path,
) -> Result<Version, GetVersionFromCachePathError> {
let version = cache_path
.file_name()
.ok_or(GetVersionFromCachePathError::NoCachePathFilename(
cache_path.to_path_buf(),
))?
.to_str()
.ok_or(GetVersionFromCachePathError::CachePathFilenameNotUtf8(
cache_path.to_path_buf(),
))?;
Ok(Version::parse(version)?)
}

/// Return the binary cache root. It constructs it if not present
/// already.
pub fn ensure_cache_versions_dir() -> Result<PathBuf, EnsureCacheVersionsDirError> {
Expand Down
13 changes: 13 additions & 0 deletions src/dfx-core/src/error/cache.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::error::fs::{
};
use crate::error::get_current_exe::GetCurrentExeError;
use crate::error::get_user_home::GetUserHomeError;
use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -34,6 +35,18 @@ pub enum EnsureCacheVersionsDirError {
GetCacheRoot(#[from] GetCacheRootError),
}

#[derive(Error, Debug)]
pub enum GetVersionFromCachePathError {
#[error("no filename in cache path '{0}'")]
NoCachePathFilename(PathBuf),

#[error("filename in cache path '{0}' is not valid UTF-8")]
CachePathFilenameNotUtf8(PathBuf),

#[error("cannot parse version from cache path filename")]
ParseVersion(#[from] semver::Error),
}

#[derive(Error, Debug)]
pub enum GetCacheRootError {
#[error(transparent)]
Expand Down
12 changes: 3 additions & 9 deletions src/dfx-core/src/error/interface.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
use crate::error::cache::GetVersionFromCachePathError;
use crate::error::extension::NewExtensionManagerError;
use std::path::PathBuf;
use thiserror::Error;

#[derive(Error, Debug)]
pub enum NewExtensionManagerFromCachePathError {
#[error("no filename in cache path '{0}'")]
NoCachePathFilename(PathBuf),

#[error("filename in cache path '{0}' is not valid UTF-8")]
CachePathFilenameNotUtf8(PathBuf),

#[error("cannot parse version from cache path filename")]
ParseVersion(#[from] semver::Error),
#[error(transparent)]
GetVersionFromCachePath(#[from] GetVersionFromCachePathError),

#[error(transparent)]
NewExtensionManager(#[from] NewExtensionManagerError),
Expand Down
16 changes: 2 additions & 14 deletions src/dfx-core/src/interface/builder.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use crate::config::cache::get_version_from_cache_path;
use crate::error::extension::NewExtensionManagerError;
use crate::error::interface::NewExtensionManagerFromCachePathError;
use crate::extension::manager::ExtensionManager;
Expand Down Expand Up @@ -100,20 +101,7 @@ impl DfxInterfaceBuilder {
self,
cache_path: &Path,
) -> Result<Self, NewExtensionManagerFromCachePathError> {
let version = {
let version = cache_path
.file_name()
.ok_or(NewExtensionManagerFromCachePathError::NoCachePathFilename(
cache_path.to_path_buf(),
))?
.to_str()
.ok_or(
NewExtensionManagerFromCachePathError::CachePathFilenameNotUtf8(
cache_path.to_path_buf(),
),
)?;
Version::parse(version)?
};
let version = get_version_from_cache_path(cache_path)?;

Ok(self.with_extension_manager(version)?)
}
Expand Down

0 comments on commit 3df2131

Please sign in to comment.