diff --git a/src/error.rs b/src/error.rs index fadb562..9199f38 100644 --- a/src/error.rs +++ b/src/error.rs @@ -62,6 +62,8 @@ pub enum Error { Unknown(String), } +pub type Result = std::result::Result; + // a helper to generate all the `From for Error` impls macro_rules! impl_error_from { // $typ: the source type @@ -114,11 +116,11 @@ impl Error { Self::Response(into_string(e)) } - pub fn mismatch(expected: impl ToString, actual: impl ToString) -> Self { - Self::Mismatch { + pub fn mismatch(expected: impl ToString, actual: impl ToString) -> Result { + Err(Self::Mismatch { expected: into_string(expected), actual: into_string(actual), - } + }) } pub fn is_mismatch(&self, want_expected: E, want_actual: A) -> bool @@ -152,5 +154,3 @@ impl Error { } } } - -pub type Result = std::result::Result; diff --git a/src/file/checked.rs b/src/file/checked.rs index 2adf26e..a4d9c8e 100644 --- a/src/file/checked.rs +++ b/src/file/checked.rs @@ -68,7 +68,7 @@ impl Checked { /// TODO this could use an error variant like `IllegalInvocation` pub fn hash(&mut self, on_progress: impl FnMut(u64)) -> crate::Result<()> { if self.hash.is_some() { - return Err(crate::Error::mismatch("unhashed file", self.path.display())); + return crate::Error::mismatch("unhashed file", self.path.display()); } self.hash = Some(super::compute_hash(&self.path, self.size, on_progress)?); diff --git a/src/file/chunk.rs b/src/file/chunk.rs index a6f0b05..129c331 100644 --- a/src/file/chunk.rs +++ b/src/file/chunk.rs @@ -39,7 +39,7 @@ impl<'t> Chunk<'t> { self.data } - pub(super) fn get_length(&self) -> u64 { + pub fn get_length(&self) -> u64 { let len = self.data.len(); // BOOKMARK this might **panic** on platforms where `usize` has more than 64 bit. @@ -47,10 +47,6 @@ impl<'t> Chunk<'t> { // Whoa! Maybe just chill? u64::try_from(len).unwrap_or_else(|e| panic!("usize={len} did not fit into u64: {e}")) } - - pub fn get_behind(&self) -> u64 { - self.offset + self.get_length() - } } #[cfg(test)] @@ -80,7 +76,6 @@ mod tests { assert_eq!(chunk.get_offset(), mock_offset); assert_eq!(chunk.get_data(), data); assert_eq!(chunk.get_length(), len); - assert_eq!(chunk.get_behind(), mock_offset + len); } } } diff --git a/src/file/mod.rs b/src/file/mod.rs index 246df07..8420d48 100644 --- a/src/file/mod.rs +++ b/src/file/mod.rs @@ -51,7 +51,7 @@ fn compute_hash(path: &Path, size: u64, mut on_progress: impl FnMut(u64)) -> cra } if bytes_read != size { - return Err(crate::Error::mismatch(size, bytes_read)); + return crate::Error::mismatch(size, bytes_read); } let result = BASE64.encode(hasher.finalize()); @@ -79,7 +79,7 @@ fn check_hash( ) -> crate::Result<()> { // check if hash is None let Some(expected) = hash else { - return Err(crate::Error::mismatch("hash", path.display())); + return crate::Error::mismatch("hash", path.display()); }; // compute and check new hash @@ -90,7 +90,7 @@ fn check_hash( Ok(()) } else { warn!("hash mismatch for file {:?}", path.display()); - Err(crate::Error::mismatch(expected, actual)) + crate::Error::mismatch(expected, actual) } } diff --git a/src/impl_ureq.rs b/src/impl_ureq.rs index c4bacee..ed12cf2 100644 --- a/src/impl_ureq.rs +++ b/src/impl_ureq.rs @@ -76,7 +76,7 @@ impl sharry::Client for ureq::Agent { Ok(res.id.into()) } else { - Err(crate::Error::response(format!("{res:?}"))) + crate::Error::mismatch("success/\"Share created.\"", format_args!("{res:?}")) } } @@ -168,15 +168,12 @@ impl sharry::Client for ureq::Agent { .map_err(crate::Error::response)? .parse::() .map_err(crate::Error::response)?; + let next_offset = chunk.get_offset() + chunk.get_length(); - if chunk.get_behind() == res_offset { + if res_offset == next_offset { Ok(()) } else { - Err(crate::Error::response(format!( - "Unexpected Upload-Offset: {} (expected {})", - res_offset, - chunk.get_behind() - ))) + crate::Error::mismatch(format_args!("Upload-Offset {next_offset:?}"), res_offset) } } } diff --git a/src/sharry/ids.rs b/src/sharry/ids.rs index 24412e0..62e054f 100644 --- a/src/sharry/ids.rs +++ b/src/sharry/ids.rs @@ -103,10 +103,10 @@ impl TryFrom for FileID { Ok(result) } else { - Err(crate::Error::mismatch( + crate::Error::mismatch( ":///api/v2/alias/upload//files/tus/", value, - )) + ) } } }