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
67
68
69
70
71
72
// Pull in core if special behavior is needed.
pub use hff_core;

// Pull in common needs.  Aka: prelude.
pub use hff_core::{
    read::{ChunkView, Hff, TableView},
    utilities,
    write::{chunk, hff, table, ChunkDesc, DataSource, HffDesc, TableBuilder},
    ByteOrder, ChunkCache, ContentInfo, Ecc, Error, Result, Version, BE, LE, NE, OP,
};

mod read;
pub use read::*;

#[cfg(test)]
mod tests {
    use super::*;

    #[tokio::test]
    async fn tests() -> Result<()> {
        let content = hff([
            table("Prime", "Second")
                // Metadata and chunks can be pulled from many types of source data.
                .metadata("Each table can have metadata.")?
                // Tables can have chunks.
                .chunks([chunk(
                    "AChunk",
                    Ecc::INVALID,
                    "Each table can have 0..n chunks of data.",
                )?])
                // Tables can have child tables.
                .children([table("Child1", Ecc::INVALID)
                    .metadata("Unique to this table.")?
                    .chunks([chunk("ThisFile", "Copy", "More data for the chunk.")?])]),
            // And there can be multiple tables at the root.
            table("Child2", Ecc::INVALID),
        ]);

        // Use std variation to write into a vector.
        let mut buffer = vec![];
        use hff_std::Writer;
        content.write::<hff_core::NE>("Test", &mut buffer)?;

        // The reader must take ownership of the given item in order to
        // properly function.
        use std::io::Cursor;
        let reader: Box<dyn ReadSeek> = Box::new(Cursor::new(buffer.into_boxed_slice()));

        // Open the buffer as an hff.
        let hff = open(reader).await?;

        for (depth, table) in hff.depth_first() {
            // Print information about the table.
            println!(
                "{}: {:?} ({})",
                depth,
                table.primary(),
                std::str::from_utf8(hff.read(&table).await?.as_slice()).unwrap()
            );

            // Iterate the chunks.
            for chunk in table.chunks() {
                println!(
                    "{}",
                    std::str::from_utf8(hff.read(&chunk).await?.as_slice()).unwrap()
                );
            }
        }

        Ok(())
    }
}