shrupl/src/file/chunk.rs
Jörn-Michael Miehe bf38aab409 major refactoring
- `sharry::Client` fn signatures
- `sharry::ClientError` rework
- `file::Uploading` saves `file_id` instead of `patch_uri`
- `impl sharry::Client for ureq::Agent` into separate file
2025-06-18 13:14:45 +00:00

52 lines
1.2 KiB
Rust

use std::fmt;
pub struct Chunk<'t> {
file_id: String,
offset: u64,
data: &'t [u8],
}
impl fmt::Debug for Chunk<'_> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("Chunk")
.field("file_id", &self.file_id)
.field("offset", &self.offset)
.field("data.len()", &self.data.len())
.finish_non_exhaustive()
}
}
impl<'t> Chunk<'t> {
pub fn new(file_id: String, offset: u64, data: &'t [u8]) -> Self {
Self {
file_id,
offset,
data,
}
}
pub fn get_file_id(&self) -> &str {
&self.file_id
}
pub fn get_offset(&self) -> u64 {
self.offset
}
pub fn get_data(&self) -> &[u8] {
self.data
}
pub fn get_length(&self) -> u64 {
let len = self.data.len();
// BOOKMARK this might **panic** on platforms where `usize` has more than 64 bit.
// Also, you've allocated more than 2 EiB ... in ONE chunk.
// 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()
}
}