hff_async_std/
lib.rs

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