Skip to main content

epub_stream/
async_api.rs

1//! Optional async helpers for high-level EPUB opening.
2//!
3//! This module is available with the `async` feature.
4
5extern crate alloc;
6
7use alloc::vec::Vec;
8use core::result::Result;
9use std::io::Cursor;
10use std::path::Path;
11
12use crate::book::{EpubBook, EpubBookOptions};
13use crate::error::EpubError;
14
15/// Read an EPUB file asynchronously and open it as an `EpubBook`.
16///
17/// This helper reads the file into memory and uses `EpubBook::from_reader`.
18pub async fn open_epub_file_async<P: AsRef<Path>>(
19    path: P,
20) -> Result<EpubBook<Cursor<Vec<u8>>>, EpubError> {
21    open_epub_file_async_with_options(path, EpubBookOptions::default()).await
22}
23
24/// Read an EPUB file asynchronously and open it as an `EpubBook` with options.
25pub async fn open_epub_file_async_with_options<P: AsRef<Path>>(
26    path: P,
27    options: EpubBookOptions,
28) -> Result<EpubBook<Cursor<Vec<u8>>>, EpubError> {
29    let bytes = tokio::fs::read(path)
30        .await
31        .map_err(|e| EpubError::Io(e.to_string()))?;
32    EpubBook::from_reader_with_options(Cursor::new(bytes), options)
33}