Merge pull request #26 from darakian/add-discombobulate

Add chunk iter
This commit is contained in:
Jon Moroney 2020-07-06 18:24:04 -07:00 committed by GitHub
commit 5360ce1477
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 39 additions and 5 deletions

View file

@ -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;

32
src/utils.rs Normal file
View file

@ -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))
}
}
}