harmos_domain/model/storage/files/
read.rs1use crate::{Error, MemorySize, Result};
22
23crate::model::storage::codec::product!(ByteRange {
28 offset: crate::model::storage::codec::Unsigned => 1,
29 limit: crate::model::storage::codec::Optional<crate::model::storage::codec::Unsigned> => 2,
30});
31crate::model::storage::codec::product!(FileBatch {
32 offset: crate::model::storage::codec::Unsigned => 1,
33 bytes: crate::model::storage::codec::Bytes => 2,
34});
35crate::model::storage::codec::product!(FileCompletion {
36 bytes: crate::model::storage::codec::Unsigned => 1,
37});
38
39#[derive(Clone, Copy, Debug, PartialEq, Eq)]
44pub struct ByteRange {
45 pub offset: u64,
46 pub limit: Option<u64>,
47}
48#[derive(Clone, Debug, PartialEq, Eq)]
49pub struct FileBatch {
50 pub offset: u64,
51 pub bytes: Vec<u8>,
52}
53#[derive(Clone, Copy, Debug, PartialEq, Eq)]
54pub struct FileCompletion {
55 pub bytes: u64,
56}
57
58impl ByteRange {
65 pub fn end(self, length: u64) -> Result<u64> {
66 let remaining = length
67 .checked_sub(self.offset)
68 .ok_or(Error::InvalidInput { field: "file byte range", reason: "offset exceeds the immutable file length" })?;
69 self.offset
70 .checked_add(self.limit.unwrap_or(remaining).min(remaining))
71 .ok_or(Error::InvalidInput { field: "file byte range", reason: "byte range overflow" })
72 }
73}
74impl MemorySize for ByteRange {
75 fn memory_size(&self) -> usize {
76 std::mem::size_of::<Self>()
77 }
78}
79impl MemorySize for FileBatch {
80 fn memory_size(&self) -> usize {
81 std::mem::size_of::<Self>().saturating_add(self.bytes.capacity())
82 }
83}
84impl MemorySize for FileCompletion {
85 fn memory_size(&self) -> usize {
86 std::mem::size_of::<Self>()
87 }
88}
89
90