Skip to content

Commit

Permalink
Update Types In bip_metainfo (i64 -> u64, PathBuf Instead Of Paths It…
Browse files Browse the repository at this point in the history
…er); Bump Minor Version
  • Loading branch information
GGist committed Apr 22, 2017
1 parent 27c51ce commit 3794dbb
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 60 deletions.
32 changes: 1 addition & 31 deletions bip_metainfo/src/iter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,34 +60,4 @@ impl<'a> Iterator for Pieces<'a> {
None
}
}
}

// ----------------------------------------------------------------------------//

/// Iterator over each file path element within the MetainfoFile.
pub struct Paths<'a> {
paths: &'a [String],
index: usize,
}

impl<'a> Paths<'a> {
pub fn new(paths: &'a [String]) -> Paths<'a> {
Paths {
index: 0,
paths: paths,
}
}
}

impl<'a> Iterator for Paths<'a> {
type Item = &'a str;

fn next(&mut self) -> Option<&'a str> {
if let Some(paths) = self.paths.get(self.index) {
self.index += 1;
Some(&paths)
} else {
None
}
}
}
}
4 changes: 2 additions & 2 deletions bip_metainfo/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@
//!
//! let single_file = file.info().files().next().unwrap();
//! assert_eq!(single_file.length() as usize, file_data.len());
//! assert_eq!(single_file.paths().count(), 1);
//! assert_eq!(single_file.paths().next().unwrap(), file_name);
//! assert_eq!(single_file.path().iter().count(), 1);
//! assert_eq!(single_file.path().to_str().unwrap(), file_name);
//! }
//! ```

Expand Down
49 changes: 26 additions & 23 deletions bip_metainfo/src/metainfo.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
//! Accessing the fields of a MetainfoFile.
use std::path::{Path, PathBuf};

use bip_bencode::{Bencode, Dictionary};
use bip_util::bt::InfoHash;
use bip_util::sha;

use parse;
use error::{ParseError, ParseErrorKind, ParseResult};
use iter::{Paths, Files, Pieces};
use iter::{Files, Pieces};

/// Contains optional and required information for the torrent.
#[derive(Debug)]
Expand Down Expand Up @@ -97,10 +98,10 @@ fn parse_from_bytes(bytes: &[u8]) -> ParseResult<MetainfoFile> {
/// Contains files and checksums for the torrent.
#[derive(Debug)]
pub struct InfoDictionary {
files: Vec<File>,
pieces: Vec<[u8; sha::SHA_HASH_LEN]>,
piece_len: i64,
is_private: bool,
files: Vec<File>,
pieces: Vec<[u8; sha::SHA_HASH_LEN]>,
piece_len: u64,
is_private: bool,
// Present only for multi file torrents.
file_directory: Option<String>,
}
Expand All @@ -121,7 +122,7 @@ impl InfoDictionary {
}

/// Length in bytes of each piece.
pub fn piece_length(&self) -> i64 {
pub fn piece_length(&self) -> u64 {
self.piece_len
}

Expand Down Expand Up @@ -221,8 +222,8 @@ fn allocate_pieces(pieces: &[u8]) -> ParseResult<Vec<[u8; sha::SHA_HASH_LEN]>> {
/// Contains information for a single file.
#[derive(Debug)]
pub struct File {
len: i64,
path: Vec<String>,
len: u64,
path: PathBuf,
md5sum: Option<Vec<u8>>,
}

Expand All @@ -235,7 +236,7 @@ impl File {

Ok(File {
len: length,
path: vec![name.to_owned()],
path: name.to_owned().into(),
md5sum: md5sum,
})
}
Expand All @@ -247,22 +248,22 @@ impl File {

let path_list_bencode = try!(parse::parse_path_list(file_dict));

let mut path_list = Vec::with_capacity(path_list_bencode.len());
let mut path_buf = PathBuf::new();
for path_bencode in path_list_bencode {
let path = try!(parse::parse_path_str(path_bencode));

path_list.push(path.to_owned());
path_buf.push(path);
}

Ok(File {
len: length,
path: path_list,
path: path_buf,
md5sum: md5sum,
})
}

/// Length of the file in bytes.
pub fn length(&self) -> i64 {
pub fn length(&self) -> u64 {
self.len
}

Expand All @@ -273,17 +274,16 @@ impl File {
self.md5sum.as_ref().map(|m| &m[..])
}

/// Iterator over the path elements of the file.
///
/// The last element is the name of the file.
pub fn paths<'a>(&'a self) -> Paths<'a> {
Paths::new(&self.path)
/// Path of the file.
pub fn path(&self) -> &Path {
&self.path
}
}

#[cfg(test)]
mod tests {
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};

use bip_bencode::Bencode;
use bip_util::sha;
Expand Down Expand Up @@ -373,7 +373,7 @@ mod tests {
assert_eq!(metainfo_file.creation_date, create_date);

assert_eq!(metainfo_file.info().directory(), directory);
assert_eq!(metainfo_file.info().piece_length(), piece_length.unwrap());
assert_eq!(metainfo_file.info().piece_length(), piece_length.unwrap() as u64);
assert_eq!(metainfo_file.info().is_private(), private.unwrap_or(0) == 1);

let pieces = pieces.unwrap();
Expand All @@ -393,12 +393,15 @@ mod tests {
let meta_file = meta_files.next().unwrap();
let supp_file = supp_files.next().unwrap();

assert_eq!(meta_file.length(), supp_file.0.unwrap());
assert_eq!(meta_file.length(), supp_file.0.unwrap() as u64);
assert_eq!(meta_file.md5sum(), supp_file.1);

let meta_paths: Vec<&str> = meta_file.paths().collect();
let supp_paths: Vec<&str> =
supp_file.2.as_ref().unwrap().iter().map(|p| &p[..]).collect();
let meta_paths: &Path = meta_file.path();
let supp_paths: PathBuf = supp_file.2.as_ref().unwrap().iter().fold(PathBuf::new(), |mut buf, item| {
let item: &str = item;
buf.push(item);
buf
});
assert_eq!(meta_paths, supp_paths);
}
}
Expand Down
8 changes: 4 additions & 4 deletions bip_metainfo/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,8 +88,8 @@ pub fn parse_info_hash<'a>(root_dict: &Dictionary<'a, Bencode<'a>>) -> ParseResu
// ----------------------------------------------------------------------------//

/// Parses the piece length from the info dictionary.
pub fn parse_piece_length<'a>(info_dict: &Dictionary<'a, Bencode<'a>>) -> ParseResult<i64> {
CONVERT.lookup_and_convert_int(info_dict, PIECE_LENGTH_KEY)
pub fn parse_piece_length<'a>(info_dict: &Dictionary<'a, Bencode<'a>>) -> ParseResult<u64> {
CONVERT.lookup_and_convert_int(info_dict, PIECE_LENGTH_KEY).map(|len| len as u64)
}

/// Parses the pieces from the info dictionary.
Expand Down Expand Up @@ -122,8 +122,8 @@ pub fn parse_file_dict<'a, 'b>(file_bencode: &'b Bencode<'a>)
}

/// Parses the length from the info or file dictionary.
pub fn parse_length<'a>(info_or_file_dict: &Dictionary<'a, Bencode<'a>>) -> ParseResult<i64> {
CONVERT.lookup_and_convert_int(info_or_file_dict, LENGTH_KEY)
pub fn parse_length<'a>(info_or_file_dict: &Dictionary<'a, Bencode<'a>>) -> ParseResult<u64> {
CONVERT.lookup_and_convert_int(info_or_file_dict, LENGTH_KEY).map(|len| len as u64)
}

/// Parses the md5sum from the info or file dictionary.
Expand Down

0 comments on commit 3794dbb

Please sign in to comment.