Compare commits

..

No commits in common. "72cfe04af11ffea353993256ea4572f9662b7dc1" and "4ad9e28bc771706028bf7834eeb8e7ceb57d57a3" have entirely different histories.

6 changed files with 24 additions and 20 deletions

View file

@ -62,8 +62,6 @@ pub enum Error {
Unknown(String),
}
pub type Result<T> = std::result::Result<T, Error>;
// a helper to generate all the `From<T> for Error` impls
macro_rules! impl_error_from {
// $typ: the source type
@ -116,11 +114,11 @@ impl Error {
Self::Response(into_string(e))
}
pub fn mismatch<T>(expected: impl ToString, actual: impl ToString) -> Result<T> {
Err(Self::Mismatch {
pub fn mismatch(expected: impl ToString, actual: impl ToString) -> Self {
Self::Mismatch {
expected: into_string(expected),
actual: into_string(actual),
})
}
}
pub fn is_mismatch<E, A>(&self, want_expected: E, want_actual: A) -> bool
@ -154,3 +152,5 @@ impl Error {
}
}
}
pub type Result<T> = std::result::Result<T, Error>;

View file

@ -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 crate::Error::mismatch("unhashed file", self.path.display());
return Err(crate::Error::mismatch("unhashed file", self.path.display()));
}
self.hash = Some(super::compute_hash(&self.path, self.size, on_progress)?);

View file

@ -27,23 +27,19 @@ impl<'t> Chunk<'t> {
}
}
/// get a reference to the associated file_id
pub fn get_file_id(&self) -> &sharry::FileID {
&self.file_id
}
/// get the chunk's offset
pub fn get_offset(&self) -> u64 {
self.offset
}
/// get a reference to the associated data
pub fn get_data(&self) -> &[u8] {
self.data
}
/// get the chunk's length
pub fn get_length(&self) -> u64 {
pub(super) fn get_length(&self) -> u64 {
let len = self.data.len();
// BOOKMARK this might **panic** on platforms where `usize` has more than 64 bit.
@ -51,6 +47,10 @@ 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,6 +80,7 @@ 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);
}
}
}

View file

@ -51,7 +51,7 @@ fn compute_hash(path: &Path, size: u64, mut on_progress: impl FnMut(u64)) -> cra
}
if bytes_read != size {
return crate::Error::mismatch(size, bytes_read);
return Err(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 crate::Error::mismatch("hash", path.display());
return Err(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());
crate::Error::mismatch(expected, actual)
Err(crate::Error::mismatch(expected, actual))
}
}

View file

@ -76,7 +76,7 @@ impl sharry::Client for ureq::Agent {
Ok(res.id.into())
} else {
crate::Error::mismatch("success/\"Share created.\"", format_args!("{res:?}"))
Err(crate::Error::response(format!("{res:?}")))
}
}
@ -168,12 +168,15 @@ impl sharry::Client for ureq::Agent {
.map_err(crate::Error::response)?
.parse::<u64>()
.map_err(crate::Error::response)?;
let next_offset = chunk.get_offset() + chunk.get_length();
if res_offset == next_offset {
if chunk.get_behind() == res_offset {
Ok(())
} else {
crate::Error::mismatch(format_args!("Upload-Offset {next_offset:?}"), res_offset)
Err(crate::Error::response(format!(
"Unexpected Upload-Offset: {} (expected {})",
res_offset,
chunk.get_behind()
)))
}
}
}

View file

@ -103,10 +103,10 @@ impl TryFrom<String> for FileID {
Ok(result)
} else {
crate::Error::mismatch(
Err(crate::Error::mismatch(
"<proto>://<host>/api/v2/alias/upload/<share>/files/tus/<file>",
value,
)
))
}
}
}