From cae983f2cc93258c69e0f28e149d5da7802bb464 Mon Sep 17 00:00:00 2001 From: Eric Kidd Date: Thu, 15 Sep 2016 10:29:29 -0400 Subject: Allow iteration over available file names Your example program included an unimplemented `entries` method. I chose not to implement `entries` because iterating over it would require decompressing any large files stored in the map. Instead, I've provided a `file_names` API that provides just the names of the files in the map, which the caller can look up (and potentially decompress) if they wish. I implemented a custom iterator wrapper to hide the implementation details from the caller. --- lib/src/lib.rs | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) (limited to 'lib') diff --git a/lib/src/lib.rs b/lib/src/lib.rs index ce1e2ec..57d3610 100644 --- a/lib/src/lib.rs +++ b/lib/src/lib.rs @@ -37,6 +37,12 @@ impl Files { self.files.contains_key(path) } + /// Returns an iterator over all available file names. Does not + /// decompress any compressed data. + pub fn file_names(&self) -> FileNames { + FileNames { iter: self.files.keys() } + } + pub fn get(&self, path: &str) -> io::Result> { let key = as_key(path); match self.files.get(key.borrow() as &str) { @@ -83,3 +89,18 @@ impl Files { } } } + +/// Iterates over the file names available for `Files` object. +pub struct FileNames<'a> { + /// Our internal iterator. We wrap this in a nice struct so our + /// caller doesn't need to know the details. + iter: phf::map::Keys<'a, &'static str, (Compression, &'static [u8])>, +} + +impl<'a> Iterator for FileNames<'a> { + type Item = &'static str; + + fn next(&mut self) -> Option { + self.iter.next().map(|s| *s) + } +} -- cgit v1.2.3