]> git.proxmox.com Git - pxar.git/commitdiff
add Decoder::open
authorWolfgang Bumiller <w.bumiller@proxmox.com>
Mon, 23 Mar 2020 09:17:40 +0000 (10:17 +0100)
committerWolfgang Bumiller <w.bumiller@proxmox.com>
Mon, 23 Mar 2020 09:17:40 +0000 (10:17 +0100)
Signed-off-by: Wolfgang Bumiller <w.bumiller@proxmox.com>
src/decoder/aio.rs
src/decoder/sync.rs

index 8cf7aa8dff5adddd944ab276bae593d1a466cb12..4a9dcc3a7689e21954a16484dd0b3e4877c1a5fe 100644 (file)
@@ -2,6 +2,9 @@
 
 use std::io;
 
+#[cfg(feature = "tokio-fs")]
+use std::path::Path;
+
 use crate::decoder::{self, SeqRead};
 use crate::Entry;
 
@@ -14,23 +17,32 @@ pub struct Decoder<T> {
 }
 
 #[cfg(feature = "futures-io")]
-impl<T: futures::io::AsyncRead> Decoder<T> {
+impl<T: futures::io::AsyncRead> Decoder<FuturesReader<T>> {
     /// Decode a `pxar` archive from a `futures::io::AsyncRead` input.
     #[inline]
-    pub async fn from_futures(input: T) -> io::Result<Decoder<FuturesReader<T>>> {
+    pub async fn from_futures(input: T) -> io::Result<Self> {
         Decoder::new(FuturesReader::new(input)).await
     }
 }
 
 #[cfg(feature = "tokio-io")]
-impl<T: tokio::io::AsyncRead> Decoder<T> {
+impl<T: tokio::io::AsyncRead> Decoder<TokioReader<T>> {
     /// Decode a `pxar` archive from a `tokio::io::AsyncRead` input.
     #[inline]
-    pub async fn from_tokio(input: T) -> io::Result<Decoder<TokioReader<T>>> {
+    pub async fn from_tokio(input: T) -> io::Result<Self> {
         Decoder::new(TokioReader::new(input)).await
     }
 }
 
+#[cfg(feature = "tokio-fs")]
+impl Decoder<TokioReader<tokio::fs::File>> {
+    /// Decode a `pxar` archive from a `tokio::io::AsyncRead` input.
+    #[inline]
+    pub async fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> {
+        Decoder::from_tokio(tokio::fs::File::open(path.as_ref()).await?).await
+    }
+}
+
 impl<T: SeqRead> Decoder<T> {
     /// Create an async decoder from an input implementing our internal read interface.
     pub async fn new(input: T) -> io::Result<Self> {
index 9311f216babb48674f76da8d9391b7f693dd8359..91e7d3c7a4cc32d712e7c8b73aeedad9c8ffe01d 100644 (file)
@@ -1,6 +1,7 @@
 //! Blocking `pxar` format handling.
 
 use std::io;
+use std::path::Path;
 use std::pin::Pin;
 use std::task::{Context, Poll};
 
@@ -21,14 +22,21 @@ pub struct Decoder<T> {
     inner: decoder::DecoderImpl<T>,
 }
 
-impl<T: io::Read> Decoder<T> {
+impl<T: io::Read> Decoder<StandardReader<T>> {
     /// Decode a `pxar` archive from a regular `std::io::Read` input.
     #[inline]
-    pub fn from_std(input: T) -> io::Result<Decoder<StandardReader<T>>> {
+    pub fn from_std(input: T) -> io::Result<Self> {
         Decoder::new(StandardReader::new(input))
     }
 }
 
+impl Decoder<StandardReader<std::fs::File>> {
+    /// Convenience shortcut for `File::open` followed by `Accessor::from_file`.
+    pub fn open<P: AsRef<Path>>(path: P) -> io::Result<Self> {
+        Self::from_std(std::fs::File::open(path.as_ref())?)
+    }
+}
+
 impl<T: SeqRead> Decoder<T> {
     /// Create a *blocking* decoder from an input implementing our internal read interface.
     ///