hff_tokio/
lib.rs

1// Pull in core if special behavior is needed.
2pub use hff_core;
3
4// Pull in common needs.  Aka: prelude.
5pub use hff_core::{
6    read::{ChunkView, Hff, TableView},
7    utilities,
8    write::{chunk, hff, table, ChunkDesc, DataSource, HffDesc, TableBuilder},
9    ByteOrder, ChunkCache, ContentInfo, Ecc, Error, IdType, Result, Version, BE, LE, NE, OP,
10};
11
12mod read;
13pub use read::*;
14
15#[cfg(test)]
16mod tests {
17    use super::*;
18
19    #[tokio::test]
20    async fn tests() -> Result<()> {
21        let content = hff([
22            table((Ecc::new("Prime"), Ecc::new("Second")))
23                // Metadata and chunks can be pulled from many types of source data.
24                .metadata("Each table can have metadata.")?
25                // Tables can have chunks.
26                .chunks([chunk(
27                    (Ecc::new("AChunk"), Ecc::INVALID),
28                    "Each table can have 0..n chunks of data.",
29                )?])
30                // Tables can have child tables.
31                .children([table((Ecc::new("Child1"), Ecc::INVALID))
32                    .metadata("Unique to this table.")?
33                    .chunks([chunk(
34                        (Ecc::new("ThisFile"), Ecc::new("Copy")),
35                        "More data for the chunk.",
36                    )?])]),
37            // And there can be multiple tables at the root.
38            table((Ecc::new("Child2"), Ecc::INVALID)),
39        ]);
40
41        // Use std variation to write into a vector.
42        let mut buffer = vec![];
43        use hff_std::Writer;
44        content.write::<hff_core::NE>(IdType::Ecc2, "Test", &mut buffer)?;
45
46        // The reader must take ownership of the given item in order to
47        // properly function.
48        use std::io::Cursor;
49        let reader: Box<dyn ReadSeek> = Box::new(Cursor::new(buffer.into_boxed_slice()));
50
51        // Open the buffer as an hff.
52        let hff = open(reader).await?;
53
54        for (depth, table) in hff.depth_first() {
55            // Print information about the table.
56            println!(
57                "{}: {:?} ({})",
58                depth,
59                table.identifier(),
60                std::str::from_utf8(hff.read(&table).await?.as_slice()).unwrap()
61            );
62
63            // Iterate the chunks.
64            for chunk in table.chunks() {
65                println!(
66                    "{}",
67                    std::str::from_utf8(hff.read(&chunk).await?.as_slice()).unwrap()
68                );
69            }
70        }
71
72        Ok(())
73    }
74}