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
use super::{ChunkDesc, DataSource, TableDesc};
use crate::{Error, Identifier, Result};

/// Builder for tables.
#[derive(Debug)]
pub struct TableBuilder<'a> {
    /// The table identifier.
    identifier: Identifier,
    /// Optional metadata associated with the table.
    metadata: Option<DataSource<'a>>,
    /// Chunks associated with the table..
    chunks: Vec<ChunkDesc<'a>>,
    /// Child tables under this table.
    children: Vec<TableBuilder<'a>>,
}

impl<'a> TableBuilder<'a> {
    /// Create a new table builder instance.
    pub fn new(identifier: Identifier) -> Self {
        Self {
            identifier,
            metadata: None,
            chunks: vec![],
            children: vec![],
        }
    }

    /// Set the metadata for this table.
    pub fn metadata<T>(mut self, content: T) -> Result<Self>
    where
        T: TryInto<DataSource<'a>, Error = Error>,
    {
        self.metadata = Some(content.try_into()?);
        Ok(self)
    }

    /// Set the child tables for this table.
    pub fn children(mut self, children: impl IntoIterator<Item = TableBuilder<'a>>) -> Self {
        self.children = children.into_iter().collect::<Vec<_>>();
        self
    }

    /// Set the chunks associated with this table.
    pub fn chunks(mut self, content: impl IntoIterator<Item = ChunkDesc<'a>>) -> Self {
        self.chunks = content.into_iter().collect::<Vec<_>>();
        self
    }

    /// Finish building the table.
    pub fn finish(self) -> TableDesc<'a> {
        TableDesc::new(
            self.identifier,
            self.metadata,
            self.chunks,
            self.children
                .into_iter()
                .map(|desc| desc.finish())
                .collect(),
        )
    }

    /// Get the identifier.
    pub fn identifier(&self) -> Identifier {
        self.identifier
    }
}