Skip to main content

harmos_domain/model/storage/files/
read.rs

1/* ***
2 *
3 * Harmos
4 *
5 * SPDX-License-Identifier: Apache-2.0
6 *
7 * ***
8 */
9
10// Finished File Read Contract
11
12/* === Modules === */
13
14/* === Re-Exports === */
15
16/* === Imports === */
17
18// Standard Library Imports
19// Third Party Imports
20// Local Imports
21use crate::{Error, MemorySize, Result};
22
23/* === Constants === */
24
25/* === Macros === */
26
27crate::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/* === Type Aliases === */
40
41/* === Structs === */
42
43#[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
58/* === Enums === */
59
60/* === Traits === */
61
62/* === Implementations === */
63
64impl 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/* === Public Functions === */
91
92/* === Private Functions === */
93
94/* === Tests === */