diff options
author | Jon Moroney <darakian@gmail.com> | 2020-07-06 18:24:04 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-07-06 18:24:04 -0700 |
commit | 5360ce1477d8a6eecc83f4919aaf2a801f92e39a (patch) | |
tree | f760b6c00fc1e21a6256e0daf772a363170202c9 | |
parent | 4c8a32d73a5407d0233451b1a89d9f4e9fb4371e (diff) | |
parent | 927d1bab3d663bee151eb3c7f0b1e1612f98ef03 (diff) |
Merge pull request #26 from darakian/add-discombobulate
Add chunk iter
-rw-r--r-- | src/lib.rs | 12 | ||||
-rw-r--r-- | src/utils.rs | 32 |
2 files changed, 39 insertions, 5 deletions
@@ -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<Vec<u8>, io::Error>; + fn next(&mut self) -> Option<Result<Vec<u8>, 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)) + } + } +} |