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

/// Build a table.
#[derive(Debug)]
pub struct TableBuilder(TableDesc);

impl TableBuilder {
    /// Create a new instance.
    pub(super) fn new(primary: Ecc, secondary: Ecc) -> Self {
        Self(TableDesc::new(primary, secondary))
    }

    /// Add a child table to the current table.
    pub fn table(mut self, content: TableDesc) -> Self {
        self.0.push_table(content);
        self
    }

    /// Add a chunk entry to the current table.
    pub fn chunk<T>(
        mut self,
        primary: impl Into<Ecc>,
        secondary: impl Into<Ecc>,
        data: T,
    ) -> Result<Self>
    where
        T: TryInto<Box<dyn DataSource>>,
        <T as TryInto<Box<dyn DataSource>>>::Error: std::fmt::Debug,
        Error: From<<T as TryInto<Box<dyn DataSource>>>::Error>,
    {
        self.0.push_chunk(ChunkDesc::new(
            primary.into(),
            secondary.into(),
            data.try_into()?,
        ));
        Ok(self)
    }

    /// Add some metadata to the current table.
    pub fn metadata<T>(mut self, data_source: T) -> Result<Self>
    where
        T: TryInto<Box<dyn DataSource>>,
        <T as TryInto<Box<dyn DataSource>>>::Error: std::fmt::Debug,
        Error: From<<T as TryInto<Box<dyn DataSource>>>::Error>,
    {
        self.0.metadata(data_source.try_into()?)?;
        Ok(self)
    }

    /// End building the table.
    pub fn end(self) -> TableDesc {
        self.0
    }
}