hff_async_std/read/
api.rs

1use super::{AsyncStdReader, ReadSeek};
2use async_std::io::Read;
3use hff_core::{
4    read::{Hff, Inspection},
5    ChunkCache, Result, NE, OP,
6};
7
8/// Opens the input and maintains it for random access to the
9/// metadata and chunks.
10pub async fn open(mut source: impl ReadSeek + 'static) -> Result<Hff<AsyncStdReader>> {
11    let header = AsyncStdReader::read_header(&mut source).await?;
12    let (tables, chunks) = if header.is_native_endian() {
13        (
14            AsyncStdReader::read_tables::<NE>(&mut source, header.table_count()).await?,
15            AsyncStdReader::read_chunks::<NE>(&mut source, header.chunk_count()).await?,
16        )
17    } else {
18        (
19            AsyncStdReader::read_tables::<OP>(&mut source, header.table_count()).await?,
20            AsyncStdReader::read_chunks::<OP>(&mut source, header.chunk_count()).await?,
21        )
22    };
23    Ok(Hff::new(
24        AsyncStdReader::new(source),
25        header,
26        tables,
27        chunks,
28    ))
29}
30
31/// Reads an entire Hff into memory.
32pub async fn read(mut source: &mut (dyn Read + std::marker::Unpin)) -> Result<Hff<ChunkCache>> {
33    let header = AsyncStdReader::read_header(&mut source).await?;
34    let (tables, chunks) = if header.is_native_endian() {
35        (
36            AsyncStdReader::read_tables::<NE>(&mut source, header.table_count()).await?,
37            AsyncStdReader::read_chunks::<NE>(&mut source, header.chunk_count()).await?,
38        )
39    } else {
40        (
41            AsyncStdReader::read_tables::<OP>(&mut source, header.table_count()).await?,
42            AsyncStdReader::read_chunks::<OP>(&mut source, header.chunk_count()).await?,
43        )
44    };
45    let cache = AsyncStdReader::read_body(&mut source, tables.len(), chunks.len()).await?;
46    Ok(Hff::new(cache, header, tables, chunks))
47}
48
49/// Read the structure of a Hff into memory.  Provides access
50/// only to the structure without any of the metadata or chunk
51/// data available.
52pub async fn inspect(mut source: &mut (dyn Read + std::marker::Unpin)) -> Result<Hff<Inspection>> {
53    let header = AsyncStdReader::read_header(&mut source).await?;
54    let (tables, chunks) = if header.is_native_endian() {
55        (
56            AsyncStdReader::read_tables::<NE>(&mut source, header.table_count()).await?,
57            AsyncStdReader::read_chunks::<NE>(&mut source, header.chunk_count()).await?,
58        )
59    } else {
60        (
61            AsyncStdReader::read_tables::<OP>(&mut source, header.table_count()).await?,
62            AsyncStdReader::read_chunks::<OP>(&mut source, header.chunk_count()).await?,
63        )
64    };
65    Ok(Hff::new(Inspection, header, tables, chunks))
66}