use crate::{ByteOrder, Result, Table};
use std::ops::{Index, IndexMut};
#[derive(Debug)]
pub struct TableArray {
tables: Vec<(bool, Table)>,
}
impl TableArray {
pub fn new() -> Self {
Self { tables: vec![] }
}
pub fn len(&self) -> usize {
self.tables.len()
}
pub fn push(&mut self, has_metadata: bool, table: Table) {
self.tables.push((has_metadata, table));
}
pub fn last_mut(&mut self) -> Option<&mut (bool, Table)> {
self.tables.last_mut()
}
pub fn to_bytes<E: ByteOrder>(self) -> Result<Vec<u8>> {
let mut buffer = vec![];
for table in self.tables {
table.1.write::<E>(&mut buffer)?;
}
Ok(buffer)
}
}
impl Index<usize> for TableArray {
type Output = (bool, Table);
fn index(&self, index: usize) -> &Self::Output {
&self.tables[index]
}
}
impl IndexMut<usize> for TableArray {
fn index_mut(&mut self, index: usize) -> &mut Self::Output {
&mut self.tables[index]
}
}