Compare commits

...

3 commits

5 changed files with 16 additions and 18 deletions

View file

@ -57,10 +57,7 @@ impl CacheFile {
};
if args.should_hash() {
fn check_hash<'a>(
file: &'a impl FileTrait<'a>,
bar: &ProgressBar,
) -> crate::Result<()> {
fn check_hash(file: &impl FileTrait, bar: &ProgressBar) -> crate::Result<()> {
bar.set_message(format!("checking {:?}", file.get_name()));
file.check_hash(|bytes| bar.inc(bytes))
}

View file

@ -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
String: PartialEq<E> + PartialEq<A>,
E: AsRef<str>,
A: AsRef<str>,
{
matches!(
self,
Self::Mismatch { expected, actual }
if *expected == has_expected
&& *actual == has_actual
if expected == want_expected.as_ref()
&& actual == want_actual.as_ref()
)
}

View file

@ -91,8 +91,8 @@ impl Checked {
}
}
impl<'t> FileTrait<'t> for Checked {
fn get_name(&'t self) -> &'t str {
impl FileTrait for Checked {
fn get_name(&self) -> &str {
<Self as FileTrait>::extract_file_name(&self.path)
}
@ -104,7 +104,7 @@ impl<'t> FileTrait<'t> for Checked {
super::check_hash(
&self.path,
self.size,
self.hash.as_ref().map(String::as_str),
self.hash.as_deref(),
on_progress,
)
}

View file

@ -16,7 +16,7 @@ pub use uploading::Uploading;
const HASH_CHUNK_SIZE: usize = 4 * 1024 * 1024;
/// compute hash for a file given its path.
/// Hash function: BLAKE2b, 512 bit
/// Hash function: `BLAKE2b`, 512 bit
///
/// # Params
///
@ -96,13 +96,13 @@ fn check_hash(
}
}
pub trait FileTrait<'t> {
pub trait FileTrait {
/// extract the filename part of a `Path` reference
///
/// # Panics
///
/// 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()
.and_then(OsStr::to_str)
.expect("bad file name")
@ -111,7 +111,7 @@ pub trait FileTrait<'t> {
/// get a reference to the file's name
///
/// 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
fn get_size(&self) -> u64;

View file

@ -94,8 +94,8 @@ impl Uploading {
}
}
impl<'t> FileTrait<'t> for Uploading {
fn get_name(&'t self) -> &'t str {
impl FileTrait for Uploading {
fn get_name(&self) -> &str {
<Self as FileTrait>::extract_file_name(&self.path)
}
@ -107,7 +107,7 @@ impl<'t> FileTrait<'t> for Uploading {
super::check_hash(
&self.path,
self.size,
self.hash.as_ref().map(String::as_str),
self.hash.as_deref(),
on_progress,
)
}