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
use crate::{ByteOrder, Chunk, Result};
use std::ops::{Index, IndexMut};

/// After flattening a table, this is where the chunks will
/// exist.
#[derive(Debug)]
pub struct ChunkArray {
    chunks: Vec<Chunk>,
}

impl ChunkArray {
    /// Create a new empty ChunkArray.
    pub fn new() -> Self {
        Self { chunks: vec![] }
    }

    /// Get the length of the chunk array.
    pub fn len(&self) -> usize {
        self.chunks.len()
    }

    /// Push a new chunk onto the array.
    pub fn push(&mut self, chunk: Chunk) {
        self.chunks.push(chunk);
    }

    /// Convert the chunk array to a byte vector.
    pub fn to_bytes<E: ByteOrder>(self) -> Result<Vec<u8>> {
        let mut buffer = vec![];
        for chunk in self.chunks {
            chunk.write::<E>(&mut buffer)?;
        }
        Ok(buffer)
    }
}

impl Index<usize> for ChunkArray {
    type Output = Chunk;

    fn index(&self, index: usize) -> &Self::Output {
        &self.chunks[index]
    }
}

impl IndexMut<usize> for ChunkArray {
    fn index_mut(&mut self, index: usize) -> &mut Self::Output {
        &mut self.chunks[index]
    }
}