hff_core/write/
mod.rs

1//! Higher level support for writing hff files.
2
3mod chunk_array;
4pub use chunk_array::ChunkArray;
5
6mod data_source;
7pub use data_source::DataSource;
8
9mod table_array;
10pub use table_array::TableArray;
11
12mod data_array;
13pub use data_array::DataArray;
14
15mod chunk_desc;
16pub use chunk_desc::ChunkDesc;
17
18mod table_builder;
19pub use table_builder::TableBuilder;
20
21mod table_desc;
22pub use table_desc::TableDesc;
23
24mod hff_desc;
25pub use hff_desc::HffDesc;
26
27use crate::{Error, Identifier, Result};
28
29/// Start building a new table.
30pub fn table<'a>(identifier: impl Into<Identifier>) -> TableBuilder<'a> {
31    TableBuilder::new(identifier.into())
32}
33
34/// Build a new chunk.
35pub fn chunk<'a, T>(identifier: impl Into<Identifier>, content: T) -> Result<ChunkDesc<'a>>
36where
37    T: TryInto<DataSource<'a>, Error = Error>,
38{
39    Ok(ChunkDesc::new(identifier.into(), content.try_into()?))
40}
41
42/// Build the structure of the Hff content.
43pub fn hff<'a>(tables: impl IntoIterator<Item = TableBuilder<'a>>) -> HffDesc<'a> {
44    // Split the tables into their components.
45    let mut table_array = TableArray::new();
46    let mut chunk_array = ChunkArray::new();
47    let mut data_array = DataArray::new();
48
49    // Collect the tables into a vector so we know the length.
50    let tables = tables
51        .into_iter()
52        .map(|desc| desc.finish())
53        .collect::<Vec<_>>();
54
55    let table_count = tables.len();
56    for (index, table) in tables.into_iter().enumerate() {
57        // Determine if this table has a sibling.
58        let has_sibling = index < table_count - 1;
59        // And flatten the table.
60        table.flatten(
61            has_sibling,
62            &mut table_array,
63            &mut chunk_array,
64            &mut data_array,
65        );
66    }
67    HffDesc::new(table_array, chunk_array, data_array)
68}