From 927d1bab3d663bee151eb3c7f0b1e1612f98ef03 Mon Sep 17 00:00:00 2001 From: Jon Moroney Date: Mon, 6 Jul 2020 18:10:06 -0700 Subject: [PATCH] Add chunk iter This PR adds a new struct which breaks a file into chunks of a given size. --- src/lib.rs | 12 +++++++----- src/utils.rs | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 5 deletions(-) create mode 100644 src/utils.rs diff --git a/src/lib.rs b/src/lib.rs index 1853962..3560e69 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -2,6 +2,8 @@ //! //! `ddh` is a collection of functions and structs to aid in analysing filesystem directories. +pub mod utils; + use std::hash::{Hasher}; use std::fs::{self, DirEntry}; use std::io::{Read}; @@ -56,7 +58,7 @@ impl Fileinfo{ Fileinfo{full_hash: full_hash, partial_hash: partial_hash, file_length: length, file_paths: vec![path]} } /// Gets the length of the files in the current collection. - /// + /// /// # Examples /// ``` /// use std::path::Path; @@ -70,7 +72,7 @@ impl Fileinfo{ self.file_length } /// Gets the hash of the full file if available. - /// + /// /// # Examples /// ``` /// use std::path::Path; @@ -87,7 +89,7 @@ impl Fileinfo{ self.full_hash = hash } /// Gets the hash of the partially read file if available. - /// + /// /// # Examples /// ``` /// use std::path::Path; @@ -104,7 +106,7 @@ impl Fileinfo{ self.partial_hash = hash } /// Gets a candidate name. This will be the name of the first file inserted into the collection and so can vary. - /// + /// /// # Examples /// ``` /// use std::path::Path; @@ -126,7 +128,7 @@ impl Fileinfo{ .unwrap() } /// Gets all paths in the current collection. This can be used to get the names of each file with the string `rsplit("/")` method. - /// + /// /// # Examples /// ``` /// use std::path::Path; diff --git a/src/utils.rs b/src/utils.rs new file mode 100644 index 0000000..cf34a62 --- /dev/null +++ b/src/utils.rs @@ -0,0 +1,32 @@ +use std::path::Path; +use std::fs::File; +use std::io::{Read, self}; + +pub struct ChunkIter{ + f: File, + chunk_len: usize, +} + +impl ChunkIter{ + pub fn new(f: File, len: usize) -> Self{ + ChunkIter{f: f, chunk_len: len} + } +} + +impl Iterator for ChunkIter{ + type Item = Result, io::Error>; + fn next(&mut self) -> Option, io::Error>>{ + let mut buffer = Vec::with_capacity(self.chunk_len); + match self.f.by_ref() + .take(self.chunk_len as u64) + .read_to_end(&mut buffer){ + Ok(i) => { + if i == 0 { + return None + } else { + Some(Ok(buffer))} + }, + Err(e) => Some(Err(e)) + } + } +}