From 7f959a708855b12fec3782654832b446c8141f49 Mon Sep 17 00:00:00 2001 From: Scott Haug Date: Mon, 27 Feb 2023 11:08:33 -0800 Subject: [PATCH] misc: dedupe --- src/link.rs | 27 +++++++++++---------------- 1 file changed, 11 insertions(+), 16 deletions(-) diff --git a/src/link.rs b/src/link.rs index cca94ad..8bb0275 100644 --- a/src/link.rs +++ b/src/link.rs @@ -241,6 +241,13 @@ impl AsyncRead for Linker { } } +fn filesize(target: &Path) -> Result { + Ok(target + .metadata() + .with_context(|| format!("Failed to get metadata of {}", target.display()))? + .len() as usize) +} + impl Linker { /// Creates a new asynchronous readable file handle into the cache. pub async fn open(cache: P, key: K, target: T) -> Result @@ -250,10 +257,7 @@ impl Linker { T: AsRef, { async fn inner(cache: &Path, key: &str, target: &Path) -> Result { - let size = target - .metadata() - .with_context(|| format!("Failed to get metadata of {}", target.display()))? - .len() as usize; + let size = filesize(&target)?; WriteOpts::new() .algorithm(Algorithm::Sha256) .size(size) @@ -270,10 +274,7 @@ impl Linker { T: AsRef, { async fn inner(cache: &Path, target: &Path) -> Result { - let size = target - .metadata() - .with_context(|| format!("Failed to get metadata of {}", target.display()))? - .len() as usize; + let size = filesize(&target)?; WriteOpts::new() .algorithm(Algorithm::Sha256) .size(size) @@ -380,10 +381,7 @@ impl SyncLinker { T: AsRef, { fn inner(cache: &Path, key: &str, target: &Path) -> Result { - let size = target - .metadata() - .with_context(|| format!("Failed to get metadata of {}", target.display()))? - .len() as usize; + let size = filesize(&target)?; WriteOpts::new() .algorithm(Algorithm::Sha256) .size(size) @@ -417,10 +415,7 @@ impl SyncLinker { T: AsRef, { fn inner(cache: &Path, target: &Path) -> Result { - let size = target - .metadata() - .with_context(|| format!("Failed to get metadata of {}", target.display()))? - .len() as usize; + let size = filesize(&target)?; WriteOpts::new() .algorithm(Algorithm::Sha256) .size(size)