hff_core/write/
table_builder.rs

1use super::{ChunkDesc, DataSource, TableDesc};
2use crate::{Error, Identifier, Result};
3
4/// Builder for tables.
5#[derive(Debug)]
6pub struct TableBuilder<'a> {
7    /// The table identifier.
8    identifier: Identifier,
9    /// Optional metadata associated with the table.
10    metadata: Option<DataSource<'a>>,
11    /// Chunks associated with the table..
12    chunks: Vec<ChunkDesc<'a>>,
13    /// Child tables under this table.
14    children: Vec<TableBuilder<'a>>,
15}
16
17impl<'a> TableBuilder<'a> {
18    /// Create a new table builder instance.
19    pub fn new(identifier: Identifier) -> Self {
20        Self {
21            identifier,
22            metadata: None,
23            chunks: vec![],
24            children: vec![],
25        }
26    }
27
28    /// Set the metadata for this table.
29    pub fn metadata<T>(mut self, content: T) -> Result<Self>
30    where
31        T: TryInto<DataSource<'a>, Error = Error>,
32    {
33        self.metadata = Some(content.try_into()?);
34        Ok(self)
35    }
36
37    /// Set the child tables for this table.
38    pub fn children(mut self, children: impl IntoIterator<Item = TableBuilder<'a>>) -> Self {
39        self.children = children.into_iter().collect::<Vec<_>>();
40        self
41    }
42
43    /// Set the chunks associated with this table.
44    pub fn chunks(mut self, content: impl IntoIterator<Item = ChunkDesc<'a>>) -> Self {
45        self.chunks = content.into_iter().collect::<Vec<_>>();
46        self
47    }
48
49    /// Finish building the table.
50    pub fn finish(self) -> TableDesc<'a> {
51        TableDesc::new(
52            self.identifier,
53            self.metadata,
54            self.chunks,
55            self.children
56                .into_iter()
57                .map(|desc| desc.finish())
58                .collect(),
59        )
60    }
61
62    /// Get the identifier.
63    pub fn identifier(&self) -> Identifier {
64        self.identifier
65    }
66}