Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add async support via Tokio #66

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ documentation = "https://docs.rs/file-format"
exclude = ["/.github", "/examples", "/fixtures", "/tests", ".gitattributes", ".gitignore"]
rust-version = "1.60.0"

[dependencies]
tokio = { version = "1.29.1", default-features = false, optional = true }

[dev-dependencies]
tokio = { version = "1.29.1", default-features = false, features = ["rt-multi-thread"] }

[features]
## Reader features
reader = [
Expand Down Expand Up @@ -42,3 +48,5 @@ reader-sqlite3 = []
reader-txt = []
reader-xml = []
reader-zip = []

tokio = ["dep:tokio", "tokio/io-util"]
42 changes: 41 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,8 @@ use std::{
pub use formats::FileFormat;

impl FileFormat {
const BUF_SIZE: usize = 36_870;

/// Determines file format from bytes.
///
/// # Examples
Expand Down Expand Up @@ -290,7 +292,7 @@ impl FileFormat {
///```
pub fn from_reader<R: Read + Seek>(mut reader: R) -> Result<Self> {
// Creates and fills a buffer.
let mut buf = [0; 36_870];
let mut buf = [0; Self::BUF_SIZE];
let nread = reader.read(&mut buf)?;

// Determines file format.
Expand All @@ -303,6 +305,44 @@ impl FileFormat {
Self::from_generic_reader(&mut reader)
})
}

#[cfg(feature = "tokio")]
/// Determines file format from an async reader.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use file_format::FileFormat;
///
/// # tokio::runtime::Runtime::new().unwrap().block_on(async {
/// let fmt = FileFormat::from_reader_async(tokio::io::empty()).await?;
/// assert_eq!(fmt, FileFormat::Empty);
/// # Ok::<(), tokio::io::Error>(())
/// # });
///```
pub async fn from_reader_async<R: tokio::io::AsyncRead + tokio::io::AsyncSeek + Unpin>(
mut reader: R,
) -> Result<Self> {
use tokio::io::AsyncReadExt;

// Creates and fills a buffer.
let mut buf = [0; Self::BUF_SIZE];
let nread = reader.read(&mut buf).await?;

// Determines file format.
Ok(if nread == 0 {
Self::Empty
} else if let Some(fmt) = Self::from_signature(&buf[..nread]) {
match Self::from_fmt_reader_async(fmt, &mut reader).await {
Ok(fmt) => fmt,
Err(_) => Self::from_generic_reader_async(&mut reader).await,
}
} else {
Self::from_generic_reader_async(&mut reader).await
})
}
}

impl Default for FileFormat {
Expand Down
Loading