diff --git a/README.md b/README.md index b292f01..974630d 100644 --- a/README.md +++ b/README.md @@ -41,4 +41,4 @@ to see Haggis implemented in other languages. - [x] Automatically detect zstd compressed archives - [x] Add path to error message when passing between threads - [x] Add ability to write archives to stdout -- [ ] Add ability to read archives from stdin +- [x] Add ability to read archives from stdin diff --git a/src/lib.rs b/src/lib.rs index d8dab3d..23e0475 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -54,6 +54,7 @@ pub fn detect_zstd(reader: &mut R) -> Result { Ok(buf == ZSTD_MAGIC) } +#[allow(clippy::similar_names)] /// Creates a haggis archive from a list of files /// # Errors /// Returns `crate::Error` if io fails or several other error conditions @@ -73,6 +74,7 @@ pub fn create_archive( Ok(()) } +#[allow(clippy::similar_names)] /// Creates a haggis archive and writes it to stdout /// # Errors /// Returns `crate::Error` if io fails @@ -88,6 +90,7 @@ pub fn create_archive_stdout( Ok(()) } +#[allow(clippy::similar_names)] /// Streams a haggis archive over something which implements `Write` /// # Errors /// Returns `crate::Error` if io fails or several other error conditions diff --git a/src/stream.rs b/src/stream.rs index 35eb9c5..8281c25 100644 --- a/src/stream.rs +++ b/src/stream.rs @@ -1,4 +1,5 @@ #![allow(clippy::similar_names)] + #[cfg(feature = "parallel")] use { crate::FileType, @@ -8,20 +9,22 @@ use { use { crate::{Error, Node, MAGIC}, std::{ - io::{ErrorKind, Read}, + fs::File, + io::{self, BufReader, ErrorKind, Read}, iter::Iterator, + os::fd::{AsRawFd, FromRawFd}, }, }; /// An iterator over a series of archive `Node`'s. This struct is generic over any /// type which implements `Read`, such as a file or a network stream. #[derive(Debug)] -pub struct Stream { +pub struct Stream { pub length: u32, reader: R, } -impl Iterator for Stream { +impl Iterator for Stream { type Item = Result; fn next(&mut self) -> Option { @@ -76,10 +79,25 @@ pub enum Message { Eof, } -impl Stream { - /// Creates a new archive +impl Stream> { + /// Creates a new `Stream` from stdin /// # Errors - /// Returns `crate::Error` if io fails or several other error conditions + /// Returns `crate::Error` if io fails or the first 11 bytes are not + /// the correct Haggis *magic* number + pub fn new_from_stdin() -> Result { + let stdin = io::stdin(); + let raw = stdin.as_raw_fd(); + let fd = unsafe { File::from_raw_fd(raw) }; + let reader = BufReader::new(fd); + Stream::new(reader) + } +} + +impl Stream { + /// Creates a new `Stream` + /// # Errors + /// Returns `crate::Error` if io fails or the first 11 bytes are not the + /// correct Haggis *magic* number pub fn new(mut reader: R) -> Result { let mut buf = [0; 11]; reader.read_exact(&mut buf)?; @@ -126,8 +144,10 @@ impl Stream { } Ok(()) } +} - #[cfg(feature = "parallel")] +#[cfg(feature = "parallel")] +impl Stream { /// Extracts and archive in parallel /// # Errors /// Returns `crate::Error` if io fails or several other error conditions @@ -186,7 +206,6 @@ impl Stream { Ok(()) } - #[cfg(feature = "parallel")] /// Extracts and archive in parallel and runs the passed in function for /// each `Node` /// # Errors