hff_core/read/
table_view.rs

1use super::{ChunkIter, Hff, TableIter};
2use crate::{ContentInfo, Identifier};
3use std::fmt::Debug;
4
5/// View of a table.
6#[derive(Copy, Clone)]
7pub struct TableView<'a, T: Debug> {
8    hff: &'a Hff<T>,
9    index: usize,
10}
11
12impl<'a, T: Debug> Debug for TableView<'a, T> {
13    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
14        write!(f, "{:?}", self.hff.tables_array()[self.index])
15    }
16}
17
18impl<'a, T: Debug> ContentInfo for TableView<'a, T> {
19    fn len(&self) -> u64 {
20        self.hff.tables_array()[self.index].metadata_length()
21    }
22    fn offset(&self) -> u64 {
23        self.hff.tables_array()[self.index].metadata_offset()
24    }
25}
26
27impl<'a, T: Debug> TableView<'a, T> {
28    /// Create a new TableView.
29    pub fn new(hff: &'a Hff<T>, index: usize) -> Self {
30        Self { hff, index }
31    }
32
33    /// Get the hff container we're built from.
34    pub fn hff(&self) -> &Hff<T> {
35        self.hff
36    }
37
38    /// Determine if the table has metadata.
39    pub fn has_metadata(&self) -> bool {
40        self.hff.tables_array()[self.index].metadata_length() > 0
41    }
42
43    /// Get the current index into the tables.
44    pub fn index(&self) -> usize {
45        self.index
46    }
47
48    /// Get the identifier.
49    pub fn identifier(&self) -> Identifier {
50        self.hff.tables_array()[self.index].identifier()
51    }
52
53    /// Get the count of child tables.
54    pub fn child_count(&self) -> usize {
55        self.hff.tables_array()[self.index].child_count() as usize
56    }
57
58    /// Get an iterator to the child tables.
59    pub fn iter(&self) -> TableIter<'a, T> {
60        if self.child_count() > 0 {
61            TableIter::new(self.hff, self.index + 1)
62        } else {
63            TableIter::empty(self.hff)
64        }
65    }
66
67    /// Get an iterator of the chunks.
68    pub fn chunks(&self) -> ChunkIter<'a, T> {
69        let table = &self.hff.tables_array()[self.index];
70        ChunkIter::new(
71            self.hff,
72            table.chunk_index() as usize,
73            table.chunk_count() as usize,
74        )
75    }
76
77    /// Get the count of chunks in the table.
78    pub fn chunk_count(&self) -> usize {
79        self.hff.tables_array()[self.index].chunk_count() as usize
80    }
81}