Remove FatDir::list() method.

Vec can be created by using Iterator trait.
This commit is contained in:
Rafał Harabień 2017-10-01 18:45:58 +02:00
parent 284d26282c
commit d88850624a
3 changed files with 13 additions and 19 deletions

View File

@ -27,13 +27,13 @@ fn main() {
let mut buf_rdr = BufReader::new(file); let mut buf_rdr = BufReader::new(file);
let fs = FatFileSystem::new(&mut buf_rdr).unwrap(); let fs = FatFileSystem::new(&mut buf_rdr).unwrap();
let mut root_dir = fs.root_dir(); let mut root_dir = fs.root_dir();
let mut dir = match env::args().nth(1) { let dir = match env::args().nth(1) {
None => root_dir, None => root_dir,
Some(ref path) if path == "." => root_dir, Some(ref path) if path == "." => root_dir,
Some(ref path) => root_dir.open_dir(&path).unwrap(), Some(ref path) => root_dir.open_dir(&path).unwrap(),
}; };
let entries = dir.list().unwrap(); for r in dir {
for e in entries { let e = r.unwrap();
let modified = e.modified().format("%Y-%m-%d %H:%M:%S").to_string(); let modified = e.modified().format("%Y-%m-%d %H:%M:%S").to_string();
println!("{:4} {} {}", format_file_size(e.len()), modified, e.file_name()); println!("{:4} {} {}", format_file_size(e.len()), modified, e.file_name());
} }

View File

@ -182,11 +182,6 @@ impl <'a, 'b> FatDir<'a, 'b> {
FatDir { rdr, state } FatDir { rdr, state }
} }
pub fn list(&mut self) -> io::Result<Vec<FatDirEntry<'a, 'b>>> {
self.rewind();
Ok(self.map(|x| x.unwrap()).collect())
}
pub fn rewind(&mut self) { pub fn rewind(&mut self) {
self.rdr.seek(SeekFrom::Start(0)).unwrap(); self.rdr.seek(SeekFrom::Start(0)).unwrap();
} }
@ -235,8 +230,9 @@ impl <'a, 'b> FatDir<'a, 'b> {
} }
fn find_entry(&mut self, name: &str) -> io::Result<FatDirEntry<'a, 'b>> { fn find_entry(&mut self, name: &str) -> io::Result<FatDirEntry<'a, 'b>> {
let entries: Vec<FatDirEntry<'a, 'b>> = self.list()?; self.rewind();
for e in entries { for r in self {
let e = r?;
if e.file_name().eq_ignore_ascii_case(name) { if e.file_name().eq_ignore_ascii_case(name) {
return Ok(e); return Ok(e);
} }

View File

@ -5,7 +5,7 @@ use std::io::{BufReader, SeekFrom};
use std::io::prelude::*; use std::io::prelude::*;
use std::str; use std::str;
use rfat::{FatFileSystem, FatType}; use rfat::{FatFileSystem, FatType, FatDirEntry};
const TEST_TEXT: &str = "Rust is cool!\n"; const TEST_TEXT: &str = "Rust is cool!\n";
const FAT12_IMG: &str = "resources/fat12.img"; const FAT12_IMG: &str = "resources/fat12.img";
@ -20,16 +20,15 @@ fn call_with_fs(f: &Fn(FatFileSystem) -> (), filename: &str) {
} }
fn test_root_dir(fs: FatFileSystem) { fn test_root_dir(fs: FatFileSystem) {
let mut root_dir = fs.root_dir(); let root_dir = fs.root_dir();
let entries = root_dir.list().unwrap(); let entries = root_dir.map(|r| r.unwrap()).collect::<Vec<FatDirEntry>>();
let short_names = entries.iter().map(|e| e.short_file_name()).collect::<Vec<String>>(); let short_names = entries.iter().map(|e| e.short_file_name()).collect::<Vec<String>>();
assert_eq!(short_names, ["LONG.TXT", "SHORT.TXT", "VERY", "VERY-L~1"]); assert_eq!(short_names, ["LONG.TXT", "SHORT.TXT", "VERY", "VERY-L~1"]);
let names = entries.iter().map(|e| e.file_name()).collect::<Vec<String>>(); let names = entries.iter().map(|e| e.file_name()).collect::<Vec<String>>();
assert_eq!(names, ["long.txt", "short.txt", "very", "very-long-dir-name"]); assert_eq!(names, ["long.txt", "short.txt", "very", "very-long-dir-name"]);
// Try read again // Try read again
let entries = root_dir.list().unwrap(); //let names2 = root_dir.map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
let names2 = entries.iter().map(|e| e.file_name()).collect::<Vec<String>>(); //assert_eq!(names2, names);
assert_eq!(names2, names);
} }
#[test] #[test]
@ -106,9 +105,8 @@ fn test_read_long_file_fat32() {
fn test_get_dir_by_path(fs: FatFileSystem) { fn test_get_dir_by_path(fs: FatFileSystem) {
let mut root_dir = fs.root_dir(); let mut root_dir = fs.root_dir();
let mut dir = root_dir.open_dir("very/long/path/").unwrap(); let dir = root_dir.open_dir("very/long/path/").unwrap();
let entries = dir.list().unwrap(); let names = dir.map(|r| r.unwrap().file_name()).collect::<Vec<String>>();
let names = entries.iter().map(|e| e.file_name()).collect::<Vec<String>>();
assert_eq!(names, [".", "..", "test.txt"]); assert_eq!(names, [".", "..", "test.txt"]);
} }