Skip to content

Commit

Permalink
Move MD5 code into util
Browse files Browse the repository at this point in the history
  • Loading branch information
rcook committed Apr 11, 2023
1 parent b098e4b commit 7e0586a
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 17 deletions.
21 changes: 9 additions & 12 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,9 @@ use crate::serialization::{
RepositoriesRecord, RepositoryRecord, UseRecord,
};
use crate::util::{
dir_url, find_project_config_path, osstr_to_str, path_to_str, read_json_file, read_yaml_file,
safe_write_file, RELEASES_URL,
dir_url, find_project_config_path, osstr_to_str, read_json_file, read_yaml_file,
safe_write_file, HexDigest, RELEASES_URL,
};
use md5::compute;
use std::fs::read_dir;
use std::path::{Path, PathBuf};

Expand All @@ -25,9 +24,9 @@ pub struct App {
pub cwd: PathBuf,
pub dir: PathBuf,
pub assets_dir: PathBuf,
pub named_environments_dir: PathBuf,
pub project_environments_dir: PathBuf,
pub uses_dir: PathBuf,
named_environments_dir: PathBuf,
project_environments_dir: PathBuf,
uses_dir: PathBuf,
}

impl App {
Expand Down Expand Up @@ -191,9 +190,8 @@ impl App {
where
P: AsRef<Path>,
{
let digest = compute(path_to_str(config_path.as_ref())?);
let hex_digest = format!("{:x}", digest);
Ok(self.project_environments_dir.join(hex_digest))
let hex_digest = HexDigest::from_path(config_path)?;
Ok(self.project_environments_dir.join(hex_digest.as_str()))
}

pub fn read_project_environment<S>(
Expand Down Expand Up @@ -240,9 +238,8 @@ impl App {
where
P: AsRef<Path>,
{
let digest = compute(path_to_str(dir.as_ref())?);
let hex_digest = format!("{:x}", digest);
Ok(self.uses_dir.join(hex_digest))
let hex_digest = HexDigest::from_path(dir)?;
Ok(self.uses_dir.join(hex_digest.as_str()))
}

pub fn read_use<S>(&self, hex_digest: S) -> Result<Option<UseRecord>>
Expand Down
7 changes: 2 additions & 5 deletions src/commands/use_.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,10 @@ use crate::app::App;
use crate::object_model::{Environment, EnvironmentName};
use crate::result::{user, Result};
use crate::serialization::UseRecord;
use crate::util::{path_to_str, safe_write_file};
use md5::compute;
use crate::util::safe_write_file;

pub fn do_use(app: &App, environment_name: &EnvironmentName) -> Result<()> {
let hex_digest = format!("{:x}", compute(path_to_str(&app.cwd)?));

let use_yaml_path = app.uses_dir.join(hex_digest).join("use.yaml");
let use_yaml_path = app.use_dir(&app.cwd)?.join("use.yaml");
if use_yaml_path.is_file() {
return Err(user(format!(
"Use is already configured for directory {}",
Expand Down
21 changes: 21 additions & 0 deletions src/util/hash.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use crate::result::Result;
use crate::util::path_to_str;
use md5::compute;
use std::path::Path;

pub struct HexDigest(String);

impl HexDigest {
pub fn from_path<P>(config_path: P) -> Result<Self>
where
P: AsRef<Path>,
{
let digest = compute(path_to_str(config_path.as_ref())?);
let hex_digest = format!("{:x}", digest);
Ok(Self(hex_digest))
}

pub fn as_str(&self) -> &str {
&self.0
}
}
2 changes: 2 additions & 0 deletions src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
mod constants;
mod download_file;
mod fs;
mod hash;
mod helpers;
mod indicator;
mod probe;
Expand All @@ -14,6 +15,7 @@ mod url;
pub use self::constants::{GENERAL_ERROR, OK, RELEASES_URL, USAGE};
pub use self::download_file::download_stream;
pub use self::fs::{open_file, read_json_file, read_yaml_file, safe_create_file, safe_write_file};
pub use self::hash::HexDigest;
pub use self::helpers::{download_asset, get_asset};
pub use self::indicator::Indicator;
pub use self::probe::{
Expand Down

0 comments on commit 7e0586a

Please sign in to comment.