| 1 | // Copyright (c) 2022 Harry [Majored] [[email protected]] |
| 2 | // MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE) |
| 3 | |
| 4 | pub(crate) mod combined_record; |
| 5 | pub(crate) mod compressed; |
| 6 | pub(crate) mod entry; |
| 7 | pub(crate) mod hashed; |
| 8 | pub(crate) mod locator; |
| 9 | pub(crate) mod owned; |
| 10 | |
| 11 | pub use combined_record::CombinedCentralDirectoryRecord; |
| 12 | |
| 13 | use crate::string::{StringEncoding, ZipString}; |
| 14 | use futures_lite::io::{AsyncRead, AsyncReadExt}; |
| 15 | |
| 16 | /// Read and return a dynamic length string from a reader which impls AsyncRead. |
| 17 | pub(crate) async fn read_string<R>(reader: R, length: usize, encoding: StringEncoding) -> std::io::Result<ZipString> |
| 18 | where |
| 19 | R: AsyncRead + Unpin, |
| 20 | { |
| 21 | Ok(ZipString::new(read_bytes(reader, length).await?, encoding)) |
| 22 | } |
| 23 | |
| 24 | /// Read and return a dynamic length vector of bytes from a reader which impls AsyncRead. |
| 25 | pub(crate) async fn read_bytes<R>(reader: R, length: usize) -> std::io::Result<Vec<u8>> |
| 26 | where |
| 27 | R: AsyncRead + Unpin, |
| 28 | { |
| 29 | let mut buffer = Vec::with_capacity(length); |
| 30 | reader.take(length as u64).read_to_end(&mut buffer).await?; |
| 31 | |
| 32 | Ok(buffer) |
| 33 | } |
| 34 | |
| 35 | /// A macro that returns the inner value of an Ok or early-returns in the case of an Err. |
| 36 | /// |
| 37 | /// This is almost identical to the ? operator but handles the situation when a Result is used in combination with |
| 38 | /// Poll (eg. tokio's IO traits such as AsyncRead). |
| 39 | macro_rules! poll_result_ok { |
| 40 | ($poll:expr) => { |
| 41 | match $poll { |
| 42 | Ok(inner) => inner, |
| 43 | Err(err) => return Poll::Ready(Err(err)), |
| 44 | } |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | use poll_result_ok; |