Compare commits

...

2 commits

Author SHA1 Message Date
72cfe04af1 doc for file::Chunk 2025-07-06 00:02:10 +00:00
f573b61ad1 crate::Error::mismatch refactor 2025-07-06 00:01:46 +00:00
6 changed files with 20 additions and 24 deletions

View file

@ -62,6 +62,8 @@ 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
@ -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<T>(expected: impl ToString, actual: impl ToString) -> Result<T> {
Err(Self::Mismatch {
expected: into_string(expected),
actual: into_string(actual),
}
})
}
pub fn is_mismatch<E, A>(&self, want_expected: E, want_actual: A) -> bool
@ -152,5 +154,3 @@ 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 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)?);

View file

@ -27,19 +27,23 @@ 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
}
pub(super) fn get_length(&self) -> u64 {
/// get the chunk's length
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 +51,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 +80,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);
}
}
}

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 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)
}
}

View file

@ -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::<u64>()
.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)
}
}
}

View file

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