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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
use crate::{ChunkIter, Hff, ReadSeek, TableIter};
use hff_core::{Ecc, Result};
use std::{fmt::Debug, io::SeekFrom};

/// View of a table.
pub struct TableView<'a> {
    hff: &'a Hff,
    index: usize,
}

impl<'a> PartialEq for TableView<'a> {
    fn eq(&self, other: &Self) -> bool {
        self.hff == other.hff && self.index == other.index
    }
}

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

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

    /// Get the primary identifier.
    pub fn primary(&self) -> Ecc {
        self.hff.tables_array()[self.index].primary()
    }

    /// Get the secondary identifier.
    pub fn secondary(&self) -> Ecc {
        self.hff.tables_array()[self.index].secondary()
    }

    /// Get the count of child tables.
    pub fn child_count(&self) -> usize {
        self.hff.tables_array()[self.index].child_count() as usize
    }

    /// Get an iterator to the child tables.
    pub fn iter(&self) -> TableIter<'a> {
        TableIter::new(self.hff, self.index + 1)
    }

    /// Get an iterator of the chunks.
    pub fn chunks(&self) -> ChunkIter<'a> {
        let table = &self.hff.tables_array()[self.index];
        ChunkIter::new(
            self.hff,
            table.chunk_index() as usize,
            table.chunk_count() as usize,
        )
    }

    /// Get the count of chunks in the table.
    pub fn chunk_count(&self) -> usize {
        self.hff.tables_array()[self.index].chunk_count() as usize
    }

    /// Read the metadata from the given source.
    pub fn metadata(&self, source: &mut dyn ReadSeek) -> Result<Vec<u8>> {
        let table = &self.hff.tables_array()[self.index];
        if table.metadata_length() > 0 {
            source.seek(SeekFrom::Start(table.metadata_offset()))?;
            let mut buffer = vec![0; table.metadata_length() as usize];
            source.read_exact(buffer.as_mut_slice())?;
            Ok(buffer)
        } else {
            Ok(vec![])
        }
    }
}