1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
use super::Hff;
use crate::{ContentInfo, Identifier};
use std::fmt::Debug;

/// A view to a chunk.
#[derive(Copy, Clone)]
pub struct ChunkView<'a, T: Debug> {
    hff: &'a Hff<T>,
    index: usize,
}

impl<'a, T: Debug> Debug for ChunkView<'a, T> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(f, "{:?}", self.hff.chunks_array()[self.index])
    }
}

impl<'a, T: Debug> ChunkView<'a, T> {
    /// Create a new view.
    pub fn new(hff: &'a Hff<T>, index: usize) -> Self {
        Self { hff, index }
    }

    /// Get the hff this was built from.
    pub fn hff(&self) -> &Hff<T> {
        self.hff
    }

    /// Get the current index.
    pub fn index(&self) -> usize {
        self.index
    }

    /// Get the identifier.
    pub fn identifier(&self) -> Identifier {
        self.hff.chunks_array()[self.index].identifier()
    }

    /// Get the size of the data in the chunk.
    pub fn size(&self) -> usize {
        self.hff.chunks_array()[self.index].length() as usize
    }
}

impl<'a, T: Debug> ContentInfo for ChunkView<'a, T> {
    fn len(&self) -> u64 {
        self.hff.chunks_array()[self.index].length()
    }

    fn offset(&self) -> u64 {
        self.hff.chunks_array()[self.index].offset()
    }
}