hff_core/write/
table_array.rs

1use crate::{ByteOrder, Result, Table};
2use std::ops::{Index, IndexMut};
3
4/// The table array to be written.
5#[derive(Debug)]
6pub struct TableArray {
7    /// A flag indicating if the table had attached metadata,
8    /// and the table structure itself.
9    tables: Vec<(bool, Table)>,
10}
11
12impl TableArray {
13    /// Create a new empty table array.
14    pub fn new() -> Self {
15        Self { tables: vec![] }
16    }
17
18    /// Get the number of tables in the array.
19    pub fn len(&self) -> usize {
20        self.tables.len()
21    }
22
23    /// Push a new table into the array.
24    pub fn push(&mut self, has_metadata: bool, table: Table) {
25        self.tables.push((has_metadata, table));
26    }
27
28    /// Get the last table in the vector mutably.
29    pub fn last_mut(&mut self) -> Option<&mut (bool, Table)> {
30        self.tables.last_mut()
31    }
32
33    /// Convert the table array to a byte vector for
34    /// writing.
35    pub fn to_bytes<E: ByteOrder>(self) -> Result<Vec<u8>> {
36        let mut buffer = vec![];
37        for table in self.tables {
38            table.1.write::<E>(&mut buffer)?;
39        }
40        Ok(buffer)
41    }
42}
43
44impl Index<usize> for TableArray {
45    type Output = (bool, Table);
46
47    fn index(&self, index: usize) -> &Self::Output {
48        &self.tables[index]
49    }
50}
51
52impl IndexMut<usize> for TableArray {
53    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
54        &mut self.tables[index]
55    }
56}