Compare commits
3 commits
5a34a8d791
...
6ba17d57df
| Author | SHA1 | Date | |
|---|---|---|---|
| 6ba17d57df | |||
| 52d455e219 | |||
| 7e4bd398c2 |
5 changed files with 16 additions and 18 deletions
|
|
@ -57,10 +57,7 @@ impl CacheFile {
|
||||||
};
|
};
|
||||||
|
|
||||||
if args.should_hash() {
|
if args.should_hash() {
|
||||||
fn check_hash<'a>(
|
fn check_hash(file: &impl FileTrait, bar: &ProgressBar) -> crate::Result<()> {
|
||||||
file: &'a impl FileTrait<'a>,
|
|
||||||
bar: &ProgressBar,
|
|
||||||
) -> crate::Result<()> {
|
|
||||||
bar.set_message(format!("checking {:?}", file.get_name()));
|
bar.set_message(format!("checking {:?}", file.get_name()));
|
||||||
file.check_hash(|bytes| bar.inc(bytes))
|
file.check_hash(|bytes| bar.inc(bytes))
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -121,15 +121,16 @@ impl Error {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn is_mismatch<E, A>(&self, has_expected: E, has_actual: A) -> bool
|
pub fn is_mismatch<E, A>(&self, want_expected: E, want_actual: A) -> bool
|
||||||
where
|
where
|
||||||
String: PartialEq<E> + PartialEq<A>,
|
E: AsRef<str>,
|
||||||
|
A: AsRef<str>,
|
||||||
{
|
{
|
||||||
matches!(
|
matches!(
|
||||||
self,
|
self,
|
||||||
Self::Mismatch { expected, actual }
|
Self::Mismatch { expected, actual }
|
||||||
if *expected == has_expected
|
if expected == want_expected.as_ref()
|
||||||
&& *actual == has_actual
|
&& actual == want_actual.as_ref()
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -91,8 +91,8 @@ impl Checked {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'t> FileTrait<'t> for Checked {
|
impl FileTrait for Checked {
|
||||||
fn get_name(&'t self) -> &'t str {
|
fn get_name(&self) -> &str {
|
||||||
<Self as FileTrait>::extract_file_name(&self.path)
|
<Self as FileTrait>::extract_file_name(&self.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -104,7 +104,7 @@ impl<'t> FileTrait<'t> for Checked {
|
||||||
super::check_hash(
|
super::check_hash(
|
||||||
&self.path,
|
&self.path,
|
||||||
self.size,
|
self.size,
|
||||||
self.hash.as_ref().map(String::as_str),
|
self.hash.as_deref(),
|
||||||
on_progress,
|
on_progress,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -16,7 +16,7 @@ pub use uploading::Uploading;
|
||||||
const HASH_CHUNK_SIZE: usize = 4 * 1024 * 1024;
|
const HASH_CHUNK_SIZE: usize = 4 * 1024 * 1024;
|
||||||
|
|
||||||
/// compute hash for a file given its path.
|
/// compute hash for a file given its path.
|
||||||
/// Hash function: BLAKE2b, 512 bit
|
/// Hash function: `BLAKE2b`, 512 bit
|
||||||
///
|
///
|
||||||
/// # Params
|
/// # Params
|
||||||
///
|
///
|
||||||
|
|
@ -96,13 +96,13 @@ fn check_hash(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait FileTrait<'t> {
|
pub trait FileTrait {
|
||||||
/// extract the filename part of a `Path` reference
|
/// extract the filename part of a `Path` reference
|
||||||
///
|
///
|
||||||
/// # Panics
|
/// # Panics
|
||||||
///
|
///
|
||||||
/// Expects `path::Path::file_name` and `ffi::OsStr::to_str` to succeed on the given path
|
/// Expects `path::Path::file_name` and `ffi::OsStr::to_str` to succeed on the given path
|
||||||
fn extract_file_name(p: &'t Path) -> &'t str {
|
fn extract_file_name(p: &Path) -> &str {
|
||||||
p.file_name()
|
p.file_name()
|
||||||
.and_then(OsStr::to_str)
|
.and_then(OsStr::to_str)
|
||||||
.expect("bad file name")
|
.expect("bad file name")
|
||||||
|
|
@ -111,7 +111,7 @@ pub trait FileTrait<'t> {
|
||||||
/// get a reference to the file's name
|
/// get a reference to the file's name
|
||||||
///
|
///
|
||||||
/// Uses `file::FileTrait::extract_file_name`, which may **panic**!
|
/// Uses `file::FileTrait::extract_file_name`, which may **panic**!
|
||||||
fn get_name(&'t self) -> &'t str;
|
fn get_name(&self) -> &str;
|
||||||
|
|
||||||
/// get the file's size
|
/// get the file's size
|
||||||
fn get_size(&self) -> u64;
|
fn get_size(&self) -> u64;
|
||||||
|
|
|
||||||
|
|
@ -94,8 +94,8 @@ impl Uploading {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'t> FileTrait<'t> for Uploading {
|
impl FileTrait for Uploading {
|
||||||
fn get_name(&'t self) -> &'t str {
|
fn get_name(&self) -> &str {
|
||||||
<Self as FileTrait>::extract_file_name(&self.path)
|
<Self as FileTrait>::extract_file_name(&self.path)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -107,7 +107,7 @@ impl<'t> FileTrait<'t> for Uploading {
|
||||||
super::check_hash(
|
super::check_hash(
|
||||||
&self.path,
|
&self.path,
|
||||||
self.size,
|
self.size,
|
||||||
self.hash.as_ref().map(String::as_str),
|
self.hash.as_deref(),
|
||||||
on_progress,
|
on_progress,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue