From baa820c60675f84ed0652f0b11feefe9230a97a1 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 8 Apr 2021 20:28:09 +0200 Subject: Remove helper Source::exists() It is easier for the caller (because more visible what happens) to call `Source::path().exists()`. Signed-off-by: Matthias Beyer --- src/commands/source.rs | 8 ++++---- src/source/mod.rs | 4 ---- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/src/commands/source.rs b/src/commands/source.rs index 7f4a1bb..50f6a30 100644 --- a/src/commands/source.rs +++ b/src/commands/source.rs @@ -154,7 +154,7 @@ pub async fn list_missing(_: &ArgMatches, config: &Configuration, repo: Reposito repo.packages().try_for_each(|p| { for source in sc.sources_for(p) { - if !source.exists() { + if !source.path().exists() { writeln!( outlock, "{} {} -> {}", @@ -244,7 +244,7 @@ pub async fn download( let bar = multi.add(progressbars.spinner()); bar.set_message(&format!("Downloading {}", source.url())); async move { - if !source.exists() && source.download_manually() { + if !source.path().exists() && source.download_manually() { return Err(anyhow!( "Cannot download source that is marked for manual download" )) @@ -253,10 +253,10 @@ pub async fn download( .map_err(Error::from); } - if source.exists() && !force { + if source.path().exists() && !force { Err(anyhow!("Source exists: {}", source.path().display())) } else { - if source.exists() { + if source.path().exists() { let _ = source.remove_file().await?; } diff --git a/src/source/mod.rs b/src/source/mod.rs index ec0e2e2..7ce00e5 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -75,10 +75,6 @@ impl SourceEntry { .collect() } - pub fn exists(&self) -> bool { - self.source_file_path().exists() - } - pub fn path(&self) -> PathBuf { self.source_file_path() } -- cgit v1.2.3 From cbdea0872d4e3f12ce08546667daa6015d0c087b Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 8 Apr 2021 20:29:40 +0200 Subject: Optimize: Dont compute PathBuf object three times Signed-off-by: Matthias Beyer --- src/commands/source.rs | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/commands/source.rs b/src/commands/source.rs index 50f6a30..f194134 100644 --- a/src/commands/source.rs +++ b/src/commands/source.rs @@ -244,7 +244,8 @@ pub async fn download( let bar = multi.add(progressbars.spinner()); bar.set_message(&format!("Downloading {}", source.url())); async move { - if !source.path().exists() && source.download_manually() { + let source_path_exists = source.path().exists(); + if !source_path_exists && source.download_manually() { return Err(anyhow!( "Cannot download source that is marked for manual download" )) @@ -253,10 +254,10 @@ pub async fn download( .map_err(Error::from); } - if source.path().exists() && !force { + if source_path_exists && !force { Err(anyhow!("Source exists: {}", source.path().display())) } else { - if source.path().exists() { + if source_path_exists { let _ = source.remove_file().await?; } -- cgit v1.2.3 From 1e6f780bea00c1ef8cfe15350a7613d6aad67832 Mon Sep 17 00:00:00 2001 From: Matthias Beyer Date: Thu, 8 Apr 2021 20:30:32 +0200 Subject: Optimize: Remove helper Because we can just use the actual interface function here. Signed-off-by: Matthias Beyer --- src/source/mod.rs | 20 ++++++++------------ 1 file changed, 8 insertions(+), 12 deletions(-) diff --git a/src/source/mod.rs b/src/source/mod.rs index 7ce00e5..7b84c30 100644 --- a/src/source/mod.rs +++ b/src/source/mod.rs @@ -47,14 +47,6 @@ pub struct SourceEntry { } impl SourceEntry { - fn source_file_path(&self) -> PathBuf { - self.source_file_directory().join(format!( - "{}-{}.source", - self.package_source_name, - self.package_source.hash().value() - )) - } - fn source_file_directory(&self) -> PathBuf { self.cache_root .join(format!("{}-{}", self.package_name, self.package_version)) @@ -76,7 +68,11 @@ impl SourceEntry { } pub fn path(&self) -> PathBuf { - self.source_file_path() + self.source_file_directory().join(format!( + "{}-{}.source", + self.package_source_name, + self.package_source.hash().value() + )) } pub fn url(&self) -> &Url { @@ -88,13 +84,13 @@ impl SourceEntry { } pub async fn remove_file(&self) -> Result<()> { - let p = self.source_file_path(); + let p = self.path(); tokio::fs::remove_file(&p).await?; Ok(()) } pub async fn verify_hash(&self) -> Result<()> { - let p = self.source_file_path(); + let p = self.path(); trace!("Verifying : {}", p.display()); let reader = tokio::fs::OpenOptions::new() @@ -114,7 +110,7 @@ impl SourceEntry { } pub async fn create(&self) -> Result { - let p = self.source_file_path(); + let p = self.path(); trace!("Creating source file: {}", p.display()); if !self.cache_root.is_dir() { -- cgit v1.2.3